+def GetAnimationScene():
+ """Returns the application-wide animation scene. ParaView has only one
+ global animation scene. This method provides access to that. Users are
+ free to create additional animation scenes directly, but those scenes
+ won't be shown in the ParaView GUI."""
+ animation_proxies = servermanager.ProxyManager().GetProxiesInGroup("animation")
+ scene = None
+ for aProxy in animation_proxies.values():
+ if aProxy.GetXMLName() == "AnimationScene":
+ scene = aProxy
+ break
+ if not scene:
+ raise servermanager.MissingProxy, "Could not locate global AnimationScene."
+ return scene
+
+def WriteAnimation(filename, **params):
+ """Writes the current animation as a file. Optionally one can specify
+ arguments that qualify the saved animation files as keyword arguments.
+ Accepted options are as follows:
+ * Magnification (integer) : set the maginification factor for the saved
+ animation.
+ * Quality (0 [worst] or 1 or 2 [best]) : set the quality of the generated
+ movie (if applicable).
+ * Subsampling (integer) : setting whether the movie encoder should use
+ subsampling of the chrome planes or not, if applicable. Since the human
+ eye is more sensitive to brightness than color variations, subsampling
+ can be useful to reduce the bitrate. Default value is 0.
+ * BackgroundColor (3-tuple of doubles) : set the RGB background color to
+ use to fill empty spaces in the image.
+ * FrameRate (double): set the frame rate (if applicable)."""
+ scene = GetAnimationScene()
+ # ensures that the TimeKeeper track is created.
+ GetTimeTrack()
+ iw = servermanager.vtkSMAnimationSceneImageWriter()
+ iw.SetAnimationScene(scene.SMProxy)
+ iw.SetFileName(filename)
+ if params.has_key("Magnification"):
+ iw.SetMagnification(int(params["Magnification"]))
+ if params.has_key("Quality"):
+ iw.SetQuality(int(params["Quality"]))
+ if params.has_key("Subsampling"):
+ iw.SetSubsampling(int(params["Subsampling"]))
+ if params.has_key("BackgroundColor"):
+ iw.SetBackgroundColor(params["BackgroundColor"])
+ if params.has_key("FrameRate"):
+ iw.SetFrameRate(float(params["FrameRate"]))
+ iw.Save()
+
+def _GetRepresentationAnimationHelper(sourceproxy):
+ """Internal method that returns the representation animation helper for a
+ source proxy. It creates a new one if none exists."""
+ # ascertain that proxy is a source proxy
+ if not sourceproxy in GetSources().values():
+ return None
+ for proxy in servermanager.ProxyManager():
+ if proxy.GetXMLName() == "RepresentationAnimationHelper" and\
+ proxy.GetProperty("Source").IsProxyAdded(sourceproxy.SMProxy):
+ return proxy
+ # create a new helper
+ proxy = servermanager.misc.RepresentationAnimationHelper(
+ Source=sourceproxy)
+ servermanager.ProxyManager().RegisterProxy(
+ "pq_helper_proxies.%s" % sourceproxy.GetGlobalIDAsString(),
+ "RepresentationAnimationHelper", proxy)
+ return proxy
+
+def GetAnimationTrack(propertyname_or_property, index=None, proxy=None):
+ """Returns an animation cue for the property. If one doesn't exist then a
+ new one will be created.
+ Typical usage:
+ track = GetAnimationTrack("Center", 0, sphere) or
+ track = GetAnimationTrack(sphere.GetProperty("Radius")) or
+
+ # this returns the track to animate visibility of the active source in
+ # all views.
+ track = GetAnimationTrack("Visibility")
+
+ For animating properties on implicit planes etc., use the following
+ signatures:
+ track = GetAnimationTrack(slice.SliceType.GetProperty("Origin"), 0) or
+ track = GetAnimationTrack("Origin", 0, slice.SliceType)
+
+ """
+ if not proxy:
+ proxy = GetActiveSource()
+ if not isinstance(proxy, servermanager.Proxy):
+ raise TypeError, "proxy must be a servermanager.Proxy instance"
+ if isinstance(propertyname_or_property, str):
+ propertyname = propertyname_or_property
+ elif isinstance(propertyname_or_property, servermanager.Property):
+ prop = propertyname_or_property
+ propertyname = prop.Name
+ proxy = prop.Proxy
+ else:
+ raise TypeError, "propertyname_or_property must be a string or servermanager.Property"
+
+ # To handle the case where the property is actually a "display" property, in
+ # which case we are actually animating the "RepresentationAnimationHelper"
+ # associated with the source.
+ if propertyname in ["Visibility", "Opacity"]:
+ proxy = _GetRepresentationAnimationHelper(proxy)
+ if not proxy or not proxy.GetProperty(propertyname):
+ raise AttributeError, "Failed to locate property %s" % propertyname
+
+ scene = GetAnimationScene()
+ for cue in scene.Cues:
+ try:
+ if cue.AnimatedProxy.IsSame(proxy) and\
+ cue.AnimatedPropertyName == propertyname:
+ if index == None or index.IsSame(cue.AnimatedElement): ##index == cue.AnimatedElement:
+ return cue
+ except AttributeError:
+ pass
+
+ # matching animation track wasn't found, create a new one.
+ cue = KeyFrameAnimationCue()
+ cue.AnimatedProxy = proxy
+ cue.AnimatedPropertyName = propertyname
+ if index != None:
+ cue.AnimatedElement = index
+ scene.Cues.append(cue)
+ return cue
+
+def GetCameraTrack(view=None):
+ """Returns the camera animation track for the given view. If no view is
+ specified, active view will be used. If no exisiting camera animation track
+ is found, a new one will be created."""
+ if not view:
+ view = GetActiveView()
+ if not view:
+ raise ValueError, "No view specified"
+ scene = GetAnimationScene()
+ for cue in scene.Cues:
+ if cue.AnimatedProxy.IsSame(view) and\
+ cue.GetXMLName() == "CameraAnimationCue":
+ return cue
+ # no cue was found, create a new one.
+ cue = CameraAnimationCue()
+ cue.AnimatedProxy = view
+ scene.Cues.append(cue)
+ return cue
+
+def GetTimeTrack():
+ """Returns the animation track used to control the time requested from all
+ readers/filters during playback.
+ This is the "TimeKeeper - Time" track shown in ParaView's 'Animation View'.
+ If none exists, a new one will be created."""
+ scene = GetAnimationScene()
+ tk = scene.TimeKeeper
+ for cue in scene.Cues:
+ if cue.GetXMLName() == "TimeAnimationCue" and cue.AnimatedProxy.IsSame(tk)\
+ and cue.AnimatedPropertyName == "Time":
+ return cue
+ # no cue was found, create a new one.
+ cue = TimeAnimationCue()
+ cue.AnimatedProxy = tk
+ cue.AnimatedPropertyName = "Time"
+ scene.Cues.append(cue)
+ return cue
+