1 # Copyright (C) 2010-2012 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 r"""simple is a module for using paraview server manager in Python. It
21 provides a simple convenience layer to functionality provided by the
22 C++ classes wrapped to Python as well as the servermanager module.
25 from paraview.simple import *
27 # Create a new sphere proxy on the active connection and register it
28 # in the sources group.
29 sphere = Sphere(ThetaResolution=16, PhiResolution=32)
31 # Apply a shrink filter
32 shrink = Shrink(sphere)
34 # Turn the visiblity of the shrink object on.
43 servermanager = paravisSM
46 if servermanager.ActiveConnection:
47 servermanager.ProxyManager().UnRegisterProxies()
48 active_objects.view = None
49 active_objects.source = None
52 servermanager.Disconnect()
54 def Connect(ds_host=None, ds_port=11111, rs_host=None, rs_port=11111):
55 """Creates a connection to a server. Example usage:
56 > Connect("amber") # Connect to a single server at default port
57 > Connect("amber", 12345) # Connect to a single server at port 12345
58 > Connect("amber", 11111, "vis_cluster", 11111) # connect to data server, render server pair"""
60 session = servermanager.Connect(ds_host, ds_port, rs_host, rs_port)
61 _add_functions(globals())
63 tk = servermanager.misc.TimeKeeper()
64 servermanager.ProxyManager().RegisterProxy("timekeeper", "tk", tk)
65 scene = AnimationScene()
69 def ReverseConnect(port=11111):
70 """Create a reverse connection to a server. Listens on port and waits for
71 an incoming connection from the server."""
73 session = servermanager.ReverseConnect(port)
74 _add_functions(globals())
75 tk = servermanager.misc.TimeKeeper()
76 servermanager.ProxyManager().RegisterProxy("timekeeper", "tk", tk)
77 scene = AnimationScene()
81 def _create_view(view_xml_name):
82 "Creates and returns a 3D render view."
83 view = servermanager._create_view(view_xml_name)
84 servermanager.ProxyManager().RegisterProxy("views", \
85 "my_view%d" % _funcs_internals.view_counter, view)
86 active_objects.view = view
87 _funcs_internals.view_counter += 1
89 tk = servermanager.ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
94 scene = GetAnimationScene()
95 if not view in scene.ViewModules:
96 scene.ViewModules.append(view)
97 except servermanager.MissingProxy:
101 def CreateRenderView():
102 return _create_view("RenderView")
104 def CreateXYPlotView():
105 return _create_view("XYChartView")
107 def CreateBarChartView():
108 return _create_view("XYBarChartView")
110 def CreateComparativeRenderView():
111 return _create_view("ComparativeRenderView")
113 def CreateComparativeXYPlotView():
114 return _create_view("ComparativeXYPlotView")
116 def CreateComparativeBarChartView():
117 return _create_view("ComparativeBarChartView")
119 def CreateParallelCoordinatesChartView():
120 return _create_view("ParallelCoordinatesChartView")
122 def Create2DRenderView():
123 return _create_view("2DRenderView")
125 def OpenDataFile(filename, **extraArgs):
126 """Creates a reader to read the give file, if possible.
127 This uses extension matching to determine the best reader possible.
128 If a reader cannot be identified, then this returns None."""
129 reader_factor = servermanager.ProxyManager().GetReaderFactory()
130 if reader_factor.GetNumberOfRegisteredPrototypes() == 0:
131 reader_factor.RegisterPrototypes("sources")
132 session = servermanager.ActiveConnection.Session
133 first_file = filename
134 if type(filename) == list:
135 first_file = filename[0]
136 if not reader_factor.TestFileReadability(first_file, session):
137 raise RuntimeError, "File not readable: %s " % first_file
138 if not reader_factor.CanReadFile(first_file, session):
139 raise RuntimeError, "File not readable. No reader found for '%s' " % first_file
140 prototype = servermanager.ProxyManager().GetPrototypeProxy(
141 reader_factor.GetReaderGroup(), reader_factor.GetReaderName())
142 xml_name = paraview.make_name_valid(prototype.GetXMLLabel())
143 reader_func = _create_func(xml_name, servermanager.sources)
144 if prototype.GetProperty("FileNames"):
145 reader = reader_func(FileNames=filename, **extraArgs)
147 reader = reader_func(FileName=filename, **extraArgs)
150 def CreateWriter(filename, proxy=None, **extraArgs):
151 """Creates a writer that can write the data produced by the source proxy in
152 the given file format (identified by the extension). If no source is
153 provided, then the active source is used. This doesn't actually write the
154 data, it simply creates the writer and returns it."""
156 raise RuntimeError, "filename must be specified"
157 writer_factory = servermanager.ProxyManager().GetWriterFactory()
158 if writer_factory.GetNumberOfRegisteredPrototypes() == 0:
159 writer_factory.RegisterPrototypes("writers")
161 proxy = GetActiveSource()
163 raise RuntimeError, "Could not locate source to write"
164 writer_proxy = writer_factory.CreateWriter(filename, proxy.SMProxy, proxy.Port)
165 return servermanager._getPyProxy(writer_proxy)
168 """Returns the active view if there is one. Else creates and returns a new view."""
169 view = active_objects.view
171 # it's possible that there's no active view, but a render view exists.
172 # If so, locate that and return it (before trying to create a new one).
173 view = servermanager.GetRenderView()
175 view = CreateRenderView()
178 def GetRenderViews():
179 """Returns all render views as a list."""
180 return servermanager.GetRenderViews()
182 def GetRepresentation(proxy=None, view=None):
183 """Given a pipeline object and view, returns the corresponding representation object.
184 If pipeline object and view are not specified, active objects are used."""
186 view = active_objects.view
188 proxy = active_objects.source
189 rep = servermanager.GetRepresentation(proxy, view)
191 rep = servermanager.CreateRepresentation(proxy, view)
192 servermanager.ProxyManager().RegisterProxy("representations", \
193 "my_representation%d" % _funcs_internals.rep_counter, rep)
194 _funcs_internals.rep_counter += 1
197 def GetDisplayProperties(proxy=None, view=None):
198 """Given a pipeline object and view, returns the corresponding representation object.
199 If pipeline object and/or view are not specified, active objects are used."""
200 return GetRepresentation(proxy, view)
202 def Show(proxy=None, view=None, **params):
203 """Turns the visibility of a given pipeline object on in the given view.
204 If pipeline object and/or view are not specified, active objects are used."""
206 proxy = GetActiveSource()
208 raise RuntimeError, "Show() needs a proxy argument or that an active source is set."
209 if not view and not active_objects.view:
211 rep = GetDisplayProperties(proxy, view)
213 raise RuntimeError, "Could not create a representation object for proxy %s" % proxy.GetXMLLabel()
214 for param in params.keys():
215 setattr(rep, param, params[param])
219 def Hide(proxy=None, view=None):
220 """Turns the visibility of a given pipeline object off in the given view.
221 If pipeline object and/or view are not specified, active objects are used."""
222 rep = GetDisplayProperties(proxy, view)
225 def Render(view=None):
226 """Renders the given view (default value is active view)"""
228 view = active_objects.view
230 if _funcs_internals.first_render:
231 # Not all views have a ResetCamera method
235 except AttributeError: pass
236 _funcs_internals.first_render = False
239 def ResetCamera(view=None):
240 """Resets the settings of the camera to preserver orientation but include
241 the whole scene. If an argument is not provided, the active view is
244 view = active_objects.view
245 if hasattr(view, "ResetCamera"):
247 if hasattr(view, "ResetDisplay"):
251 def _DisableFirstRenderCameraReset():
252 """Disable the first render camera reset. Normally a ResetCamera is called
253 automatically when Render is called for the first time after importing
255 _funcs_internals.first_render = False
257 def SetProperties(proxy=None, **params):
258 """Sets one or more properties of the given pipeline object. If an argument
259 is not provided, the active source is used. Pass a list of property_name=value
260 pairs to this function to set property values. For example:
261 SetProperties(Center=[1, 2, 3], Radius=3.5)
264 proxy = active_objects.source
265 for param in params.keys():
266 if not hasattr(proxy, param):
267 raise AttributeError("object has no property %s" % param)
268 setattr(proxy, param, params[param])
270 def GetProperty(*arguments, **keywords):
271 """Get one property of the given pipeline object. If keywords are used,
272 you can set the proxy and the name of the property that you want to get
273 like in the following example :
274 GetProperty({proxy=sphere, name="Radius"})
275 If it's arguments that are used, then you have two case:
276 - if only one argument is used that argument will be
278 - if two arguments are used then the first one will be
279 the proxy and the second one the property name.
280 Several example are given below:
281 GetProperty({name="Radius"})
282 GetProperty({proxy=sphereProxy, name="Radius"})
283 GetProperty( sphereProxy, "Radius" )
284 GetProperty( "Radius" )
292 proxy = keywords[key]
293 if len(arguments) == 1 :
295 if len(arguments) == 2 :
299 raise RuntimeError, "Expecting at least a property name as input. Otherwise keyword could be used to set 'proxy' and property 'name'"
301 proxy = active_objects.source
302 return proxy.GetProperty(name)
304 def SetDisplayProperties(proxy=None, view=None, **params):
305 """Sets one or more display properties of the given pipeline object. If an argument
306 is not provided, the active source is used. Pass a list of property_name=value
307 pairs to this function to set property values. For example:
308 SetProperties(Color=[1, 0, 0], LineWidth=2)
310 rep = GetDisplayProperties(proxy, view)
311 SetProperties(rep, **params)
313 def SetViewProperties(view=None, **params):
314 """Sets one or more properties of the given view. If an argument
315 is not provided, the active view is used. Pass a list of property_name=value
316 pairs to this function to set property values. For example:
317 SetProperties(Background=[1, 0, 0], UseImmediateMode=0)
320 view = active_objects.view
321 SetProperties(view, **params)
323 def RenameSource(newName, proxy=None):
324 """Renames the given source. If the given proxy is not registered
325 in the sources group this method will have no effect. If no source is
326 provided, the active source is used."""
328 proxy = active_objects.source
329 pxm = servermanager.ProxyManager()
330 oldName = pxm.GetProxyName("sources", proxy)
332 pxm.RegisterProxy("sources", newName, proxy)
333 pxm.UnRegisterProxy("sources", oldName, proxy)
335 def FindSource(name):
336 return servermanager.ProxyManager().GetProxy("sources", name)
339 """Given the name of a source, return its Python object."""
340 return servermanager.ProxyManager().GetProxiesInGroup("sources")
342 def GetRepresentations():
343 """Returns all representations (display properties)."""
344 return servermanager.ProxyManager().GetProxiesInGroup("representations")
346 def UpdatePipeline(time=None, proxy=None):
347 """Updates (executes) the given pipeline object for the given time as
348 necessary (i.e. if it did not already execute). If no source is provided,
349 the active source is used instead."""
351 proxy = active_objects.source
353 proxy.UpdatePipeline(time)
355 proxy.UpdatePipeline()
357 def Delete(proxy=None):
358 """Deletes the given pipeline object or the active source if no argument
361 proxy = active_objects.source
362 # Unregister any helper proxies stored by a vtkSMProxyListDomain
364 listdomain = prop.GetDomain('proxy_list')
366 if listdomain.GetClassName() != 'vtkSMProxyListDomain':
368 group = "pq_helper_proxies." + proxy.GetGlobalIDAsString()
369 for i in xrange(listdomain.GetNumberOfProxies()):
370 pm = servermanager.ProxyManager()
371 iproxy = listdomain.GetProxy(i)
372 name = pm.GetProxyName(group, iproxy)
374 pm.UnRegisterProxy(group, name, iproxy)
376 # Remove source/view from time keeper
377 tk = servermanager.ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
378 if isinstance(proxy, servermanager.SourceProxy):
380 idx = tk.TimeSources.index(proxy)
381 del tk.TimeSources[idx]
386 idx = tk.Views.index(proxy)
390 servermanager.UnRegister(proxy)
392 # If this is a representation, remove it from all views.
393 if proxy.SMProxy.IsA("vtkSMRepresentationProxy") or \
394 proxy.SMProxy.IsA("vtkSMNewWidgetRepresentationProxy"):
395 for view in GetRenderViews():
396 view.Representations.remove(proxy)
397 # If this is a source, remove the representation iff it has no consumers
398 # Also change the active source if necessary
399 elif proxy.SMProxy.IsA("vtkSMSourceProxy"):
400 sources = servermanager.ProxyManager().GetProxiesInGroup("sources")
401 for i in range(proxy.GetNumberOfConsumers()):
402 if proxy.GetConsumerProxy(i) in sources:
403 raise RuntimeError("Source has consumers. It cannot be deleted " +
404 "until all consumers are deleted.")
406 if proxy.IsSame(GetActiveSource()):
407 if hasattr(proxy, "Input") and proxy.Input:
408 if isinstance(proxy.Input, servermanager.Proxy):
409 SetActiveSource(proxy.Input)
411 SetActiveSource(proxy.Input[0])
412 else: SetActiveSource(None)
413 for rep in GetRepresentations().values():
415 if rep.Input.IsSame(proxy):
417 # Change the active view if necessary
418 elif proxy.SMProxy.IsA("vtkSMRenderViewProxy"):
420 if proxy.IsSame(GetActiveView()):
421 if len(GetRenderViews()) > 0:
422 SetActiveView(GetRenderViews()[0])
426 def CreateLookupTable(**params):
427 """Create and return a lookup table. Optionally, parameters can be given
428 to assign to the lookup table.
430 lt = servermanager.rendering.PVLookupTable()
431 servermanager.Register(lt)
432 SetProperties(lt, **params)
435 def CreatePiecewiseFunction(**params):
436 """Create and return a piecewise function. Optionally, parameters can be
437 given to assign to the piecewise function.
439 pfunc = servermanager.piecewise_functions.PiecewiseFunction()
440 servermanager.Register(pfunc)
441 SetProperties(pfunc, **params)
444 def GetLookupTableForArray(arrayname, num_components, **params):
445 """Used to get an existing lookuptable for a array or to create one if none
446 exists. Keyword arguments can be passed in to initialize the LUT if a new
448 proxyName = "%d.%s.PVLookupTable" % (int(num_components), arrayname)
449 lut = servermanager.ProxyManager().GetProxy("lookup_tables", proxyName)
452 # No LUT exists for this array, create a new one.
453 # TODO: Change this to go a LookupTableManager that is shared with the GUI,
454 # so that the GUI and python end up create same type of LUTs. For now,
455 # python will create a Blue-Red LUT, unless overridden by params.
456 lut = servermanager.rendering.PVLookupTable(
457 ColorSpace="HSV", RGBPoints=[0, 0, 0, 1, 1, 1, 0, 0])
458 SetProperties(lut, **params)
459 servermanager.Register(lut, registrationName=proxyName)
462 def CreateScalarBar(**params):
463 """Create and return a scalar bar widget. The returned widget may
464 be added to a render view by appending it to the view's representations
465 The widget must have a valid lookup table before it is added to a view.
466 It is possible to pass the lookup table (and other properties) as arguments
469 lt = MakeBlueToRedLt(3.5, 7.5)
470 bar = CreateScalarBar(LookupTable=lt, Title="Velocity")
471 GetRenderView().Representations.append(bar)
473 By default the returned widget is selectable and resizable.
475 sb = servermanager.rendering.ScalarBarWidgetRepresentation()
476 servermanager.Register(sb)
481 SetProperties(sb, **params)
484 # TODO: Change this to take the array name and number of components. Register
485 # the lt under the name ncomp.array_name
486 def MakeBlueToRedLT(min, max):
487 # Define RGB points. These are tuples of 4 values. First one is
488 # the scalar values, the other 3 the RGB values.
489 rgbPoints = [min, 0, 0, 1, max, 1, 0, 0]
490 return CreateLookupTable(RGBPoints=rgbPoints, ColorSpace="HSV")
492 def _find_writer(filename):
493 """Internal function."""
495 parts = filename.split('.')
497 extension = parts[-1]
499 raise RuntimeError, "Filename has no extension, please specify a write"
501 if extension == 'png':
502 return 'vtkPNGWriter'
503 elif extension == 'bmp':
504 return 'vtkBMPWriter'
505 elif extension == 'ppm':
506 return 'vtkPNMWriter'
507 elif extension == 'tif' or extension == 'tiff':
508 return 'vtkTIFFWriter'
509 elif extension == 'jpg' or extension == 'jpeg':
510 return 'vtkJPEGWriter'
512 raise RuntimeError, "Cannot infer filetype from extension:", extension
514 def AddCameraLink(viewProxy, viewProxyOther, linkName):
515 """Create a camera link between two view proxies. A name must be given
516 so that the link can be referred to by name. If a link with the given
517 name already exists it will be removed first."""
518 if not viewProxyOther: viewProxyOther = GetActiveView()
519 link = servermanager.vtkSMCameraLink()
520 link.AddLinkedProxy(viewProxy.SMProxy, 1)
521 link.AddLinkedProxy(viewProxyOther.SMProxy, 2)
522 link.AddLinkedProxy(viewProxyOther.SMProxy, 1)
523 link.AddLinkedProxy(viewProxy.SMProxy, 2)
524 RemoveCameraLink(linkName)
525 servermanager.ProxyManager().RegisterLink(linkName, link)
527 def RemoveCameraLink(linkName):
528 """Remove a camera link with the given name."""
529 servermanager.ProxyManager().UnRegisterLink(linkName)
531 def WriteImage(filename, view=None, **params):
532 """Saves the given view (or the active one if none is given) as an
533 image. Optionally, you can specify the writer and the magnification
534 using the Writer and Magnification named arguments. For example:
535 WriteImage("foo.mypng", aview, Writer=vtkPNGWriter, Magnification=2)
536 If no writer is provided, the type is determined from the file extension.
537 Currently supported extensions are png, bmp, ppm, tif, tiff, jpg and jpeg.
538 The writer is a VTK class that is capable of writing images.
539 Magnification is used to determine the size of the written image. The size
540 is obtained by multiplying the size of the view with the magnification.
541 Rendering may be done using tiling to obtain the correct size without
542 resizing the view."""
544 view = active_objects.view
546 if params.has_key('Writer'):
547 writer = params['Writer']
549 if params.has_key('Magnification'):
550 mag = int(params['Magnification'])
552 writer = _find_writer(filename)
553 view.WriteImage(filename, writer, mag)
555 def AnimateReader(reader=None, view=None, filename=None):
556 """This is a utility function that, given a reader and a view
557 animates over all time steps of the reader. If the optional
558 filename is provided, a movie is created (type depends on the
559 extension of the filename."""
561 reader = active_objects.source
563 view = active_objects.view
565 return servermanager.AnimateReader(reader, view, filename)
568 def _create_func(key, module):
569 """Internal function."""
571 def CreateObject(*input, **params):
572 """This function creates a new proxy. For pipeline objects that accept inputs,
573 all non-keyword arguments are assumed to be inputs. All keyword arguments are
574 assumed to be property,value pairs and are passed to the new proxy."""
576 # Instantiate the actual object from the given module.
577 px = module.__dict__[key]()
579 # Make sure non-keyword arguments are valid
581 if inp != None and not isinstance(inp, servermanager.Proxy):
582 if px.GetProperty("Input") != None:
583 raise RuntimeError, "Expecting a proxy as input."
585 raise RuntimeError, "This function does not accept non-keyword arguments."
588 if px.GetProperty("Input") != None:
592 # If no input is specified, try the active pipeline object
593 if px.GetProperty("Input").GetRepeatable() and active_objects.get_selected_sources():
594 px.Input = active_objects.get_selected_sources()
595 elif active_objects.source:
596 px.Input = active_objects.source
599 raise RuntimeError, "This function does not expect an input."
601 registrationName = None
602 for nameParam in ['registrationName', 'guiName']:
603 if nameParam in params:
604 registrationName = params[nameParam]
605 del params[nameParam]
607 # Pass all the named arguments as property,value pairs
608 for param in params.keys():
609 setattr(px, param, params[param])
612 # Register the proxy with the proxy manager.
614 group, name = servermanager.Register(px, registrationName=registrationName)
616 group, name = servermanager.Register(px)
619 # Register pipeline objects with the time keeper. This is used to extract time values
620 # from sources. NOTE: This should really be in the servermanager controller layer.
621 if group == "sources":
622 tk = servermanager.ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
623 sources = tk.TimeSources
624 if not px in sources:
627 active_objects.source = px
628 except servermanager.MissingRegistrationInformation:
635 def _create_doc(new, old):
636 """Internal function."""
639 for doc in (new, old):
641 strpd = doc.split('\n')
643 ts.append(s.lstrip())
644 res += string.join(ts)
648 def _func_name_valid(name):
649 """Internal function."""
652 if c == '(' or c ==')':
657 def _add_functions(g):
658 for m in [servermanager.filters, servermanager.sources,
659 servermanager.writers, servermanager.animation]:
661 for key in dt.keys():
663 if not isinstance(cl, str):
664 if not key in g and _func_name_valid(key):
665 g[key] = _create_func(key, m)
666 exec "g[key].__doc__ = _create_doc(m.%s.__doc__, g[key].__doc__)" % key
669 """Returns the active view."""
670 return active_objects.view
672 def SetActiveView(view):
673 """Sets the active view."""
674 active_objects.view = view
676 def GetActiveSource():
677 """Returns the active source."""
678 return active_objects.source
680 def SetActiveSource(source):
681 """Sets the active source."""
682 active_objects.source = source
684 def GetActiveCamera():
685 """Returns the active camera for the active view. The returned object
686 is an instance of vtkCamera."""
687 return GetActiveView().GetActiveCamera()
689 def GetAnimationScene():
690 """Returns the application-wide animation scene. ParaView has only one
691 global animation scene. This method provides access to that. Users are
692 free to create additional animation scenes directly, but those scenes
693 won't be shown in the ParaView GUI."""
694 animation_proxies = servermanager.ProxyManager().GetProxiesInGroup("animation")
696 for aProxy in animation_proxies.values():
697 if aProxy.GetXMLName() == "AnimationScene":
701 raise servermanager.MissingProxy, "Could not locate global AnimationScene."
704 def WriteAnimation(filename, **params):
705 """Writes the current animation as a file. Optionally one can specify
706 arguments that qualify the saved animation files as keyword arguments.
707 Accepted options are as follows:
708 * Magnification (integer) : set the maginification factor for the saved
710 * Quality (0 [worst] or 1 or 2 [best]) : set the quality of the generated
711 movie (if applicable).
712 * Subsampling (integer) : setting whether the movie encoder should use
713 subsampling of the chrome planes or not, if applicable. Since the human
714 eye is more sensitive to brightness than color variations, subsampling
715 can be useful to reduce the bitrate. Default value is 0.
716 * BackgroundColor (3-tuple of doubles) : set the RGB background color to
717 use to fill empty spaces in the image.
718 * FrameRate (double): set the frame rate (if applicable)."""
719 scene = GetAnimationScene()
720 # ensures that the TimeKeeper track is created.
722 iw = servermanager.vtkSMAnimationSceneImageWriter()
723 iw.SetAnimationScene(scene.SMProxy)
724 iw.SetFileName(filename)
725 if params.has_key("Magnification"):
726 iw.SetMagnification(int(params["Magnification"]))
727 if params.has_key("Quality"):
728 iw.SetQuality(int(params["Quality"]))
729 if params.has_key("Subsampling"):
730 iw.SetSubsampling(int(params["Subsampling"]))
731 if params.has_key("BackgroundColor"):
732 iw.SetBackgroundColor(params["BackgroundColor"])
733 if params.has_key("FrameRate"):
734 iw.SetFrameRate(float(params["FrameRate"]))
737 def _GetRepresentationAnimationHelper(sourceproxy):
738 """Internal method that returns the representation animation helper for a
739 source proxy. It creates a new one if none exists."""
740 # ascertain that proxy is a source proxy
741 if not sourceproxy in GetSources().values():
743 for proxy in servermanager.ProxyManager():
744 if proxy.GetXMLName() == "RepresentationAnimationHelper" and\
745 proxy.GetProperty("Source").IsProxyAdded(sourceproxy.SMProxy):
747 # create a new helper
748 proxy = servermanager.misc.RepresentationAnimationHelper(
750 servermanager.ProxyManager().RegisterProxy(
751 "pq_helper_proxies.%s" % sourceproxy.GetGlobalIDAsString(),
752 "RepresentationAnimationHelper", proxy)
755 def GetAnimationTrack(propertyname_or_property, index=None, proxy=None):
756 """Returns an animation cue for the property. If one doesn't exist then a
757 new one will be created.
759 track = GetAnimationTrack("Center", 0, sphere) or
760 track = GetAnimationTrack(sphere.GetProperty("Radius")) or
762 # this returns the track to animate visibility of the active source in
764 track = GetAnimationTrack("Visibility")
766 For animating properties on implicit planes etc., use the following
768 track = GetAnimationTrack(slice.SliceType.GetProperty("Origin"), 0) or
769 track = GetAnimationTrack("Origin", 0, slice.SliceType)
773 proxy = GetActiveSource()
774 if not isinstance(proxy, servermanager.Proxy):
775 raise TypeError, "proxy must be a servermanager.Proxy instance"
776 if isinstance(propertyname_or_property, str):
777 propertyname = propertyname_or_property
778 elif isinstance(propertyname_or_property, servermanager.Property):
779 prop = propertyname_or_property
780 propertyname = prop.Name
783 raise TypeError, "propertyname_or_property must be a string or servermanager.Property"
785 # To handle the case where the property is actually a "display" property, in
786 # which case we are actually animating the "RepresentationAnimationHelper"
787 # associated with the source.
788 if propertyname in ["Visibility", "Opacity"]:
789 proxy = _GetRepresentationAnimationHelper(proxy)
790 if not proxy or not proxy.GetProperty(propertyname):
791 raise AttributeError, "Failed to locate property %s" % propertyname
793 scene = GetAnimationScene()
794 for cue in scene.Cues:
796 if cue.AnimatedProxy.IsSame(proxy) and\
797 cue.AnimatedPropertyName == propertyname:
798 if index == None or index.IsSame(cue.AnimatedElement): ##index == cue.AnimatedElement:
800 except AttributeError:
803 # matching animation track wasn't found, create a new one.
804 cue = KeyFrameAnimationCue()
805 cue.AnimatedProxy = proxy
806 cue.AnimatedPropertyName = propertyname
808 cue.AnimatedElement = index
809 scene.Cues.append(cue)
812 def GetCameraTrack(view=None):
813 """Returns the camera animation track for the given view. If no view is
814 specified, active view will be used. If no exisiting camera animation track
815 is found, a new one will be created."""
817 view = GetActiveView()
819 raise ValueError, "No view specified"
820 scene = GetAnimationScene()
821 for cue in scene.Cues:
822 if cue.AnimatedProxy.IsSame(view) and\
823 cue.GetXMLName() == "CameraAnimationCue":
825 # no cue was found, create a new one.
826 cue = CameraAnimationCue()
827 cue.AnimatedProxy = view
828 scene.Cues.append(cue)
832 """Returns the animation track used to control the time requested from all
833 readers/filters during playback.
834 This is the "TimeKeeper - Time" track shown in ParaView's 'Animation View'.
835 If none exists, a new one will be created."""
836 scene = GetAnimationScene()
837 tk = scene.TimeKeeper
838 for cue in scene.Cues:
839 if cue.GetXMLName() == "TimeAnimationCue" and cue.AnimatedProxy.IsSame(tk)\
840 and cue.AnimatedPropertyName == "Time":
842 # no cue was found, create a new one.
843 cue = TimeAnimationCue()
844 cue.AnimatedProxy = tk
845 cue.AnimatedPropertyName = "Time"
846 scene.Cues.append(cue)
849 def LoadXML(xmlstring, ns=None):
850 """Given a server manager XML as a string, parse and process it.
851 If you loaded the simple module with from paraview.simple import *,
852 make sure to pass globals() as the second arguments:
853 LoadXML(xmlstring, globals())
854 Otherwise, the new functions will not appear in the global namespace."""
857 servermanager.LoadXML(xmlstring)
860 def LoadPlugin(filename, remote=True, ns=None):
861 """Loads a ParaView plugin and updates this module with new constructors
862 if any. The remote argument (default to True) is to specify whether
863 the plugin will be loaded on client (remote=False) or on server (remote=True).
864 If you loaded the simple module with from paraview.simple import *,
865 make sure to pass globals() as an argument:
866 LoadPlugin("myplugin", False, globals()), to load on client;
867 LoadPlugin("myplugin", True, globals()), to load on server;
868 LoadPlugin("myplugin", ns=globals()), to load on server.
869 Otherwise, the new functions will not appear in the global namespace."""
873 servermanager.LoadPlugin(filename, remote)
876 def LoadDistributedPlugin(pluginname, remote=True, ns=None):
877 """Loads a plugin that's distributed with the executable. This uses the
878 information known about plugins distributed with ParaView to locate the
879 shared library for the plugin to load. Raises a RuntimeError if the plugin
881 plm = servermanager.ProxyManager().GetSession().GetPluginManager()
883 info = plm.GetRemoteInformation()
885 info = plm.GetLocalInformation()
886 for cc in range(0, info.GetNumberOfPlugins()):
887 if info.GetPluginName(cc) == pluginname:
888 return LoadPlugin(info.GetPluginFileName(cc), remote, ns)
889 raise RuntimeError, "Plugin '%s' not found" % pluginname
892 class ActiveObjects(object):
893 """This class manages the active objects (source and view). The active
894 objects are shared between Python and the user interface. This class
895 is for internal use. Use the Set/Get methods for setting and getting
897 def __get_selection_model(self, name):
899 pxm = servermanager.ProxyManager()
900 model = pxm.GetSelectionModel(name)
902 model = servermanager.vtkSMProxySelectionModel()
903 pxm.RegisterSelectionModel(name, model)
906 def set_view(self, view):
907 "Sets the active view."
908 active_view_model = self.__get_selection_model("ActiveView")
910 active_view_model.SetCurrentProxy(view.SMProxy, 0)
912 active_view_model.SetCurrentProxy(None, 0)
915 "Returns the active view."
916 return servermanager._getPyProxy(
917 self.__get_selection_model("ActiveView").GetCurrentProxy())
919 def set_source(self, source):
920 "Sets the active source."
921 active_sources_model = self.__get_selection_model("ActiveSources")
923 # 3 == CLEAR_AND_SELECT
924 active_sources_model.SetCurrentProxy(source.SMProxy, 3)
926 active_sources_model.SetCurrentProxy(None, 3)
928 def __convert_proxy(self, px):
932 if px.IsA("vtkSMSourceProxy"):
933 return servermanager._getPyProxy(px)
935 return servermanager.OutputPort(
936 servermanager._getPyProxy(px.GetSourceProxy()),
939 def get_source(self):
940 "Returns the active source."
941 return self.__convert_proxy(
942 self.__get_selection_model("ActiveSources").GetCurrentProxy())
944 def get_selected_sources(self):
945 "Returns the set of sources selected in the pipeline browser."
946 model = self.__get_selection_model("ActiveSources")
948 for i in xrange(model.GetNumberOfSelectedProxies()):
949 proxies.append(self.__convert_proxy(model.GetSelectedProxy(i)))
952 view = property(get_view, set_view)
953 source = property(get_source, set_source)
955 class _funcs_internals:
956 """Internal class."""
962 """Simple demo that create the following pipeline
967 # Create a sphere of radius = 2, theta res. = 32
968 # This object becomes the active source.
969 ss = Sphere(Radius=2, ThetaResolution=32)
970 # Apply the shrink filter. The Input property is optional. If Input
971 # is not specified, the filter is applied to the active source.
972 shr = Shrink(Input=ss)
973 # Create a cone source.
975 # Append cone and shrink
976 app = AppendDatasets()
977 app.Input = [shr, cs]
978 # Show the output of the append filter. The argument is optional
979 # as the app filter is now the active object.
981 # Render the default view.
984 def demo2(fname="/Users/berk/Work/ParaView/ParaViewData/Data/disk_out_ref.ex2"):
985 """This demo shows the use of readers, data information and display
988 # Create the exodus reader and specify a file name
989 reader = ExodusIIReader(FileName=fname)
990 # Get the list of point arrays.
991 avail = reader.PointVariables.Available
994 reader.PointVariables = avail
996 # Turn on the visibility of the reader
998 # Set representation to wireframe
999 SetDisplayProperties(Representation = "Wireframe")
1000 # Black background is not pretty
1001 SetViewProperties(Background = [0.4, 0.4, 0.6])
1003 # Change the elevation of the camera. See VTK documentation of vtkCamera
1004 # for camera parameters.
1005 # NOTE: THIS WILL BE SIMPLER
1006 GetActiveCamera().Elevation(45)
1008 # Now that the reader executed, let's get some information about it's
1010 pdi = reader[0].PointData
1011 # This prints a list of all read point data arrays as well as their
1013 print 'Number of point arrays:', len(pdi)
1014 for i in range(len(pdi)):
1016 print "----------------"
1017 print "Array:", i, " ", ai.Name, ":"
1018 numComps = ai.GetNumberOfComponents()
1019 print "Number of components:", numComps
1020 for j in range(numComps):
1021 print "Range:", ai.GetRange(j)
1022 # White is boring. Let's color the geometry using a variable.
1023 # First create a lookup table. This object controls how scalar
1024 # values are mapped to colors. See VTK documentation for
1026 # Map min (0.00678) to blue, max (0.0288) to red
1027 SetDisplayProperties(LookupTable = MakeBlueToRedLT(0.00678, 0.0288))
1028 # Color by point array called Pres
1029 SetDisplayProperties(ColorAttributeType = "POINT_DATA")
1030 SetDisplayProperties(ColorArrayName = "Pres")
1034 print paravisSM.myParavis.GetTrace()
1036 def SaveTrace(fileName):
1037 paravisSM.myParavis.SaveTrace(fileName)
1040 _add_functions(globals())
1042 if not servermanager.ActiveConnection:
1045 def ImportFile(theFileName):
1046 paravisSM.ImportFile(theFileName)
1048 active_objects = ActiveObjects()
1049 active_objects.view = GetRenderView()