1 # Copyright (C) 2010-2013 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
45 def enableMultiServer():
46 servermanager.enableMultiServer()
48 def switchActiveConnection(newActiveConnection=None, ns=None):
52 servermanager.switchActiveConnection(newActiveConnection)
55 def Disconnect(ns=None, force=True):
56 if servermanager.ActiveConnection and (force or servermanager.MultiServerConnections == None):
59 _remove_functions(globals())
60 if not servermanager.fromGUI:
61 servermanager.ProxyManager().DisableStateUpdateNotification()
62 servermanager.ProxyManager().UnRegisterProxies()
63 active_objects.view = None
64 active_objects.source = None
65 servermanager.Disconnect()
66 if not servermanager.fromGUI:
70 def Connect(ds_host=None, ds_port=11111, rs_host=None, rs_port=11111):
71 """Creates a connection to a server. Example usage:
72 > Connect("amber") # Connect to a single server at default port
73 > Connect("amber", 12345) # Connect to a single server at port 12345
74 > Connect("amber", 11111, "vis_cluster", 11111) # connect to data server, render server pair"""
75 Disconnect(globals(), False)
76 connection = servermanager.Connect(ds_host, ds_port, rs_host, rs_port)
77 _add_functions(globals())
79 servermanager.ProxyManager().DisableStateUpdateNotification()
80 servermanager.ProxyManager().UpdateFromRemote()
81 tk = servermanager.ProxyManager().GetProxy("timekeeper", "TimeKeeper")
84 tk = servermanager.misc.TimeKeeper()
85 servermanager.ProxyManager().RegisterProxy("timekeeper", "TimeKeeper", tk)
86 except AttributeError:
87 print "Error: Could not create TimeKeeper"
89 scene = servermanager.ProxyManager().GetProxy("animation", "AnimationScene")
92 scene = AnimationScene()
95 print "Error: Could not create AnimationScene"
97 servermanager.ProxyManager().EnableStateUpdateNotification()
98 servermanager.ProxyManager().TriggerStateUpdate()
102 def ReverseConnect(port=11111):
103 """Create a reverse connection to a server. Listens on port and waits for
104 an incoming connection from the server."""
105 Disconnect(globals(), False)
106 connection = servermanager.ReverseConnect(port)
107 _add_functions(globals())
109 servermanager.ProxyManager().DisableStateUpdateNotification()
110 servermanager.ProxyManager().UpdateFromRemote()
111 tk = servermanager.ProxyManager().GetProxy("timekeeper", "TimeKeeper")
113 tk = servermanager.misc.TimeKeeper()
114 servermanager.ProxyManager().RegisterProxy("timekeeper", "TimeKeeper", tk)
116 scene = servermanager.ProxyManager().GetProxy("animation", "AnimationScene")
118 scene = AnimationScene()
119 scene.TimeKeeper = tk
121 servermanager.ProxyManager().EnableStateUpdateNotification()
122 servermanager.ProxyManager().TriggerStateUpdate()
126 def _create_view(view_xml_name):
127 "Creates and returns a 3D render view."
128 view = servermanager._create_view(view_xml_name)
129 servermanager.ProxyManager().RegisterProxy("views", \
130 "my_view%d" % _funcs_internals.view_counter, view)
131 active_objects.view = view
132 _funcs_internals.view_counter += 1
134 tk = servermanager.ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
136 if not view in views:
139 scene = GetAnimationScene()
140 if not view in scene.ViewModules:
141 scene.ViewModules.append(view)
142 except servermanager.MissingProxy:
146 def CreateRenderView():
147 return _create_view("RenderView")
149 def CreateXYPlotView():
150 return _create_view("XYChartView")
152 def CreateBarChartView():
153 return _create_view("XYBarChartView")
155 def CreateComparativeRenderView():
156 return _create_view("ComparativeRenderView")
158 def CreateComparativeXYPlotView():
159 return _create_view("ComparativeXYPlotView")
161 def CreateComparativeBarChartView():
162 return _create_view("ComparativeBarChartView")
164 def CreateParallelCoordinatesChartView():
165 return _create_view("ParallelCoordinatesChartView")
167 def Create2DRenderView():
168 return _create_view("2DRenderView")
170 def OpenDataFile(filename, **extraArgs):
171 """Creates a reader to read the give file, if possible.
172 This uses extension matching to determine the best reader possible.
173 If a reader cannot be identified, then this returns None."""
174 session = servermanager.ActiveConnection.Session
175 reader_factor = servermanager.vtkSMProxyManager.GetProxyManager().GetReaderFactory()
176 if reader_factor.GetNumberOfRegisteredPrototypes() == 0:
177 reader_factor.RegisterPrototypes(session, "sources")
178 first_file = filename
179 if type(filename) == list:
180 first_file = filename[0]
181 if not reader_factor.TestFileReadability(first_file, session):
182 msg = "File not readable: %s " % first_file
183 raise RuntimeError, msg
184 if not reader_factor.CanReadFile(first_file, session):
185 msg = "File not readable. No reader found for '%s' " % first_file
186 raise RuntimeError, msg
187 prototype = servermanager.ProxyManager().GetPrototypeProxy(
188 reader_factor.GetReaderGroup(), reader_factor.GetReaderName())
189 xml_name = paraview.make_name_valid(prototype.GetXMLLabel())
190 reader_func = _create_func(xml_name, servermanager.sources)
191 if prototype.GetProperty("FileNames"):
192 reader = reader_func(FileNames=filename, **extraArgs)
194 reader = reader_func(FileName=filename, **extraArgs)
197 def CreateWriter(filename, proxy=None, **extraArgs):
198 """Creates a writer that can write the data produced by the source proxy in
199 the given file format (identified by the extension). If no source is
200 provided, then the active source is used. This doesn't actually write the
201 data, it simply creates the writer and returns it."""
203 raise RuntimeError, "filename must be specified"
204 session = servermanager.ActiveConnection.Session
205 writer_factory = servermanager.vtkSMProxyManager.GetProxyManager().GetWriterFactory()
206 if writer_factory.GetNumberOfRegisteredPrototypes() == 0:
207 writer_factory.RegisterPrototypes(session, "writers")
209 proxy = GetActiveSource()
211 raise RuntimeError, "Could not locate source to write"
212 writer_proxy = writer_factory.CreateWriter(filename, proxy.SMProxy, proxy.Port)
213 return servermanager._getPyProxy(writer_proxy)
216 """Returns the active view if there is one. Else creates and returns a new view."""
217 view = active_objects.view
219 # it's possible that there's no active view, but a render view exists.
220 # If so, locate that and return it (before trying to create a new one).
221 view = servermanager.GetRenderView()
223 view = CreateRenderView()
226 def GetRenderViews():
227 """Returns all render views as a list."""
228 return servermanager.GetRenderViews()
230 def GetRepresentation(proxy=None, view=None):
231 """Given a pipeline object and view, returns the corresponding representation object.
232 If pipeline object and view are not specified, active objects are used."""
234 view = active_objects.view
236 proxy = active_objects.source
237 rep = servermanager.GetRepresentation(proxy, view)
239 rep = servermanager.CreateRepresentation(proxy, view)
240 servermanager.ProxyManager().RegisterProxy("representations", \
241 "my_representation%d" % _funcs_internals.rep_counter, rep)
242 _funcs_internals.rep_counter += 1
245 def GetDisplayProperties(proxy=None, view=None):
246 """Given a pipeline object and view, returns the corresponding representation object.
247 If pipeline object and/or view are not specified, active objects are used."""
248 return GetRepresentation(proxy, view)
250 def Show(proxy=None, view=None, **params):
251 """Turns the visibility of a given pipeline object on in the given view.
252 If pipeline object and/or view are not specified, active objects are used."""
254 proxy = GetActiveSource()
256 raise RuntimeError, "Show() needs a proxy argument or that an active source is set."
257 if not view and not active_objects.view:
259 rep = GetDisplayProperties(proxy, view)
261 raise RuntimeError, "Could not create a representation object for proxy %s" % proxy.GetXMLLabel()
262 for param in params.keys():
263 setattr(rep, param, params[param])
267 def Hide(proxy=None, view=None):
268 """Turns the visibility of a given pipeline object off in the given view.
269 If pipeline object and/or view are not specified, active objects are used."""
270 rep = GetDisplayProperties(proxy, view)
273 def Render(view=None):
274 """Renders the given view (default value is active view)"""
276 view = active_objects.view
278 if _funcs_internals.first_render:
279 # Not all views have a ResetCamera method
283 except AttributeError: pass
284 _funcs_internals.first_render = False
287 def ResetCamera(view=None):
288 """Resets the settings of the camera to preserver orientation but include
289 the whole scene. If an argument is not provided, the active view is
292 view = active_objects.view
293 if hasattr(view, "ResetCamera"):
295 if hasattr(view, "ResetDisplay"):
299 def _DisableFirstRenderCameraReset():
300 """Disable the first render camera reset. Normally a ResetCamera is called
301 automatically when Render is called for the first time after importing
303 _funcs_internals.first_render = False
305 def SetProperties(proxy=None, **params):
306 """Sets one or more properties of the given pipeline object. If an argument
307 is not provided, the active source is used. Pass a list of property_name=value
308 pairs to this function to set property values. For example:
309 SetProperties(Center=[1, 2, 3], Radius=3.5)
312 proxy = active_objects.source
313 for param in params.keys():
314 if not hasattr(proxy, param):
315 raise AttributeError("object has no property %s" % param)
316 setattr(proxy, param, params[param])
318 def GetProperty(*arguments, **keywords):
319 """Get one property of the given pipeline object. If keywords are used,
320 you can set the proxy and the name of the property that you want to get
321 like in the following example :
322 GetProperty({proxy=sphere, name="Radius"})
323 If it's arguments that are used, then you have two case:
324 - if only one argument is used that argument will be
326 - if two arguments are used then the first one will be
327 the proxy and the second one the property name.
328 Several example are given below:
329 GetProperty({name="Radius"})
330 GetProperty({proxy=sphereProxy, name="Radius"})
331 GetProperty( sphereProxy, "Radius" )
332 GetProperty( "Radius" )
340 proxy = keywords[key]
341 if len(arguments) == 1 :
343 if len(arguments) == 2 :
347 raise RuntimeError, "Expecting at least a property name as input. Otherwise keyword could be used to set 'proxy' and property 'name'"
349 proxy = active_objects.source
350 return proxy.GetProperty(name)
352 def SetDisplayProperties(proxy=None, view=None, **params):
353 """Sets one or more display properties of the given pipeline object. If an argument
354 is not provided, the active source is used. Pass a list of property_name=value
355 pairs to this function to set property values. For example:
356 SetProperties(Color=[1, 0, 0], LineWidth=2)
358 rep = GetDisplayProperties(proxy, view)
359 SetProperties(rep, **params)
361 def SetViewProperties(view=None, **params):
362 """Sets one or more properties of the given view. If an argument
363 is not provided, the active view is used. Pass a list of property_name=value
364 pairs to this function to set property values. For example:
365 SetProperties(Background=[1, 0, 0], UseImmediateMode=0)
368 view = active_objects.view
369 SetProperties(view, **params)
371 def RenameSource(newName, proxy=None):
372 """Renames the given source. If the given proxy is not registered
373 in the sources group this method will have no effect. If no source is
374 provided, the active source is used."""
376 proxy = active_objects.source
377 pxm = servermanager.ProxyManager()
378 oldName = pxm.GetProxyName("sources", proxy)
380 pxm.RegisterProxy("sources", newName, proxy)
381 pxm.UnRegisterProxy("sources", oldName, proxy)
383 def FindSource(name):
384 return servermanager.ProxyManager().GetProxy("sources", name)
387 """Given the name of a source, return its Python object."""
388 return servermanager.ProxyManager().GetProxiesInGroup("sources")
390 def GetRepresentations():
391 """Returns all representations (display properties)."""
392 return servermanager.ProxyManager().GetProxiesInGroup("representations")
394 def UpdatePipeline(time=None, proxy=None):
395 """Updates (executes) the given pipeline object for the given time as
396 necessary (i.e. if it did not already execute). If no source is provided,
397 the active source is used instead."""
399 proxy = active_objects.source
401 proxy.UpdatePipeline(time)
403 proxy.UpdatePipeline()
405 def Delete(proxy=None):
406 """Deletes the given pipeline object or the active source if no argument
409 proxy = active_objects.source
410 # Unregister any helper proxies stored by a vtkSMProxyListDomain
412 listdomain = prop.GetDomain('proxy_list')
414 if listdomain.GetClassName() != 'vtkSMProxyListDomain':
416 group = "pq_helper_proxies." + proxy.GetGlobalIDAsString()
417 for i in xrange(listdomain.GetNumberOfProxies()):
418 pm = servermanager.ProxyManager()
419 iproxy = listdomain.GetProxy(i)
420 name = pm.GetProxyName(group, iproxy)
422 pm.UnRegisterProxy(group, name, iproxy)
424 # Remove source/view from time keeper
425 tk = servermanager.ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
426 if isinstance(proxy, servermanager.SourceProxy):
428 idx = tk.TimeSources.index(proxy)
429 del tk.TimeSources[idx]
434 idx = tk.Views.index(proxy)
438 servermanager.UnRegister(proxy)
440 # If this is a representation, remove it from all views.
441 if proxy.SMProxy.IsA("vtkSMRepresentationProxy") or \
442 proxy.SMProxy.IsA("vtkSMNewWidgetRepresentationProxy"):
443 for view in GetRenderViews():
444 view.Representations.remove(proxy)
445 # If this is a source, remove the representation iff it has no consumers
446 # Also change the active source if necessary
447 elif proxy.SMProxy.IsA("vtkSMSourceProxy"):
448 sources = servermanager.ProxyManager().GetProxiesInGroup("sources")
449 for i in range(proxy.GetNumberOfConsumers()):
450 if proxy.GetConsumerProxy(i) in sources:
451 raise RuntimeError("Source has consumers. It cannot be deleted " +
452 "until all consumers are deleted.")
454 if proxy.IsSame(GetActiveSource()):
455 if hasattr(proxy, "Input") and proxy.Input:
456 if isinstance(proxy.Input, servermanager.Proxy):
457 SetActiveSource(proxy.Input)
459 SetActiveSource(proxy.Input[0])
460 else: SetActiveSource(None)
461 for rep in GetRepresentations().values():
463 if rep.Input.IsSame(proxy):
465 # Change the active view if necessary
466 elif proxy.SMProxy.IsA("vtkSMRenderViewProxy"):
468 if proxy.IsSame(GetActiveView()):
469 if len(GetRenderViews()) > 0:
470 SetActiveView(GetRenderViews()[0])
474 def CreateLookupTable(**params):
475 """Create and return a lookup table. Optionally, parameters can be given
476 to assign to the lookup table.
478 lt = servermanager.rendering.PVLookupTable()
479 servermanager.Register(lt)
480 SetProperties(lt, **params)
483 def CreatePiecewiseFunction(**params):
484 """Create and return a piecewise function. Optionally, parameters can be
485 given to assign to the piecewise function.
487 pfunc = servermanager.piecewise_functions.PiecewiseFunction()
488 servermanager.Register(pfunc)
489 SetProperties(pfunc, **params)
492 def GetLookupTableForArray(arrayname, num_components, **params):
493 """Used to get an existing lookuptable for a array or to create one if none
494 exists. Keyword arguments can be passed in to initialize the LUT if a new
496 proxyName = "%d.%s.PVLookupTable" % (int(num_components), arrayname)
497 lut = servermanager.ProxyManager().GetProxy("lookup_tables", proxyName)
500 # No LUT exists for this array, create a new one.
501 # TODO: Change this to go a LookupTableManager that is shared with the GUI,
502 # so that the GUI and python end up create same type of LUTs. For now,
503 # python will create a Blue-Red LUT, unless overridden by params.
504 lut = servermanager.rendering.PVLookupTable(
505 ColorSpace="HSV", RGBPoints=[0, 0, 0, 1, 1, 1, 0, 0])
506 SetProperties(lut, **params)
507 servermanager.Register(lut, registrationName=proxyName)
510 def CreateScalarBar(**params):
511 """Create and return a scalar bar widget. The returned widget may
512 be added to a render view by appending it to the view's representations
513 The widget must have a valid lookup table before it is added to a view.
514 It is possible to pass the lookup table (and other properties) as arguments
517 lt = MakeBlueToRedLt(3.5, 7.5)
518 bar = CreateScalarBar(LookupTable=lt, Title="Velocity")
519 GetRenderView().Representations.append(bar)
521 By default the returned widget is selectable and resizable.
523 sb = servermanager.rendering.ScalarBarWidgetRepresentation()
524 servermanager.Register(sb)
529 SetProperties(sb, **params)
532 # TODO: Change this to take the array name and number of components. Register
533 # the lt under the name ncomp.array_name
534 def MakeBlueToRedLT(min, max):
535 # Define RGB points. These are tuples of 4 values. First one is
536 # the scalar values, the other 3 the RGB values.
537 rgbPoints = [min, 0, 0, 1, max, 1, 0, 0]
538 return CreateLookupTable(RGBPoints=rgbPoints, ColorSpace="HSV")
540 def _find_writer(filename):
541 """Internal function."""
543 parts = filename.split('.')
545 extension = parts[-1]
547 raise RuntimeError, "Filename has no extension, please specify a write"
549 if extension == 'png':
550 return 'vtkPNGWriter'
551 elif extension == 'bmp':
552 return 'vtkBMPWriter'
553 elif extension == 'ppm':
554 return 'vtkPNMWriter'
555 elif extension == 'tif' or extension == 'tiff':
556 return 'vtkTIFFWriter'
557 elif extension == 'jpg' or extension == 'jpeg':
558 return 'vtkJPEGWriter'
560 raise RuntimeError, "Cannot infer filetype from extension:", extension
562 def AddCameraLink(viewProxy, viewProxyOther, linkName):
563 """Create a camera link between two view proxies. A name must be given
564 so that the link can be referred to by name. If a link with the given
565 name already exists it will be removed first."""
566 if not viewProxyOther: viewProxyOther = GetActiveView()
567 link = servermanager.vtkSMCameraLink()
568 link.AddLinkedProxy(viewProxy.SMProxy, 1)
569 link.AddLinkedProxy(viewProxyOther.SMProxy, 2)
570 link.AddLinkedProxy(viewProxyOther.SMProxy, 1)
571 link.AddLinkedProxy(viewProxy.SMProxy, 2)
572 RemoveCameraLink(linkName)
573 servermanager.ProxyManager().RegisterLink(linkName, link)
575 def RemoveCameraLink(linkName):
576 """Remove a camera link with the given name."""
577 servermanager.ProxyManager().UnRegisterLink(linkName)
579 def WriteImage(filename, view=None, **params):
580 """Saves the given view (or the active one if none is given) as an
581 image. Optionally, you can specify the writer and the magnification
582 using the Writer and Magnification named arguments. For example:
583 WriteImage("foo.mypng", aview, Writer=vtkPNGWriter, Magnification=2)
584 If no writer is provided, the type is determined from the file extension.
585 Currently supported extensions are png, bmp, ppm, tif, tiff, jpg and jpeg.
586 The writer is a VTK class that is capable of writing images.
587 Magnification is used to determine the size of the written image. The size
588 is obtained by multiplying the size of the view with the magnification.
589 Rendering may be done using tiling to obtain the correct size without
590 resizing the view."""
592 view = active_objects.view
594 if params.has_key('Writer'):
595 writer = params['Writer']
597 if params.has_key('Magnification'):
598 mag = int(params['Magnification'])
600 writer = _find_writer(filename)
601 view.WriteImage(filename, writer, mag)
603 def AnimateReader(reader=None, view=None, filename=None):
604 """This is a utility function that, given a reader and a view
605 animates over all time steps of the reader. If the optional
606 filename is provided, a movie is created (type depends on the
607 extension of the filename."""
609 reader = active_objects.source
611 view = active_objects.view
613 return servermanager.AnimateReader(reader, view, filename)
616 def _create_func(key, module):
617 """Internal function."""
619 def CreateObject(*input, **params):
620 """This function creates a new proxy. For pipeline objects that accept inputs,
621 all non-keyword arguments are assumed to be inputs. All keyword arguments are
622 assumed to be property,value pairs and are passed to the new proxy."""
624 # Instantiate the actual object from the given module.
625 px = module.__dict__[key]()
627 # Make sure non-keyword arguments are valid
629 if inp != None and not isinstance(inp, servermanager.Proxy):
630 if px.GetProperty("Input") != None:
631 raise RuntimeError, "Expecting a proxy as input."
633 raise RuntimeError, "This function does not accept non-keyword arguments."
636 if px.GetProperty("Input") != None:
640 # If no input is specified, try the active pipeline object
641 if px.GetProperty("Input").GetRepeatable() and active_objects.get_selected_sources():
642 px.Input = active_objects.get_selected_sources()
643 elif active_objects.source:
644 px.Input = active_objects.source
647 raise RuntimeError, "This function does not expect an input."
649 registrationName = None
650 for nameParam in ['registrationName', 'guiName']:
651 if nameParam in params:
652 registrationName = params[nameParam]
653 del params[nameParam]
655 # Pass all the named arguments as property,value pairs
656 for param in params.keys():
657 setattr(px, param, params[param])
660 # Register the proxy with the proxy manager.
662 group, name = servermanager.Register(px, registrationName=registrationName)
664 group, name = servermanager.Register(px)
667 # Register pipeline objects with the time keeper. This is used to extract time values
668 # from sources. NOTE: This should really be in the servermanager controller layer.
669 if group == "sources":
672 tk = servermanager.ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
676 sources = tk.TimeSources
677 if not px in sources:
680 active_objects.source = px
681 except servermanager.MissingRegistrationInformation:
688 def _create_doc(new, old):
689 """Internal function."""
692 for doc in (new, old):
694 strpd = doc.split('\n')
696 ts.append(s.lstrip())
697 res += string.join(ts)
701 def _func_name_valid(name):
702 """Internal function."""
705 if c == '(' or c ==')':
710 def _add_functions(g):
711 activeModule = servermanager.ActiveConnection.Modules
712 for m in [activeModule.filters, activeModule.sources,
713 activeModule.writers, activeModule.animation]:
715 for key in dt.keys():
717 if not isinstance(cl, str):
718 if not key in g and _func_name_valid(key):
719 g[key] = _create_func(key, m)
720 exec "g[key].__doc__ = _create_doc(m.%s.__doc__, g[key].__doc__)" % key
722 def _remove_functions(g):
724 if servermanager.ActiveConnection:
725 list = [m for m in dir(servermanager.ActiveConnection.Modules) if m[0] != '_']
728 dt = servermanager.ActiveConnection.Modules.__dict__[m].__dict__
729 for key in dt.keys():
731 if not isinstance(cl, str) and g.has_key(key):
733 #print "remove %s function" % key
736 """Returns the active view."""
737 return active_objects.view
739 def SetActiveView(view):
740 """Sets the active view."""
741 active_objects.view = view
743 def GetActiveSource():
744 """Returns the active source."""
745 return active_objects.source
747 def SetActiveSource(source):
748 """Sets the active source."""
749 active_objects.source = source
751 def GetActiveCamera():
752 """Returns the active camera for the active view. The returned object
753 is an instance of vtkCamera."""
754 return GetActiveView().GetActiveCamera()
756 def GetAnimationScene():
757 """Returns the application-wide animation scene. ParaView has only one
758 global animation scene. This method provides access to that. Users are
759 free to create additional animation scenes directly, but those scenes
760 won't be shown in the ParaView GUI."""
761 animation_proxies = servermanager.ProxyManager().GetProxiesInGroup("animation")
763 for aProxy in animation_proxies.values():
764 if aProxy.GetXMLName() == "AnimationScene":
768 raise servermanager.MissingProxy, "Could not locate global AnimationScene."
771 def WriteAnimation(filename, **params):
772 """Writes the current animation as a file. Optionally one can specify
773 arguments that qualify the saved animation files as keyword arguments.
774 Accepted options are as follows:
775 * Magnification (integer) : set the maginification factor for the saved
777 * Quality (0 [worst] or 1 or 2 [best]) : set the quality of the generated
778 movie (if applicable).
779 * Subsampling (integer) : setting whether the movie encoder should use
780 subsampling of the chrome planes or not, if applicable. Since the human
781 eye is more sensitive to brightness than color variations, subsampling
782 can be useful to reduce the bitrate. Default value is 0.
783 * BackgroundColor (3-tuple of doubles) : set the RGB background color to
784 use to fill empty spaces in the image.
785 * FrameRate (double): set the frame rate (if applicable)."""
786 scene = GetAnimationScene()
787 # ensures that the TimeKeeper track is created.
789 iw = servermanager.vtkSMAnimationSceneImageWriter()
790 iw.SetAnimationScene(scene.SMProxy)
791 iw.SetFileName(filename)
792 if params.has_key("Magnification"):
793 iw.SetMagnification(int(params["Magnification"]))
794 if params.has_key("Quality"):
795 iw.SetQuality(int(params["Quality"]))
796 if params.has_key("Subsampling"):
797 iw.SetSubsampling(int(params["Subsampling"]))
798 if params.has_key("BackgroundColor"):
799 iw.SetBackgroundColor(params["BackgroundColor"])
800 if params.has_key("FrameRate"):
801 iw.SetFrameRate(float(params["FrameRate"]))
804 def _GetRepresentationAnimationHelper(sourceproxy):
805 """Internal method that returns the representation animation helper for a
806 source proxy. It creates a new one if none exists."""
807 # ascertain that proxy is a source proxy
808 if not sourceproxy in GetSources().values():
810 for proxy in servermanager.ProxyManager():
811 if proxy.GetXMLName() == "RepresentationAnimationHelper" and\
812 proxy.GetProperty("Source").IsProxyAdded(sourceproxy.SMProxy):
814 # create a new helper
815 proxy = servermanager.misc.RepresentationAnimationHelper(
817 servermanager.ProxyManager().RegisterProxy(
818 "pq_helper_proxies.%s" % sourceproxy.GetGlobalIDAsString(),
819 "RepresentationAnimationHelper", proxy)
822 def GetAnimationTrack(propertyname_or_property, index=None, proxy=None):
823 """Returns an animation cue for the property. If one doesn't exist then a
824 new one will be created.
826 track = GetAnimationTrack("Center", 0, sphere) or
827 track = GetAnimationTrack(sphere.GetProperty("Radius")) or
829 # this returns the track to animate visibility of the active source in
831 track = GetAnimationTrack("Visibility")
833 For animating properties on implicit planes etc., use the following
835 track = GetAnimationTrack(slice.SliceType.GetProperty("Origin"), 0) or
836 track = GetAnimationTrack("Origin", 0, slice.SliceType)
840 proxy = GetActiveSource()
841 if not isinstance(proxy, servermanager.Proxy):
842 raise TypeError, "proxy must be a servermanager.Proxy instance"
843 if isinstance(propertyname_or_property, str):
844 propertyname = propertyname_or_property
845 elif isinstance(propertyname_or_property, servermanager.Property):
846 prop = propertyname_or_property
847 propertyname = prop.Name
850 raise TypeError, "propertyname_or_property must be a string or servermanager.Property"
852 # To handle the case where the property is actually a "display" property, in
853 # which case we are actually animating the "RepresentationAnimationHelper"
854 # associated with the source.
855 if propertyname in ["Visibility", "Opacity"]:
856 proxy = _GetRepresentationAnimationHelper(proxy)
857 if not proxy or not proxy.GetProperty(propertyname):
858 raise AttributeError, "Failed to locate property %s" % propertyname
860 scene = GetAnimationScene()
861 for cue in scene.Cues:
863 if cue.AnimatedProxy.IsSame(proxy) and\
864 cue.AnimatedPropertyName == propertyname:
865 if index == None or index.IsSame(cue.AnimatedElement): ##index == cue.AnimatedElement:
867 except AttributeError:
870 # matching animation track wasn't found, create a new one.
871 cue = KeyFrameAnimationCue()
872 cue.AnimatedProxy = proxy
873 cue.AnimatedPropertyName = propertyname
875 cue.AnimatedElement = index
876 scene.Cues.append(cue)
879 def GetCameraTrack(view=None):
880 """Returns the camera animation track for the given view. If no view is
881 specified, active view will be used. If no exisiting camera animation track
882 is found, a new one will be created."""
884 view = GetActiveView()
886 raise ValueError, "No view specified"
887 scene = GetAnimationScene()
888 for cue in scene.Cues:
889 if cue.AnimatedProxy.IsSame(view) and\
890 cue.GetXMLName() == "CameraAnimationCue":
892 # no cue was found, create a new one.
893 cue = CameraAnimationCue()
894 cue.AnimatedProxy = view
895 scene.Cues.append(cue)
899 """Returns the animation track used to control the time requested from all
900 readers/filters during playback.
901 This is the "TimeKeeper - Time" track shown in ParaView's 'Animation View'.
902 If none exists, a new one will be created."""
903 scene = GetAnimationScene()
904 tk = scene.TimeKeeper
905 for cue in scene.Cues:
906 if cue.GetXMLName() == "TimeAnimationCue" and cue.AnimatedProxy.IsSame(tk)\
907 and cue.AnimatedPropertyName == "Time":
909 # no cue was found, create a new one.
910 cue = TimeAnimationCue()
911 cue.AnimatedProxy = tk
912 cue.AnimatedPropertyName = "Time"
913 scene.Cues.append(cue)
916 def LoadXML(xmlstring, ns=None):
917 """Given a server manager XML as a string, parse and process it.
918 If you loaded the simple module with from paraview.simple import *,
919 make sure to pass globals() as the second arguments:
920 LoadXML(xmlstring, globals())
921 Otherwise, the new functions will not appear in the global namespace."""
924 servermanager.LoadXML(xmlstring)
927 def LoadPlugin(filename, remote=True, ns=None):
928 """Loads a ParaView plugin and updates this module with new constructors
929 if any. The remote argument (default to True) is to specify whether
930 the plugin will be loaded on client (remote=False) or on server (remote=True).
931 If you loaded the simple module with from paraview.simple import *,
932 make sure to pass globals() as an argument:
933 LoadPlugin("myplugin", False, globals()), to load on client;
934 LoadPlugin("myplugin", True, globals()), to load on server;
935 LoadPlugin("myplugin", ns=globals()), to load on server.
936 Otherwise, the new functions will not appear in the global namespace."""
940 servermanager.LoadPlugin(filename, remote)
943 def LoadDistributedPlugin(pluginname, remote=True, ns=None):
944 """Loads a plugin that's distributed with the executable. This uses the
945 information known about plugins distributed with ParaView to locate the
946 shared library for the plugin to load. Raises a RuntimeError if the plugin
948 if not servermanager.ActiveConnection:
949 raise RuntimeError, "Cannot load a plugin without a session."
950 plm = servermanager.vtkSMProxyManager.GetProxyManager().GetPluginManager()
952 session = servermanager.ActiveConnection.Session
953 info = plm.GetRemoteInformation(session)
955 info = plm.GetLocalInformation()
956 for cc in range(0, info.GetNumberOfPlugins()):
957 if info.GetPluginName(cc) == pluginname:
958 return LoadPlugin(info.GetPluginFileName(cc), remote, ns)
959 raise RuntimeError, "Plugin '%s' not found" % pluginname
962 """Returns the layout proxies on the active session.
963 Layout proxies are used to place views in a grid."""
964 return servermanager.ProxyManager().GetProxiesInGroup("layouts")
966 def GetLayout(view=None):
967 """Return the layout containing the give view, if any.
968 If no view is specified, active view is used.
971 view = GetActiveView()
973 raise RuntimeError, "No active view was found."
974 layouts = GetLayouts()
975 for layout in layouts.values():
976 if layout.GetViewLocation(view) != -1:
981 def SelectCells(query=None, proxy=None):
982 """Select cells satisfying the query. If query is None, then all cells are
983 selected. If proxy is None, then the active source is used."""
985 proxy = GetActiveSource()
987 raise RuntimeError, "No active source was found."
990 # This ends up being true for all cells.
993 # Note, selSource is not registered with the proxy manager.
994 selSource = servermanager.sources.SelectionQuerySource()
995 selSource.FieldType = "CELL"
996 selSource.QueryString = str(query)
997 proxy.SMProxy.SetSelectionInput(proxy.Port, selSource.SMProxy, 0)
1000 def ClearSelection(proxy=None):
1001 """Clears the selection on the active source."""
1003 proxy = GetActiveSource()
1005 raise RuntimeError, "No active source was found."
1006 proxy.SMProxy.SetSelectionInput(proxy.Port, None, 0)
1008 class ActiveObjects(object):
1009 """This class manages the active objects (source and view). The active
1010 objects are shared between Python and the user interface. This class
1011 is for internal use. Use the Set/Get methods for setting and getting
1013 def __get_selection_model(self, name, session=None):
1015 if session and not servermanager.ActiveConnection.Session.IsSame(session):
1016 raise RuntimeError, "Try to set an active object with invalid active connection."
1017 pxm = servermanager.ProxyManager(session)
1018 model = pxm.GetSelectionModel(name)
1020 model = servermanager.vtkSMProxySelectionModel()
1021 pxm.RegisterSelectionModel(name, model)
1024 def set_view(self, view):
1025 "Sets the active view."
1026 active_view_model = self.__get_selection_model("ActiveView")
1028 active_view_model = self.__get_selection_model("ActiveView", view.GetSession())
1029 active_view_model.SetCurrentProxy(view.SMProxy, 0)
1031 active_view_model = self.__get_selection_model("ActiveView")
1032 active_view_model.SetCurrentProxy(None, 0)
1035 "Returns the active view."
1036 return servermanager._getPyProxy(
1037 self.__get_selection_model("ActiveView").GetCurrentProxy())
1039 def set_source(self, source):
1040 "Sets the active source."
1041 active_sources_model = self.__get_selection_model("ActiveSources")
1043 # 3 == CLEAR_AND_SELECT
1044 active_sources_model = self.__get_selection_model("ActiveSources", source.GetSession())
1045 active_sources_model.SetCurrentProxy(source.SMProxy, 3)
1047 active_sources_model = self.__get_selection_model("ActiveSources")
1048 active_sources_model.SetCurrentProxy(None, 3)
1050 def __convert_proxy(self, px):
1054 if px.IsA("vtkSMSourceProxy"):
1055 return servermanager._getPyProxy(px)
1057 return servermanager.OutputPort(
1058 servermanager._getPyProxy(px.GetSourceProxy()),
1061 def get_source(self):
1062 "Returns the active source."
1063 return self.__convert_proxy(
1064 self.__get_selection_model("ActiveSources").GetCurrentProxy())
1066 def get_selected_sources(self):
1067 "Returns the set of sources selected in the pipeline browser."
1068 model = self.__get_selection_model("ActiveSources")
1070 for i in xrange(model.GetNumberOfSelectedProxies()):
1071 proxies.append(self.__convert_proxy(model.GetSelectedProxy(i)))
1074 view = property(get_view, set_view)
1075 source = property(get_source, set_source)
1077 class _funcs_internals:
1078 """Internal class."""
1084 """Simple demo that create the following pipeline
1089 # Create a sphere of radius = 2, theta res. = 32
1090 # This object becomes the active source.
1091 ss = Sphere(Radius=2, ThetaResolution=32)
1092 # Apply the shrink filter. The Input property is optional. If Input
1093 # is not specified, the filter is applied to the active source.
1094 shr = Shrink(Input=ss)
1095 # Create a cone source.
1097 # Append cone and shrink
1098 app = AppendDatasets()
1099 app.Input = [shr, cs]
1100 # Show the output of the append filter. The argument is optional
1101 # as the app filter is now the active object.
1103 # Render the default view.
1106 def demo2(fname="/Users/berk/Work/ParaView/ParaViewData/Data/disk_out_ref.ex2"):
1107 """This demo shows the use of readers, data information and display
1110 # Create the exodus reader and specify a file name
1111 reader = ExodusIIReader(FileName=fname)
1112 # Get the list of point arrays.
1113 avail = reader.PointVariables.Available
1116 reader.PointVariables = avail
1118 # Turn on the visibility of the reader
1120 # Set representation to wireframe
1121 SetDisplayProperties(Representation = "Wireframe")
1122 # Black background is not pretty
1123 SetViewProperties(Background = [0.4, 0.4, 0.6])
1125 # Change the elevation of the camera. See VTK documentation of vtkCamera
1126 # for camera parameters.
1127 # NOTE: THIS WILL BE SIMPLER
1128 GetActiveCamera().Elevation(45)
1130 # Now that the reader executed, let's get some information about it's
1132 pdi = reader[0].PointData
1133 # This prints a list of all read point data arrays as well as their
1135 print 'Number of point arrays:', len(pdi)
1136 for i in range(len(pdi)):
1138 print "----------------"
1139 print "Array:", i, " ", ai.Name, ":"
1140 numComps = ai.GetNumberOfComponents()
1141 print "Number of components:", numComps
1142 for j in range(numComps):
1143 print "Range:", ai.GetRange(j)
1144 # White is boring. Let's color the geometry using a variable.
1145 # First create a lookup table. This object controls how scalar
1146 # values are mapped to colors. See VTK documentation for
1148 # Map min (0.00678) to blue, max (0.0288) to red
1149 SetDisplayProperties(LookupTable = MakeBlueToRedLT(0.00678, 0.0288))
1150 # Color by point array called Pres
1151 SetDisplayProperties(ColorAttributeType = "POINT_DATA")
1152 SetDisplayProperties(ColorArrayName = "Pres")
1156 print paravisSM.myParavis.GetTrace()
1158 def SaveTrace(fileName):
1159 paravisSM.myParavis.SaveTrace(fileName)
1162 if not servermanager.ActiveConnection:
1165 _add_functions(globals())
1167 def ImportFile(theFileName):
1168 paravisSM.ImportFile(theFileName)
1170 active_objects = ActiveObjects()
1171 active_objects.view = GetRenderView()