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"""servermanager is a module for using paraview server manager in Python.
21 One can always use the server manager API directly. However, this module
22 provides an interface easier to use from Python by wrapping several VTK
23 classes around Python classes.
25 Note that, upon load, this module will create several sub-modules: sources,
26 filters and rendering. These modules can be used to instantiate specific
27 proxy types. For a list, try "dir(servermanager.sources)"
30 from paraview.servermanager import *
32 # Creates a new built-in session and makes it the active session.
35 # Creates a new render view on the active session.
36 renModule = CreateRenderView()
38 # Create a new sphere proxy on the active session and register it
39 # in the sources group.
40 sphere = sources.SphereSource(registrationGroup="sources", ThetaResolution=16, PhiResolution=32)
42 # Create a representation for the sphere proxy and adds it to the render
44 display = CreateRepresentation(sphere, renModule)
46 renModule.StillRender()
51 import re, os, new, sys
56 def _wrap_property(proxy, smproperty):
57 """ Internal function.
58 Given a server manager property and its domains, returns the
59 appropriate python object.
62 if smproperty.IsA("vtkSMStringVectorProperty"):
63 al = smproperty.GetDomain("array_list")
64 if al and al.IsA("vtkSMArraySelectionDomain") and \
65 smproperty.GetRepeatable():
66 property = ArrayListProperty(proxy, smproperty)
67 elif al and al.IsA("vtkSMArrayListDomain") and smproperty.GetNumberOfElements() == 5:
68 property = ArraySelectionProperty(proxy, smproperty)
70 iter = smproperty.NewDomainIterator()
72 while not iter.IsAtEnd():
73 # Refer to BUG #9710 to see why optional domains need to be
75 if iter.GetDomain().IsA("vtkSMFileListDomain") and \
76 iter.GetDomain().GetIsOptional() == 0 :
82 property = FileNameProperty(proxy, smproperty)
83 elif _make_name_valid(smproperty.GetXMLLabel()) == 'ColorArrayName':
84 property = ColorArrayProperty(proxy, smproperty)
86 property = VectorProperty(proxy, smproperty)
87 elif smproperty.IsA("vtkSMVectorProperty"):
88 if smproperty.IsA("vtkSMIntVectorProperty") and \
89 smproperty.GetDomain("enum"):
90 property = EnumerationProperty(proxy, smproperty)
92 property = VectorProperty(proxy, smproperty)
93 elif smproperty.IsA("vtkSMInputProperty"):
94 property = InputProperty(proxy, smproperty)
95 elif smproperty.IsA("vtkSMProxyProperty"):
96 property = ProxyProperty(proxy, smproperty)
98 property = Property(proxy, smproperty)
102 """Proxy for a server side object. A proxy manages the lifetime of
103 one or more server manager objects. It also provides an interface
104 to set and get the properties of the server side objects. These
105 properties are presented as Python properties. For example,
106 you can set a property Foo using the following:
109 proxy.Foo.SetData((1,2))
111 proxy.Foo[0:2] = (1,2)
112 For more information, see the documentation of the property which
116 This class also provides an iterator which can be used to iterate
119 proxy = Proxy(proxy=smproxy)
120 for property in proxy:
124 This is a python class that wraps a vtkSMProxy.. Makes it easier to
127 proxy.GetProperty("Foo").SetElement(0, 1)
128 proxy.GetProperty("Foo").SetElement(0, 2)
132 proxy.Foo.SetData((1,2))
134 proxy.Foo[0:2] = (1,2)
136 proxy.GetProperty("Foo").GetElement(0)
138 proxy.Foo.GetData()[0]
141 For proxy properties, you can use append:
142 proxy.GetProperty("Bar").AddProxy(foo)
144 proxy.Bar.append(foo)
145 Properties support most of the list API. See VectorProperty and
146 ProxyProperty documentation for details.
148 Please note that some of the methods accessible through the Proxy
149 class are not listed by help() because the Proxy objects forward
150 unresolved attributes to the underlying object. To get the full list,
151 see also dir(proxy.SMProxy). See also the doxygen based documentation
152 of the vtkSMProxy C++ class.
155 def __init__(self, **args):
156 """ Default constructor. It can be used to initialize properties
157 by passing keyword arguments where the key is the name of the
158 property. In addition registrationGroup and registrationName (optional)
159 can be specified (as keyword arguments) to automatically register
160 the proxy with the proxy manager. """
161 self.add_attribute('Observed', None)
162 self.add_attribute('ObserverTag', -1)
163 self.add_attribute('_Proxy__Properties', {})
164 self.add_attribute('_Proxy__LastAttrName', None)
165 self.add_attribute('SMProxy', None)
166 self.add_attribute('Port', 0)
169 self.Port = args['port']
173 if 'no_update' in args:
174 if args['no_update']:
176 del args['no_update']
179 self.InitializeFromProxy(args['proxy'])
182 self.Initialize(None, update)
183 if 'registrationGroup' in args:
184 registrationGroup = args['registrationGroup']
185 del args['registrationGroup']
186 registrationName = self.SMProxy.GetGlobalIDAsString()
187 if 'registrationName' in args:
188 registrationName = args['registrationName']
189 del args['registrationName']
191 pxm.RegisterProxy(registrationGroup, registrationName, self.SMProxy)
193 self.UpdateVTKObjects()
194 for key in args.keys():
195 setattr(self, key, args[key])
196 # Visit all properties so that they are created
200 def __setattr__(self, name, value):
202 setter = getattr(self.__class__, name)
203 setter = setter.__set__
204 except AttributeError:
205 if not hasattr(self, name):
206 raise AttributeError("Attribute %s does not exist. " % name +
207 " This class does not allow addition of new attributes to avoid " +
208 "mistakes due to typos. Use add_attribute() if you really want " +
209 "to add this attribute.")
210 self.__dict__[name] = value
214 def add_attribute(self, name, value):
215 self.__dict__[name] = value
218 """Destructor. Cleans up all observers as well as remove
219 the proxy from the _pyproxies dictionary"""
220 # Make sure that we remove observers we added
222 observed = self.Observed
223 tag = self.ObserverTag
225 self.ObserverTag = -1
226 observed.RemoveObserver(tag)
227 if self.SMProxy and (self.SMProxy, self.Port) in _pyproxies:
228 del _pyproxies[(self.SMProxy, self.Port)]
230 def InitializeFromProxy(self, aProxy, update=True):
231 """Constructor. Assigns proxy to self.SMProxy, updates the server
232 object as well as register the proxy in _pyproxies dictionary."""
234 self.SMProxy = aProxy
236 self.SMProxy.UpdateVTKObjects()
237 _pyproxies[(self.SMProxy, self.Port)] = weakref.ref(self)
239 def Initialize(self):
240 "Overridden by the subclass created automatically"
243 def __eq__(self, other):
244 "Returns true if the underlying SMProxies are the same."
245 if isinstance(other, Proxy):
247 if self.Port != other.Port:
251 ## VSV using IsSame instead ==
252 return self.SMProxy.IsSame(other.SMProxy)
253 return self.SMProxy.IsSame(other)
255 def __ne__(self, other):
256 "Returns false if the underlying SMProxies are the same."
257 return not self.__eq__(other)
260 "Creates an iterator for the properties."
261 return PropertyIterator(self)
263 def SetPropertyWithName(self, pname, arg):
264 """Generic method for setting the value of a property."""
265 prop = self.GetProperty(pname)
267 raise RuntimeError, "Property %s does not exist. Please check the property name for typos." % pname
270 def GetPropertyValue(self, name):
271 """Returns a scalar for properties with 1 elements, the property
272 itself for vectors."""
273 p = self.GetProperty(name)
274 if isinstance(p, VectorProperty):
275 if p.GetNumberOfElements() == 1 and not p.GetRepeatable():
276 if p.SMProperty.IsA("vtkSMStringVectorProperty") or not p.GetArgumentIsArray():
278 elif isinstance(p, InputProperty):
279 if not p.GetMultipleInput():
284 elif isinstance(p, ProxyProperty):
285 if not p.GetRepeatable():
292 def GetProperty(self, name):
293 """Given a property name, returns the property object."""
294 if name in self.__Properties and self.__Properties[name]():
295 return self.__Properties[name]()
296 smproperty = self.SMProxy.GetProperty(name)
297 # Maybe they are looking by the label. Try to match that.
299 iter = PropertyIterator(self)
301 if name == _make_name_valid(iter.PropertyLabel):
302 smproperty = prop.SMProperty
305 property = _wrap_property(self, smproperty)
306 if property is not None:
308 self.__Properties[name] = weakref.ref(property)
312 def ListProperties(self):
313 """Returns a list of all property names on this proxy."""
315 iter = self.__iter__()
316 for property in iter:
317 name = _make_name_valid(iter.PropertyLabel)
319 property_list.append(name)
322 def __ConvertArgumentsAndCall(self, *args):
323 """ Internal function.
324 Used to call a function on SMProxy. Converts input and
325 output values as appropriate.
329 if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
330 newArgs.append(arg.SMProxy)
333 func = getattr(self.SMProxy, self.__LastAttrName)
334 retVal = func(*newArgs)
335 if type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProxy"):
336 return _getPyProxy(retVal)
337 elif type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProperty"):
338 return _wrap_property(self, retVal)
342 def __GetActiveCamera(self):
343 """ This method handles GetActiveCamera specially. It adds
344 an observer to the camera such that everytime it is modified
345 the render view updated"""
347 c = self.SMProxy.GetActiveCamera()
348 # VSV: Observers are not supported
349 ## if not c.HasObserver("ModifiedEvent"):
350 ## self.ObserverTag =c.AddObserver("ModifiedEvent", _makeUpdateCameraMethod(weakref.ref(self)))
354 def __getattr__(self, name):
355 """With the exception of a few overloaded methods,
356 returns the SMProxy method"""
358 raise AttributeError("class %s has no attribute %s" % ("None", name))
360 # Handle GetActiveCamera specially.
361 if name == "GetActiveCamera" and \
362 hasattr(self.SMProxy, "GetActiveCamera"):
363 return self.__GetActiveCamera
364 if name == "SaveDefinition" and hasattr(self.SMProxy, "SaveDefinition"):
365 return self.__SaveDefinition
366 # If not a property, see if SMProxy has the method
368 proxyAttr = getattr(self.SMProxy, name)
369 self.__LastAttrName = name
370 return self.__ConvertArgumentsAndCall
373 return getattr(self.SMProxy, name)
375 class SourceProxy(Proxy):
376 """Proxy for a source object. This class adds a few methods to Proxy
377 that are specific to sources. It also provides access to the output
378 ports. Output ports can be accessed by name or index:
381 > op = source['some name'].
383 def UpdatePipeline(self, time=None):
384 """This method updates the server-side VTK pipeline and the associated
385 data information. Make sure to update a source to validate the output
388 self.SMProxy.UpdatePipeline(time)
390 self.SMProxy.UpdatePipeline()
391 # This is here to cause a receive
392 # on the client side so that progress works properly.
393 if ActiveConnection and ActiveConnection.IsRemote():
394 self.SMProxy.GetDataInformation()
396 def FileNameChanged(self):
397 "Called when the filename of a source proxy is changed."
398 self.UpdatePipelineInformation()
400 def UpdatePipelineInformation(self):
401 """This method updates the meta-data of the server-side VTK pipeline and
402 the associated information properties"""
403 self.SMProxy.UpdatePipelineInformation()
405 def GetDataInformation(self, idx=None):
406 """This method returns a DataInformation wrapper around a
407 vtkPVDataInformation"""
411 return DataInformation( \
412 self.SMProxy.GetDataInformation(idx), \
415 def __getitem__(self, idx):
416 """Given a slice, int or string, returns the corresponding
418 if isinstance(idx, slice):
419 indices = idx.indices(self.SMProxy.GetNumberOfOutputPorts())
421 for i in range(*indices):
422 retVal.append(OutputPort(self, i))
424 elif isinstance(idx, int):
425 if idx >= self.SMProxy.GetNumberOfOutputPorts() or idx < 0:
427 return OutputPort(self, idx)
429 return OutputPort(self, self.SMProxy.GetOutputPortIndex(idx))
431 def GetPointDataInformation(self):
432 """Returns the associated point data information."""
433 self.UpdatePipeline()
434 return FieldDataInformation(self.SMProxy, self.Port, "PointData")
436 def GetCellDataInformation(self):
437 """Returns the associated cell data information."""
438 self.UpdatePipeline()
439 return FieldDataInformation(self.SMProxy, self.Port, "CellData")
441 def GetFieldDataInformation(self):
442 """Returns the associated cell data information."""
443 self.UpdatePipeline()
444 return FieldDataInformation(self.SMProxy, self.Port, "FieldData")
446 PointData = property(GetPointDataInformation, None, None, "Returns point data information")
447 CellData = property(GetCellDataInformation, None, None, "Returns cell data information")
448 FieldData = property(GetFieldDataInformation, None, None, "Returns field data information")
451 class ExodusIIReaderProxy(SourceProxy):
452 """Special class to define convenience functions for array
455 def FileNameChanged(self):
456 "Called when the filename changes. Selects all variables."
457 SourceProxy.FileNameChanged(self)
458 self.SelectAllVariables()
460 def SelectAllVariables(self):
461 "Select all available variables for reading."
462 for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
463 'ElementVariables', 'GlobalVariables'):
464 f = getattr(self, prop)
467 def DeselectAllVariables(self):
468 "Deselects all variables."
469 for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
470 'ElementVariables', 'GlobalVariables'):
471 f = getattr(self, prop)
474 class ViewLayoutProxy(Proxy):
475 """Special class to define convenience methods for View Layout"""
477 def SplitViewHorizontal(self, view, fraction=0.5):
478 """Split the cell containing the specified view horizontally.
479 If no fraction is specified, the frame is split into equal parts.
480 On success returns a positve number that identifying the new cell
481 location that can be used to assign view to, or split further.
482 Return -1 on failure."""
483 location = self.GetViewLocation(view)
485 raise RuntimeError, "View is not present in this layout."
486 if fraction < 0.0 or fraction > 1.0:
487 raise RuntimeError, "'fraction' must be in the range [0.0, 1.0]"
488 return self.SMProxy.SplitHorizontal(location, fraction)
490 def SplitViewVertical(self, view=None, fraction=0.5):
491 """Split the cell containing the specified view horizontally.
492 If no view is specified, active view is used.
493 If no fraction is specified, the frame is split into equal parts.
494 On success returns a positve number that identifying the new cell
495 location that can be used to assign view to, or split further.
496 Return -1 on failure."""
497 location = self.GetViewLocation(view)
499 raise RuntimeError, "View is not present in this layout."
500 if fraction < 0.0 or fraction > 1.0:
501 raise RuntimeError, "'fraction' must be in the range [0.0, 1.0]"
502 return self.SMProxy.SplitVertical(location, fraction)
504 def AssignView(self, location, view):
505 """Assign a view at a particular location. Note that the view's position may
506 be changed by subsequent Split() calls. Returns true on success."""
508 if isinstance(view, Proxy):
510 return self.SMProxy.AssignView(location, view)
512 def GetViewLocation(self, view):
513 if isinstance(view, Proxy):
515 return self.SMProxy.GetViewLocation(view)
517 class Property(object):
518 """Generic property object that provides access to one of the properties of
519 a server object. This class does not allow setting/getting any values but
520 provides an interface to update a property using __call__. This can be used
521 for command properties that correspond to function calls without arguments.
524 would push a Foo property which may cause the proxy to call a Foo method
525 on the actual VTK object.
528 Python wrapper around a vtkSMProperty with a simple interface.
529 In addition to all method provided by vtkSMProperty (obtained by
530 forwarding unknown attributes requests to the underlying SMProxy),
531 Property and sub-class provide a list API.
533 Please note that some of the methods accessible through the Property
534 class are not listed by help() because the Property objects forward
535 unresolved attributes to the underlying object. To get the full list,
536 see also dir(proxy.SMProperty). See also the doxygen based documentation
537 of the vtkSMProperty C++ class.
539 def __init__(self, proxy, smproperty):
540 """Default constructor. Stores a reference to the proxy."""
542 self.SMProperty = smproperty
546 """Returns a string representation containing property name
548 if not type(self) is Property:
549 if self.GetData() is not None:
550 repr = self.GetData().__repr__()
554 repr = "Property name= "
555 name = self.Proxy.GetPropertyName(self.SMProperty)
564 """Forces a property update using InvokeCommand."""
565 if type(self) is Property:
566 self.Proxy.SMProxy.InvokeCommand(self._FindPropertyName())
568 raise RuntimeError, "Cannot invoke this property"
570 def _FindPropertyName(self):
571 "Returns the name of this property."
572 return self.Proxy.GetPropertyName(self.SMProperty)
574 def _UpdateProperty(self):
575 "Pushes the value of this property to the server."
576 # For now, we are updating all properties. This is due to an
577 # issue with the representations. Their VTK objects are not
578 # created until Input is set therefore, updating a property
579 # has no effect. Updating all properties everytime one is
580 # updated has the effect of pushing values set before Input
581 # when Input is updated.
582 # self.Proxy.SMProxy.UpdateProperty(self._FindPropertyName())
583 self.Proxy.SMProxy.UpdateVTKObjects()
585 def __getattr__(self, name):
586 "Unknown attribute requests get forwarded to SMProperty."
587 return getattr(self.SMProperty, name)
589 Name = property(_FindPropertyName, None, None,
590 "Returns the name for the property")
592 class GenericIterator(object):
593 """Iterator for container type objects"""
595 def __init__(self, obj):
603 if self.index >= len(self.Object):
608 return self.Object[idx]
610 class VectorProperty(Property):
611 """A VectorProperty provides access to one or more values. You can use
612 a slice to get one or more property values:
615 > vals = property[0:5:2]
616 You can use a slice to set one or more property values:
619 > property[1:3] = (1,2)
621 def ConvertValue(self, value):
625 """Returns the number of elements."""
626 return self.SMProperty.GetNumberOfElements()
629 """Implementation of the sequence API"""
630 return GenericIterator(self)
632 def __setitem__(self, idx, value):
633 """Given a list or tuple of values, sets a slice of values [min, max)"""
634 if isinstance(idx, slice):
635 indices = idx.indices(len(self))
636 for i, j in zip(range(*indices), value):
637 self.SMProperty.SetElement(i, self.ConvertValue(j))
638 self._UpdateProperty()
639 elif idx >= len(self) or idx < 0:
642 self.SMProperty.SetElement(idx, self.ConvertValue(value))
643 self._UpdateProperty()
645 def GetElement(self, index):
646 return self.SMProperty.GetElement(index)
648 def __getitem__(self, idx):
649 """Returns the range [min, max) of elements. Raises an IndexError
650 exception if an argument is out of bounds."""
652 if isinstance(idx, slice):
653 indices = idx.indices(ls)
655 for i in range(*indices):
656 retVal.append(self.GetElement(i))
665 return self.GetElement(idx)
668 "Returns all elements as either a list or a single value."
669 property = self.SMProperty
670 if property.GetRepeatable() or \
671 property.GetNumberOfElements() > 1:
672 return self[0:len(self)]
673 elif property.GetNumberOfElements() == 1:
674 return self.GetElement(0)
676 def SetData(self, values):
677 """Allows setting of all values at once. Requires a single value or
678 a iterable object."""
679 if not hasattr(values, "__iter__"):
681 if not self.GetRepeatable() and len(values) != self.GetNumberOfElements():
682 raise RuntimeError("This property requires %d values." % self.GetNumberOfElements())
683 if self.GetRepeatable():
685 self.SMProperty.SetNumberOfElements(0)
688 self.SMProperty.SetElement(idx, self.ConvertValue(val))
690 self._UpdateProperty()
693 "Removes all elements."
694 self.SMProperty().SetNumberOfElements(0)
695 self._UpdateProperty()
697 class ColorArrayProperty(VectorProperty):
698 """This subclass of VectorProperty handles setting of the array to
699 color by. It handles attribute type as well as well array name."""
701 def GetAvailable(self):
702 """Returns the list of available arrays as (attribute type, array name
705 for a in self.Proxy.Input.PointData:
706 arrays.append(('POINT_DATA', a.GetName()))
707 for a in self.Proxy.Input.CellData:
708 arrays.append(('CELL_DATA', a.GetName()))
711 def SetData(self, value):
712 """Overwritten to enable setting attribute type (the ColorAttributeType
713 property and the array name. The argument should be the array name
714 (in which case the first appropriate attribute type is picked) or
715 a tuple of attribute type and array name."""
716 if isinstance(value, tuple) and len(value) == 2:
719 elif isinstance(value, str):
723 raise ValueError("Expected a tuple of 2 values or a string.")
726 self.SMProperty.SetElement(0, '')
727 self._UpdateProperty()
731 for a in self.Available:
732 if a[1] == arr and (not att or att == a[0]):
738 pvoptions = vtkProcessModule.GetProcessModule().GetOptions()
739 # if this process is from a parallel batch run in symmetric mpi mode
740 # then we may not have any points or cells on some processes in which
741 # case we'll probably be missing the point and cell data too. the
742 # check below makes sure that we avoid this situation.
743 if pvoptions.GetProcessType() != 0x40 or pvoptions.GetSymmetricMPIMode() == False \
744 or len(self.Available) != 0:
745 raise ValueError("Could not locate array %s in the input." % arr)
747 catt = self.Proxy.GetProperty("ColorAttributeType")
750 self.SMProperty.SetElement(0, arr)
751 self._UpdateProperty()
753 Available = property(GetAvailable, None, None, \
754 "This read-only property returns the list of arrays that can be colored by.")
757 class EnumerationProperty(VectorProperty):
758 """Subclass of VectorProperty that is applicable for enumeration type
761 def GetElement(self, index):
762 """Returns the text for the given element if available. Returns
763 the numerical values otherwise."""
764 val = self.SMProperty.GetElement(index)
765 domain = self.SMProperty.GetDomain("enum")
766 for i in range(domain.GetNumberOfEntries()):
767 if domain.GetEntryValue(i) == val:
768 return domain.GetEntryText(i)
771 def ConvertValue(self, value):
772 """Converts value to type suitable for vtSMProperty::SetElement()"""
773 if type(value) == str:
774 domain = self.SMProperty.GetDomain("enum")
775 if domain.HasEntryText(value):
776 return domain.GetEntryValueForText(value)
778 raise ValueError("%s is not a valid value." % value)
779 return VectorProperty.ConvertValue(self, value)
781 def GetAvailable(self):
782 "Returns the list of available values for the property."
784 domain = self.SMProperty.GetDomain("enum")
785 for i in range(domain.GetNumberOfEntries()):
786 retVal.append(domain.GetEntryText(i))
789 Available = property(GetAvailable, None, None, \
790 "This read-only property contains the list of values that can be applied to this property.")
793 class FileNameProperty(VectorProperty):
794 """Property to set/get one or more file names.
795 This property updates the pipeline information everytime its value changes.
796 This is used to keep the array lists up to date."""
798 def _UpdateProperty(self):
799 "Pushes the value of this property to the server."
800 VectorProperty._UpdateProperty(self)
801 self.Proxy.FileNameChanged()
803 class ArraySelectionProperty(VectorProperty):
804 "Property to select an array to be processed by a filter."
806 def GetAssociation(self):
807 val = self.GetElement(3)
810 for key, value in ASSOCIATIONS.iteritems():
811 if value == int(val):
816 def GetArrayName(self):
817 return self.GetElement(4)
820 """Returns the number of elements."""
823 def __setitem__(self, idx, value):
824 raise RuntimeError, "This property cannot be accessed using __setitem__"
826 def __getitem__(self, idx):
827 """Returns attribute type for index 0, array name for index 1"""
828 if isinstance(idx, slice):
829 indices = idx.indices(len(self))
831 for i in range(*indices):
835 retVal.append(self.GetAssociation())
837 retVal.append(self.GetArrayName())
839 elif idx >= 2 or idx < 0:
843 return self.GetAssociation()
845 return self.GetArrayName()
847 def SetData(self, values):
848 """Allows setting of all values at once. Requires a single value,
850 if not isinstance(values, tuple) and \
851 not isinstance(values, list):
854 self.SMProperty.SetElement(4, values[0])
855 elif len(values) == 2:
856 if isinstance(values[0], str):
857 val = str(ASSOCIATIONS[values[0]])
859 # In case user didn't specify valid association,
861 val = str(ASSOCIATIONS['POINTS'])
862 self.SMProperty.SetElement(3, str(val))
863 self.SMProperty.SetElement(4, values[1])
865 raise RuntimeError, "Expected 1 or 2 values."
866 self._UpdateProperty()
868 def UpdateDefault(self):
869 "Helper method to set default values."
870 if self.SMProperty.GetNumberOfElements() != 5:
872 if self.GetElement(4) != '' or \
873 self.GetElement(3) != '':
877 if self.GetElement(i) == '':
878 self.SMProperty.SetElement(i, '0')
879 al = self.SMProperty.GetDomain("array_list")
880 al.Update(self.SMProperty)
881 al.SetDefaultValues(self.SMProperty)
883 class ArrayListProperty(VectorProperty):
884 """This property provides a simpler interface for selecting arrays.
885 Simply assign a list of arrays that should be loaded by the reader.
886 Use the Available property to get a list of available arrays."""
888 def __init__(self, proxy, smproperty):
889 VectorProperty.__init__(self, proxy, smproperty)
892 def GetAvailable(self):
893 "Returns the list of available arrays"
894 dm = self.GetDomain("array_list")
896 for i in range(dm.GetNumberOfStrings()):
897 retVal.append(dm.GetString(i))
900 Available = property(GetAvailable, None, None, \
901 "This read-only property contains the list of items that can be read by a reader.")
904 "Selects all arrays."
905 self.SetData(self.Available)
907 def DeselectAll(self):
908 "Deselects all arrays."
912 """Implementation of the sequence API"""
913 return GenericIterator(self)
916 """Returns the number of elements."""
917 return len(self.GetData())
919 def __setitem__(self, idx, value):
920 """Given a list or tuple of values, sets a slice of values [min, max)"""
922 if isinstance(idx, slice):
923 indices = idx.indices(len(self))
924 for i, j in zip(range(*indices), value):
926 self.SetData(self.__arrays)
927 elif idx >= len(self) or idx < 0:
930 self.__arrays[idx] = self.ConvertValue(value)
931 self.SetData(self.__arrays)
933 def __getitem__(self, idx):
934 """Returns the range [min, max) of elements. Raises an IndexError
935 exception if an argument is out of bounds."""
937 if isinstance(idx, slice):
938 indices = idx.indices(len(self))
940 for i in range(*indices):
941 retVal.append(self.__arrays[i])
943 elif idx >= len(self) or idx < 0:
945 return self.__arrays[idx]
947 def SetData(self, values):
948 """Allows setting of all values at once. Requires a single value,
951 iup = self.SMProperty.GetImmediateUpdate()
952 self.SMProperty.SetImmediateUpdate(False)
954 self.SMProperty.SetNumberOfElements(0)
955 if not isinstance(values, tuple) and \
956 not isinstance(values, list):
959 for i in range(len(values)):
960 val = self.ConvertValue(values[i])
961 fullvalues.append(val)
962 fullvalues.append('1')
963 for array in self.Available:
964 if not values.__contains__(array):
965 fullvalues.append(array)
966 fullvalues.append('0')
968 for value in fullvalues:
969 self.SMProperty.SetElement(i, value)
972 self._UpdateProperty()
973 self.SMProperty.SetImmediateUpdate(iup)
976 "Returns all elements as a list."
977 property = self.SMProperty
978 nElems = property.GetNumberOfElements()
980 raise ValueError, "The SMProperty with XML label '%s' has a size that is not a multiple of 2." % property.GetXMLLabel()
982 for i in range(0, nElems, 2):
983 if self.GetElement(i+1) != '0':
984 self.__arrays.append(self.GetElement(i))
985 return list(self.__arrays)
987 class ProxyProperty(Property):
988 """A ProxyProperty provides access to one or more proxies. You can use
989 a slice to get one or more property values:
990 > proxy = property[2]
992 > proxies = property[0:5:2]
993 You can use a slice to set one or more property values:
994 > property[2] = proxy
996 > property[1:3] = (proxy1, proxy2)
997 You can also append and delete:
998 > property.append(proxy)
1002 You can also remove all elements with Clear().
1004 Note that some properties expect only 1 proxy and will complain if
1005 you set the number of values to be something else.
1007 def __init__(self, proxy, smproperty):
1008 """Default constructor. Stores a reference to the proxy. Also looks
1009 at domains to find valid values."""
1010 Property.__init__(self, proxy, smproperty)
1011 # Check to see if there is a proxy list domain and, if so,
1012 # initialize ourself. (Should this go in ProxyProperty?)
1013 listdomain = self.GetDomain('proxy_list')
1015 if listdomain.GetClassName() != 'vtkSMProxyListDomain':
1016 raise ValueError, "Found a 'proxy_list' domain on an InputProperty that is not a ProxyListDomain."
1018 group = "pq_helper_proxies." + proxy.GetGlobalIDAsString()
1019 if listdomain.GetNumberOfProxies() == 0:
1020 for i in xrange(listdomain.GetNumberOfProxyTypes()):
1021 igroup = listdomain.GetProxyGroup(i)
1022 name = listdomain.GetProxyName(i)
1023 iproxy = CreateProxy(igroup, name)
1024 listdomain.AddProxy(iproxy)
1025 pm.RegisterProxy(group, proxy.GetPropertyName(smproperty), iproxy)
1026 listdomain.SetDefaultValues(self.SMProperty)
1028 def GetAvailable(self):
1029 """If this proxy has a list domain, then this function returns the
1030 strings you can use to select from the domain. If there is no such
1031 list domain, the returned list is empty."""
1032 listdomain = self.GetDomain('proxy_list')
1035 for i in xrange(listdomain.GetNumberOfProxies()):
1036 proxy = listdomain.GetProxy(i)
1037 retval.append(proxy.GetXMLLabel())
1040 Available = property(GetAvailable, None, None,
1041 """This read only property is a list of strings you can
1042 use to select from the list domain. If there is no
1043 such list domain, the array is empty.""")
1046 """Implementation of the sequence API"""
1047 return GenericIterator(self)
1050 """Returns the number of elements."""
1051 return self.SMProperty.GetNumberOfProxies()
1053 def remove(self, proxy):
1054 """Removes the first occurence of the proxy from the property."""
1055 self.SMProperty.RemoveProxy(proxy.SMProxy)
1056 self._UpdateProperty()
1058 def __setitem__(self, idx, value):
1059 """Given a list or tuple of values, sets a slice of values [min, max)"""
1060 if isinstance(idx, slice):
1061 indices = idx.indices(len(self))
1062 for i, j in zip(range(*indices), value):
1063 self.SMProperty.SetProxy(i, j.SMProxy)
1064 self._UpdateProperty()
1065 elif idx >= len(self) or idx < 0:
1068 self.SMProperty.SetProxy(idx, value.SMProxy)
1069 self._UpdateProperty()
1071 def __delitem__(self,idx):
1072 """Removes the element idx"""
1073 if isinstance(idx, slice):
1074 indices = idx.indices(len(self))
1075 # Collect the elements to delete to a new list first.
1076 # Otherwise indices are screwed up during the actual
1079 for i in range(*indices):
1080 toremove.append(self[i])
1082 self.SMProperty.RemoveProxy(i.SMProxy)
1083 self._UpdateProperty()
1084 elif idx >= len(self) or idx < 0:
1087 self.SMProperty.RemoveProxy(self[idx].SMProxy)
1088 self._UpdateProperty()
1090 def __getitem__(self, idx):
1091 """Returns the range [min, max) of elements. Raises an IndexError
1092 exception if an argument is out of bounds."""
1093 if isinstance(idx, slice):
1094 indices = idx.indices(len(self))
1096 for i in range(*indices):
1097 retVal.append(_getPyProxy(self.SMProperty.GetProxy(i)))
1099 elif idx >= len(self) or idx < 0:
1101 return _getPyProxy(self.SMProperty.GetProxy(idx))
1103 def __getattr__(self, name):
1104 "Unknown attribute requests get forwarded to SMProperty."
1105 return getattr(self.SMProperty, name)
1107 def index(self, proxy):
1111 if proxy.IsSame(px):
1114 raise ValueError("proxy is not in the list.")
1116 def append(self, proxy):
1117 "Appends the given proxy to the property values."
1118 self.SMProperty.AddProxy(proxy.SMProxy)
1119 self._UpdateProperty()
1122 "Returns all elements as either a list or a single value."
1123 property = self.SMProperty
1124 if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
1125 return self[0:len(self)]
1127 if property.GetNumberOfProxies() > 0:
1128 return _getPyProxy(property.GetProxy(0))
1131 def SetData(self, values):
1132 """Allows setting of all values at once. Requires a single value,
1134 if isinstance(values, str):
1137 position = self.Available.index(values)
1139 raise ValueError, values + " is not a valid object in the domain."
1140 values = self.GetDomain('proxy_list').GetProxy(position)
1141 if not isinstance(values, tuple) and \
1142 not isinstance(values, list):
1144 self.SMProperty.RemoveAllProxies()
1145 for value in values:
1146 if isinstance(value, Proxy):
1147 value_proxy = value.SMProxy
1150 self.SMProperty.AddProxy(value_proxy)
1151 self._UpdateProperty()
1154 "Removes all elements."
1155 self.SMProperty.RemoveAllProxies()
1156 self._UpdateProperty()
1158 class InputProperty(ProxyProperty):
1159 """An InputProperty allows making pipeline connections. You can set either
1160 a source proxy or an OutputProperty to an input property:
1162 > property[0] = proxy
1164 > property[0] = OuputPort(proxy, 1)
1166 > property.append(proxy)
1168 > property.append(OutputPort(proxy, 0))
1170 def __setitem__(self, idx, value):
1171 """Given a list or tuple of values, sets a slice of values [min, max)"""
1172 if isinstance(idx, slice):
1173 indices = idx.indices(len(self))
1174 for i, j in zip(range(*indices), value):
1176 self.SMProperty.SetInputConnection(i, op.SMProxy, op.Port)
1177 self._UpdateProperty()
1178 elif idx >= len(self) or idx < 0:
1181 self.SMProperty.SetInputConnection(idx, value.SMProxy, value.Port)
1182 self._UpdateProperty()
1184 def __getitem__(self, idx):
1185 """Returns the range [min, max) of elements. Raises an IndexError
1186 exception if an argument is out of bounds."""
1187 if isinstance(idx, slice):
1188 indices = idx.indices(len(self))
1190 for i in range(*indices):
1192 if self.SMProperty.GetProxy(i):
1193 port = OutputPort(_getPyProxy(self.SMProperty.GetProxy(i)),\
1194 self.SMProperty.GetOutputPortForConnection(i))
1197 elif idx >= len(self) or idx < 0:
1199 return OutputPort(_getPyProxy(self.SMProperty.GetProxy(idx)),\
1200 self.SMProperty.GetOutputPortForConnection(idx))
1202 def append(self, value):
1203 """Appends the given proxy to the property values.
1204 Accepts Proxy or OutputPort objects."""
1205 self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
1206 self._UpdateProperty()
1209 """Returns all elements as either a list of OutputPort objects or
1210 a single OutputPort object."""
1211 property = self.SMProperty
1212 if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
1213 return self[0:len(self)]
1215 if property.GetNumberOfProxies() > 0:
1216 return OutputPort(_getPyProxy(property.GetProxy(0)),\
1217 self.SMProperty.GetOutputPortForConnection(0))
1220 def SetData(self, values):
1221 """Allows setting of all values at once. Requires a single value,
1222 a tuple or list. Accepts Proxy or OutputPort objects."""
1223 if isinstance(values, str):
1224 ProxyProperty.SetData(self, values)
1226 if not isinstance(values, tuple) and \
1227 not isinstance(values, list):
1229 self.SMProperty.RemoveAllProxies()
1230 for value in values:
1232 self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
1233 self._UpdateProperty()
1235 def _UpdateProperty(self):
1236 "Pushes the value of this property to the server."
1237 ProxyProperty._UpdateProperty(self)
1238 iter = PropertyIterator(self.Proxy)
1240 if isinstance(prop, ArraySelectionProperty):
1241 prop.UpdateDefault()
1244 class DataInformation(object):
1245 """DataInformation is a contained for meta-data associated with an
1248 DataInformation is a python wrapper around a vtkPVDataInformation.
1249 In addition to proving all methods of a vtkPVDataInformation, it provides
1250 a few convenience methods.
1252 Please note that some of the methods accessible through the DataInformation
1253 class are not listed by help() because the DataInformation objects forward
1254 unresolved attributes to the underlying object. To get the full list,
1255 see also dir(proxy.DataInformation).
1256 See also the doxygen based documentation of the vtkPVDataInformation C++
1259 def __init__(self, dataInformation, proxy, idx):
1260 """Default constructor. Requires a vtkPVDataInformation, a source proxy
1261 and an output port id."""
1262 self.DataInformation = dataInformation
1267 """****Deprecated**** There is no reason anymore to use this method
1268 explicitly, it is called automatically when one gets any value from the
1269 data information object.
1270 Update the data information if necessary. Note that this
1271 does not cause execution of the underlying object. In certain
1272 cases, you may have to call UpdatePipeline() on the proxy."""
1274 self.Proxy.GetDataInformation(self.Idx)
1276 def GetDataSetType(self):
1277 """Returns the dataset type as defined in vtkDataObjectTypes."""
1279 if not self.DataInformation:
1280 raise RuntimeError, "No data information is available"
1281 if self.DataInformation.GetCompositeDataSetType() > -1:
1282 return self.DataInformation.GetCompositeDataSetType()
1283 return self.DataInformation.GetDataSetType()
1285 def GetDataSetTypeAsString(self):
1286 """Returns the dataset type as a user-friendly string. This is
1287 not the same as the enumaration used by VTK"""
1288 return vtk.vtkDataObjectTypes.GetClassNameFromTypeId(self.GetDataSetType())
1290 def __getattr__(self, name):
1291 """Forwards unknown attribute requests to the underlying
1292 vtkPVInformation."""
1293 if not self.DataInformation:
1294 raise AttributeError("class has no attribute %s" % name)
1297 return getattr(self.DataInformation, name)
1299 class ArrayInformation(object):
1300 """Meta-information associated with an array. Use the Name
1301 attribute to get the array name.
1303 Please note that some of the methods accessible through the ArrayInformation
1304 class are not listed by help() because the ArrayInformation objects forward
1305 unresolved attributes to the underlying object.
1306 See the doxygen based documentation of the vtkPVArrayInformation C++
1307 class for a full list.
1309 def __init__(self, proxy, field, name):
1311 self.FieldData = field
1314 def __getattr__(self, name):
1315 """Forward unknown methods to vtkPVArrayInformation"""
1316 array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
1317 if not array: return None
1318 return getattr(array, name)
1321 """Returns a user-friendly representation string."""
1322 return "Array: " + self.Name
1324 def GetRange(self, component=0):
1325 """Given a component, returns its value range as a tuple of 2 values."""
1326 array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
1327 range = array.GetComponentRange(component)
1328 return (range[0], range[1])
1330 class FieldDataInformationIterator(object):
1331 """Iterator for FieldDataInformation"""
1333 def __init__(self, info, items=False):
1334 self.FieldDataInformation = info
1342 if self.index >= self.FieldDataInformation.GetNumberOfArrays():
1346 ai = self.FieldDataInformation[self.index-1]
1348 return (ai.GetName(), ai)
1353 class FieldDataInformation(object):
1354 """Meta-data for a field of an output object (point data, cell data etc...).
1355 Provides easy access to the arrays using the slice interface:
1356 > narrays = len(field_info)
1357 > for i in range(narrays):
1358 > array_info = field_info[i]
1360 Full slice interface is supported:
1361 > arrays = field_info[0:5:3]
1362 where arrays is a list.
1364 Array access by name is also possible:
1365 > array_info = field_info['Temperature']
1367 The number of arrays can also be accessed using the NumberOfArrays
1370 def __init__(self, proxy, idx, field):
1372 self.OutputPort = idx
1373 self.FieldData = field
1375 def GetFieldData(self):
1376 """Convenience method to get the underlying
1377 vtkPVDataSetAttributesInformation"""
1378 return getattr(self.Proxy.GetDataInformation(self.OutputPort), "Get%sInformation" % self.FieldData)()
1380 def GetNumberOfArrays(self):
1381 """Returns the number of arrays."""
1382 self.Proxy.UpdatePipeline()
1383 return self.GetFieldData().GetNumberOfArrays()
1385 def GetArray(self, idx):
1386 """Given an index or a string, returns an array information.
1387 Raises IndexError if the index is out of bounds."""
1388 self.Proxy.UpdatePipeline()
1389 if not self.GetFieldData().GetArrayInformation(idx):
1391 if isinstance(idx, str):
1392 return ArrayInformation(self.Proxy, self, idx)
1393 elif idx >= len(self) or idx < 0:
1395 return ArrayInformation(self.Proxy, self, self.GetFieldData().GetArrayInformation(idx).GetName())
1398 """Returns the number of arrays."""
1399 return self.GetNumberOfArrays()
1401 def __getitem__(self, idx):
1402 """Implements the [] operator. Accepts an array name."""
1403 if isinstance(idx, slice):
1404 indices = idx.indices(self.GetNumberOfArrays())
1406 for i in range(*indices):
1407 retVal.append(self.GetArray(i))
1409 return self.GetArray(idx)
1412 """Implementation of the dictionary API"""
1414 narrays = self.GetNumberOfArrays()
1415 for i in range(narrays):
1416 kys.append(self.GetArray(i).GetName())
1420 """Implementation of the dictionary API"""
1422 narrays = self.GetNumberOfArrays()
1423 for i in range(narrays):
1424 vals.append(self.GetArray(i))
1427 def iteritems(self):
1428 """Implementation of the dictionary API"""
1429 return FieldDataInformationIterator(self, True)
1432 """Implementation of the dictionary API"""
1434 narrays = self.GetNumberOfArrays()
1435 for i in range(narrays):
1436 ai = self.GetArray(i)
1437 itms.append((ai.GetName(), ai))
1440 def has_key(self, key):
1441 """Implementation of the dictionary API"""
1442 if self.GetArray(key):
1447 """Implementation of the dictionary API"""
1448 return FieldDataInformationIterator(self)
1450 def __getattr__(self, name):
1451 """Forwards unknown attributes to the underlying
1452 vtkPVDataSetAttributesInformation"""
1453 array = self.GetArray(name)
1454 if array: return array
1455 raise AttributeError("class has no attribute %s" % name)
1458 NumberOfArrays = property(GetNumberOfArrays, None, None, "Returns the number of arrays.")
1460 def OutputPort(proxy, outputPort=0):
1463 if isinstance(outputPort, str):
1464 outputPort = proxy.GetOutputPortIndex(outputPort)
1465 if outputPort >= proxy.GetNumberOfOutputPorts():
1467 if proxy.Port == outputPort:
1469 newinstance = _getPyProxy(proxy.SMProxy, outputPort)
1470 newinstance.Port = outputPort
1471 newinstance._Proxy__Properties = proxy._Proxy__Properties
1474 class ProxyManager(object):
1475 """When running scripts from the python shell in the ParaView application,
1476 registering proxies with the proxy manager is the only mechanism to
1477 notify the graphical user interface (GUI) that a proxy
1478 exists. Therefore, unless a proxy is registered, it will not show up in
1479 the user interface. Also, the proxy manager is the only way to get
1480 access to proxies created using the GUI. Proxies created using the GUI
1481 are automatically registered under an appropriate group (sources,
1482 filters, representations and views). To get access to these objects,
1483 you can use proxyManager.GetProxy(group, name). The name is the same
1484 as the name shown in the pipeline browser.
1486 This class is a python wrapper for vtkSMProxyManager. Note that the
1487 underlying vtkSMProxyManager is a singleton. All instances of this
1488 class will refer to the same object. In addition to all methods provided by
1489 vtkSMProxyManager (all unknown attribute requests are forwarded
1490 to the vtkSMProxyManager), this class provides several convenience
1493 Please note that some of the methods accessible through the ProxyManager
1494 class are not listed by help() because the ProxyManager objects forwards
1495 unresolved attributes to the underlying object. To get the full list,
1496 see also dir(proxy.SMProxyManager). See also the doxygen based documentation
1497 of the vtkSMProxyManager C++ class.
1500 def __init__(self, session=None):
1501 """Constructor. Assigned self.SMProxyManager to
1502 vtkSMProxyManager.GetProxyManager()."""
1503 global ActiveConnection
1505 session = ActiveConnection.Session
1506 self.SMProxyManager = session.GetSessionProxyManager()
1508 def RegisterProxy(self, group, name, aProxy):
1509 """Registers a proxy (either SMProxy or proxy) with the
1511 if isinstance(aProxy, Proxy):
1512 self.SMProxyManager.RegisterProxy(group, name, aProxy.SMProxy)
1514 self.SMProxyManager.RegisterProxy(group, name, aProxy)
1516 def NewProxy(self, group, name):
1517 """Creates a new proxy of given group and name and returns an SMProxy.
1518 Note that this is a server manager object. You should normally create
1519 proxies using the class objects. For example:
1520 obj = servermanager.sources.SphereSource()"""
1521 if not self.SMProxyManager:
1523 aProxy = self.SMProxyManager.NewProxy(group, name, "NULL")
1526 aProxy.UnRegister(None)
1529 def GetProxy(self, group, name):
1530 """Returns a Proxy registered under a group and name"""
1531 if not self.SMProxyManager:
1533 aProxy = self.SMProxyManager.GetProxy(group, name)
1536 return _getPyProxy(aProxy)
1538 def GetPrototypeProxy(self, group, name):
1539 """Returns a prototype proxy given a group and name. This is an
1540 SMProxy. This is a low-level method. You should not normally
1542 if not self.SMProxyManager:
1544 aProxy = self.SMProxyManager.GetPrototypeProxy(group, name)
1549 def GetProxiesInGroup(self, groupname):
1550 """Returns a map of proxies in a particular group."""
1552 iter = self.NewGroupIterator(groupname)
1554 proxies[(iter.GetKey(), aProxy.GetGlobalIDAsString())] = aProxy
1557 def UnRegisterProxy(self, groupname, proxyname, aProxy):
1558 """Unregisters a proxy."""
1559 if not self.SMProxyManager:
1561 if aProxy != None and isinstance(aProxy,Proxy):
1562 aProxy = aProxy.SMProxy
1564 self.SMProxyManager.UnRegisterProxy(groupname, proxyname, aProxy)
1566 def GetProxies(self, groupname, proxyname):
1567 """Returns all proxies registered under the given group with the
1568 given name. Note that it is possible to register more than one
1569 proxy with the same name in the same group. Because the proxies
1570 are different, there is no conflict. Use this method instead of
1571 GetProxy() if you know that there are more than one proxy registered
1573 if not self.SMProxyManager:
1575 collection = vtk.vtkCollection()
1577 self.SMProxyManager.GetProxies(groupname, proxyname, collection)
1578 for i in range(0, collection.GetNumberOfItems()):
1579 aProxy = _getPyProxy(collection.GetItemAsObject(i))
1581 result.append(aProxy)
1586 """Returns a new ProxyIterator."""
1587 iter = ProxyIterator()
1591 def NewGroupIterator(self, group_name):
1592 """Returns a ProxyIterator for a group. The resulting object
1593 can be used to traverse the proxies that are in the given
1595 iter = self.__iter__()
1596 iter.SetModeToOneGroup()
1597 iter.Begin(group_name)
1600 def NewDefinitionIterator(self, groupname=None):
1601 """Returns an iterator that can be used to iterate over
1602 all groups and types of proxies that the proxy manager
1605 if groupname != None:
1606 iter = ProxyDefinitionIterator(self.GetProxyDefinitionManager().NewSingleGroupIterator(groupname,0))
1608 iter = ProxyDefinitionIterator(self.GetProxyDefinitionManager().NewIterator(0))
1612 def __ConvertArgumentsAndCall(self, *args):
1615 if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
1616 newArgs.append(arg.SMProxy)
1619 func = getattr(self.SMProxyManager, self.__LastAttrName)
1620 retVal = func(*newArgs)
1621 if type(retVal) is type(self.SMProxyManager) and retVal.IsA("vtkSMProxy"):
1622 return _getPyProxy(retVal)
1626 def __getattr__(self, name):
1627 """Returns attribute from the ProxyManager"""
1629 pmAttr = getattr(self.SMProxyManager, name)
1630 self.__LastAttrName = name
1631 return self.__ConvertArgumentsAndCall
1634 return getattr(self.SMProxyManager, name)
1636 def LoadState(self, filename, loader = None):
1637 self.SMProxyManager.LoadXMLState(filename, loader)
1639 def SaveState(self, filename):
1640 self.SMProxyManager.SaveXMLState(filename)
1642 class PropertyIterator(object):
1643 """Wrapper for a vtkSMPropertyIterator class to satisfy
1644 the python iterator protocol. Note that the list of
1645 properties can also be obtained from the class object's
1647 See the doxygen documentation for vtkSMPropertyIterator C++
1651 def __init__(self, aProxy):
1652 self.SMIterator = aProxy.NewPropertyIterator()
1654 self.SMIterator.UnRegister(None)
1655 self.SMIterator.Begin()
1657 self.PropertyLabel = None
1664 if not self.SMIterator:
1667 if self.SMIterator.IsAtEnd():
1670 self.Key = self.SMIterator.GetKey()
1671 self.PropertyLabel = self.SMIterator.GetPropertyLabel()
1672 self.SMIterator.Next()
1673 return self.Proxy.GetProperty(self.Key)
1676 """Returns the proxy for the property last returned by the call to
1681 """Returns the key for the property last returned by the call to
1685 def GetProperty(self):
1686 """Returns the property last returned by the call to 'next()' """
1687 return self.Proxy.GetProperty(self.Key)
1689 def __getattr__(self, name):
1690 """returns attributes from the vtkSMPropertyIterator."""
1691 return getattr(self.SMIterator, name)
1693 class ProxyDefinitionIterator(object):
1694 """Wrapper for a vtkPVProxyDefinitionIterator class to satisfy
1695 the python iterator protocol.
1696 See the doxygen documentation of the vtkPVProxyDefinitionIterator
1697 C++ class for more information."""
1698 def __init__(self, iter):
1699 self.SMIterator = iter
1701 self.SMIterator.UnRegister(None)
1702 self.SMIterator.InitTraversal()
1710 if self.SMIterator.IsDoneWithTraversal():
1714 self.Group = self.SMIterator.GetGroupName()
1715 self.Key = self.SMIterator.GetProxyName()
1716 self.SMIterator.GoToNextItem()
1717 return {"group": self.Group, "key":self.Key }
1719 def GetProxyName(self):
1720 """Returns the key for the proxy definition last returned by the call
1725 """Returns the group for the proxy definition last returned by the
1726 call to 'next()' """
1729 def __getattr__(self, name):
1730 """returns attributes from the vtkPVProxyDefinitionIterator."""
1731 return getattr(self.SMIterator, name)
1734 class ProxyIterator(object):
1735 """Wrapper for a vtkSMProxyIterator class to satisfy the
1736 python iterator protocol.
1737 See the doxygen documentation of vtkSMProxyIterator C++ class for
1741 self.SMIterator = vtkSMProxyIterator()
1742 self.SMIterator.SetSession(ActiveConnection.Session)
1743 self.SMIterator.Begin()
1752 if self.SMIterator.IsAtEnd():
1758 self.AProxy = _getPyProxy(self.SMIterator.GetProxy())
1759 self.Group = self.SMIterator.GetGroup()
1760 self.Key = self.SMIterator.GetKey()
1761 self.SMIterator.Next()
1765 """Returns the proxy last returned by the call to 'next()'"""
1769 """Returns the key for the proxy last returned by the call to
1774 """Returns the group for the proxy last returned by the call to
1778 def __getattr__(self, name):
1779 """returns attributes from the vtkSMProxyIterator."""
1780 return getattr(self.SMIterator, name)
1782 # Caution: Observers must be global methods otherwise we run into memory
1783 # leak when the interpreter get reset from the C++ layer.
1784 def _update_definitions(caller, event):
1785 updateModules(ActiveConnection.Modules)
1787 class Connection(object):
1789 This is a python representation for a session/connection.
1791 def __init__(self, connectionId, session):
1792 """Default constructor. Creates a Connection with the given
1793 ID, all other data members initialized to None."""
1794 global MultiServerConnections
1795 global ActiveConnection
1796 self.ID = connectionId
1797 self.Session = session
1798 self.Modules = PVModule()
1800 self.DefinitionObserverTag = 0
1801 self.CustomDefinitionObserverTag = 0
1802 if MultiServerConnections == None and ActiveConnection:
1803 raise RuntimeError, "Concurrent connections not supported!"
1804 if MultiServerConnections != None and not self in MultiServerConnections:
1805 MultiServerConnections.append(self)
1806 ActiveConnection = self
1807 __InitAfterConnect__(self)
1808 __exposeActiveModules__()
1810 def __eq__(self, other):
1811 "Returns true if the connection ids are the same."
1812 return (self.ID == other.ID)
1815 """User friendly string representation"""
1816 return "Connection (%s) [%d]" % (self.Session.GetURI(), self.ID)
1819 """Get URI of the connection"""
1820 return self.Session.GetURI()
1823 """Returns True if the connection to a remote server, False if
1824 it is local (built-in)"""
1825 if self.Session.IsA("vtkSMSessionClient"):
1829 def GetNumberOfDataPartitions(self):
1830 """Returns the number of partitions on the data server for this
1832 return self.Session.GetServerInformation().GetNumberOfProcesses()
1834 def AttachDefinitionUpdater(self):
1835 """Attach observer to automatically update modules when needed."""
1836 # VTN: Observers are not supported
1837 # ProxyDefinitionsUpdated = 2000
1838 ## self.DefinitionObserverTag = self.Session.GetProxyDefinitionManager().AddObserver(2000, _update_definitions)
1839 # CompoundProxyDefinitionsUpdated = 2001
1840 ## self.CustomDefinitionObserverTag = self.Session.GetProxyDefinitionManager().AddObserver(2001, _update_definitions)
1844 if self.DefinitionObserverTag:
1845 self.Session.GetProxyDefinitionManager().RemoveObserver(self.DefinitionObserverTag)
1846 self.Session.GetProxyDefinitionManager().RemoveObserver(self.CustomDefinitionObserverTag)
1855 def SaveState(filename):
1856 """Given a state filename, saves the state of objects registered
1857 with the proxy manager."""
1859 pm.SaveState(filename)
1861 def LoadState(filename, connection=None):
1862 """Given a state filename and an optional connection, loads the server
1865 connection = ActiveConnection
1867 raise RuntimeError, "Cannot load state without a connection"
1869 pm.LoadState(filename, None)
1870 views = GetRenderViews()
1872 # Make sure that the client window size matches the
1873 # ViewSize property. In paraview, the GUI takes care
1875 if view.GetClassName() == "vtkSMIceTDesktopRenderViewProxy":
1876 view.GetRenderWindow().SetSize(view.ViewSize[0], \
1881 Method used to initialize the Python Shell from the ParaView GUI.
1883 global fromGUI, ActiveConnection
1885 print "from paraview.simple import *"
1887 # ToggleProgressPrinting() ### FIXME COLLABORATION
1888 enableMultiServer(vtkProcessModule.GetProcessModule().GetMultipleSessionsSupport())
1889 iter = vtkProcessModule.GetProcessModule().NewSessionIterator();
1890 iter.InitTraversal()
1891 ActiveConnection = None
1892 activeSession = vtkSMProxyManager.GetProxyManager().GetActiveSession()
1893 tmpActiveConnection = None
1894 while not iter.IsDoneWithTraversal():
1895 c = Connection(iter.GetCurrentSessionId(), iter.GetCurrentSession())
1896 if c.Session == activeSession:
1897 tmpActiveConnection = c
1899 iter.UnRegister(None)
1900 if tmpActiveConnection:
1901 ActiveConnection = tmpActiveConnection
1903 def Connect(ds_host=None, ds_port=11111, rs_host=None, rs_port=22221):
1905 Use this function call to create a new session. On success,
1906 it returns a vtkSMSession object that abstracts the connection.
1907 Otherwise, it returns None.
1908 There are several ways in which this function can be called:
1909 * When called with no arguments, it creates a new session
1910 to the built-in server on the client itself.
1911 * When called with ds_host and ds_port arguments, it
1912 attempts to connect to a server(data and render server on the same server)
1913 on the indicated host:port.
1914 * When called with ds_host, ds_port, rs_host, rs_port, it
1915 creates a new connection to the data server on ds_host:ds_port and to the
1916 render server on rs_host: rs_port.
1920 raise RuntimeError, "Cannot create a session through python. Use the GUI to setup the connection."
1922 session = vtkSMSession()
1923 elif rs_host == None:
1924 session = vtkSMSessionClient()
1925 session.Connect("cs://%s:%d" % (ds_host, ds_port))
1927 session = vtkSMSessionClient()
1928 session.Connect("cdsrs://%s:%d/%s:%d" % (ds_host, ds_port, rs_host, rs_port))
1929 id = vtkProcessModule.GetProcessModule().RegisterSession(session)
1930 connection = Connection(id, session)
1933 def ReverseConnect(port=11111):
1935 Use this function call to create a new session. On success,
1936 it returns a Session object that abstracts the connection.
1937 Otherwise, it returns None.
1938 In reverse connection mode, the client waits for a connection
1939 from the server (client has to be started first). The server
1940 then connects to the client (run pvserver with -rc and -ch
1942 The optional port specified the port to listen to.
1946 raise RuntimeError, "Cannot create a connection through python. Use the GUI to setup the connection."
1947 session = vtkSMSessionClient()
1948 session.Connect("csrc://hostname:" + port)
1949 id = vtkProcessModule.GetProcessModule().RegisterSession(session)
1950 connection = Connection(id, session)
1953 def Disconnect(session=None):
1954 """Disconnects the connection. Make sure to clear the proxy manager
1956 global ActiveConnection
1957 global MultiServerConnections
1960 raise RuntimeError, "Cannot disconnect through python. Use the GUI to disconnect."
1961 if ActiveConnection and (not session or session == ActiveConnection.Session):
1962 session = ActiveConnection.Session
1963 if MultiServerConnections:
1964 MultiServerConnections.remove(ActiveConnection)
1965 ActiveConnection.close()
1966 ActiveConnection = None
1967 switchActiveConnection()
1969 ActiveConnection.close()
1970 ActiveConnection = None
1971 elif MultiServerConnections:
1972 for connection in MultiServerConnections:
1973 if connection.Session == session:
1975 MultiServerConnections.remove(connection)
1977 vtkProcessModule.GetProcessModule().UnRegisterSession(session)
1980 def CreateProxy(xml_group, xml_name, session=None):
1981 """Creates a proxy. If session is set, the proxy's session is
1982 set accordingly. If session is None, the current Session is used, if
1983 present. You should not have to use method normally. Instantiate the
1984 appropriate class from the appropriate module, for example:
1985 sph = servermanager.sources.SphereSource()"""
1986 global ActiveConnection
1988 session = ActiveConnection.Session
1990 raise RuntimeError, "Cannot create objects without a session."
1991 pxm = ProxyManager(session)
1992 return pxm.NewProxy(xml_group, xml_name)
1994 def GetRenderView(connection=None):
1995 """Return the render view in use. If more than one render view is in
1996 use, return the first one."""
1998 render_module = None
1999 for aProxy in ProxyManager():
2000 if aProxy.IsA("vtkSMRenderViewProxy"):
2001 render_module = aProxy
2003 return render_module
2005 def GetRenderViews(connection=None):
2006 """Returns the set of all render views."""
2008 for aProxy in ProxyManager():
2009 if aProxy.IsA("vtkSMRenderViewProxy"):
2010 render_modules.append(aProxy)
2011 return render_modules
2013 def GetContextViews(connection=None):
2014 """Returns the set of all context views."""
2015 context_modules = []
2016 for aProxy in ProxyManager():
2017 if aProxy.IsA("vtkSMContextViewProxy"):
2018 context_modules.append(aProxy)
2019 return context_modules
2021 def CreateRenderView(session=None, **extraArgs):
2022 """Creates a render window on the particular session. If session
2023 is not specified, then the active session is used, if available.
2025 This method can also be used to initialize properties by passing
2026 keyword arguments where the key is the name of the property. In addition
2027 registrationGroup and registrationName (optional) can be specified (as
2028 keyword arguments) to automatically register the proxy with the proxy
2030 return _create_view("RenderView", session, **extraArgs)
2032 def _create_view(view_xml_name, session=None, **extraArgs):
2033 """Creates a view on the particular session. If session
2034 is not specified, then the active session is used, if available.
2035 This method can also be used to initialize properties by passing
2036 keyword arguments where the key is the name of the property."""
2038 session = ActiveConnection.Session
2040 raise RuntimeError, "Cannot create view without session."
2041 pxm = ProxyManager()
2044 view_module = CreateProxy("views", view_xml_name, session)
2047 extraArgs['proxy'] = view_module
2048 python_proxy_name = _make_name_valid(view_module.GetXMLName())
2049 proxy = rendering.__dict__[python_proxy_name](**extraArgs)
2052 def GetRepresentation(aProxy, view):
2053 for rep in view.Representations:
2055 try: isRep = rep.Input.IsSame(aProxy)
2056 except: isRep = False
2057 if isRep: return rep
2060 def CreateRepresentation(aProxy, view, **extraArgs):
2061 """Creates a representation for the proxy and adds it to the render
2064 This method can also be used to initialize properties by passing
2065 keyword arguments where the key is the name of the property.In addition
2066 registrationGroup and registrationName (optional) can be specified (as
2067 keyword arguments) to automatically register the proxy with the proxy
2070 This method tries to create the best possible representation for the given
2071 proxy in the given view. Additionally, the user can specify proxyName
2072 (optional) to create a representation of a particular type."""
2076 raise RuntimeError, "proxy argument cannot be None."
2078 raise RuntimeError, "view argument cannot be None."
2079 if "proxyName" in extraArgs:
2080 display = CreateProxy("representations", extraArgs['proxyName'], None)
2081 del extraArgs['proxyName']
2083 display = view.SMProxy.CreateDefaultRepresentation(aProxy.SMProxy, 0)
2085 display.UnRegister(None)
2088 extraArgs['proxy'] = display
2089 proxy = rendering.__dict__[display.GetXMLName()](**extraArgs)
2090 proxy.Input = aProxy
2091 proxy.UpdateVTKObjects()
2092 view.Representations.append(proxy)
2095 class _ModuleLoader(object):
2096 def find_module(self, fullname, path=None):
2097 if vtkPVPythonModule.HasModule(fullname):
2101 def load_module(self, fullname):
2103 moduleInfo = vtkPVPythonModule.GetModule(fullname)
2106 module = sys.modules.setdefault(fullname, imp.new_module(fullname))
2107 module.__file__ = "<%s>" % moduleInfo.GetFullName()
2108 module.__loader__ = self
2109 if moduleInfo.GetIsPackage:
2110 module.__path__ = moduleInfo.GetFullName()
2111 code = compile(moduleInfo.GetSource(), module.__file__, 'exec')
2112 exec code in module.__dict__
2115 def LoadXML(xmlstring):
2116 """DEPRECATED. Given a server manager XML as a string, parse and process it."""
2117 raise RuntimeError, "Deprecated. Use LoadPlugin(...) instead."
2120 def LoadPlugin(filename, remote=True, connection=None):
2121 """ Given a filename and a session (optional, otherwise uses
2122 ActiveConnection), loads a plugin. It then updates the sources,
2123 filters and rendering modules."""
2126 connection = ActiveConnection
2128 raise RuntimeError, "Cannot load a plugin without a connection."
2129 plm = vtkSMProxyManager.GetProxyManager().GetPluginManager()
2132 status = plm.LoadRemotePlugin(filename, connection.Session)
2134 status = plm.LoadLocalPlugin(filename)
2136 # shouldn't the extension check happend before attempting to load the plugin?
2138 raise RuntimeError, "Problem loading plugin %s" % (filename)
2140 # we should never have to call this. The modules should update automatically.
2141 updateModules(connection.Modules)
2144 def Fetch(input, arg1=None, arg2=None, idx=0):
2146 A convenience method that moves data from the server to the client,
2147 optionally performing some operation on the data as it moves.
2148 The input argument is the name of the (proxy for a) source or filter
2149 whose output is needed on the client.
2151 You can use Fetch to do three things:
2153 If arg1 is None (the default) then all of the data is brought to the client.
2154 In parallel runs an appropriate append Filter merges the
2155 data on each processor into one data object. The filter chosen will be
2156 vtkAppendPolyData for vtkPolyData, vtkAppendRectilinearGrid for
2157 vtkRectilinearGrid, vtkMultiBlockDataGroupFilter for vtkCompositeData,
2158 and vtkAppendFilter for anything else.
2160 If arg1 is an integer then one particular processor's output is brought to
2161 the client. In serial runs the arg is ignored. If you have a filter that
2162 computes results in parallel and brings them to the root node, then set
2165 If arg1 and arg2 are a algorithms, for example vtkMinMax, the algorithm
2166 will be applied to the data to obtain some result. Here arg1 will be
2167 applied pre-gather and arg2 will be applied post-gather. In parallel
2168 runs the algorithm will be run on each processor to make intermediate
2169 results and then again on the root processor over all of the
2170 intermediate results to create a global result.
2172 Optional argument idx is used to specify the output port number to fetch the
2173 data from. Default is port 0.
2178 reducer = filters.ReductionFilter(Input=OutputPort(input,idx))
2180 #create the pipeline that reduces and transmits the data
2182 cdinfo = input.GetDataInformation(idx).GetCompositeDataInformation()
2183 if cdinfo.GetDataIsComposite():
2184 print "use composite data append"
2185 reducer.PostGatherHelperName = "vtkMultiBlockDataGroupFilter"
2187 elif input.GetDataInformation(idx).GetDataClassName() == "vtkPolyData":
2188 print "use append poly data filter"
2189 reducer.PostGatherHelperName = "vtkAppendPolyData"
2191 elif input.GetDataInformation(idx).GetDataClassName() == "vtkRectilinearGrid":
2192 print "use append rectilinear grid filter"
2193 reducer.PostGatherHelperName = "vtkAppendRectilinearGrid"
2195 elif input.GetDataInformation(idx).IsA("vtkDataSet"):
2196 print "use unstructured append filter"
2197 reducer.PostGatherHelperName = "vtkAppendFilter"
2199 elif type(arg1) is types.IntType:
2200 reducer.PassThrough = arg1
2203 reducer.PreGatherHelper = arg1
2204 reducer.PostGatherHelper = arg2
2207 reducer.UpdatePipeline()
2208 dataInfo = reducer.GetDataInformation(0)
2209 dataType = dataInfo.GetDataSetType()
2210 if dataInfo.GetCompositeDataSetType() > 0:
2211 dataType = dataInfo.GetCompositeDataSetType()
2213 fetcher = filters.ClientServerMoveData(Input=reducer)
2214 fetcher.OutputDataType = dataType
2215 fetcher.WholeExtent = dataInfo.GetExtent()[:]
2217 fetcher.UpdatePipeline()
2219 op = fetcher.GetClientSideObject().GetOutputDataObject(0)
2220 opc = op.NewInstance()
2222 opc.UnRegister(None)
2225 def AnimateReader(reader, view, filename=None):
2226 """This is a utility function that, given a reader and a view
2227 animates over all time steps of the reader. If the optional
2228 filename is provided, a movie is created (type depends on the
2229 extension of the filename."""
2231 raise RuntimeError, "No reader was specified, cannot animate."
2233 raise RuntimeError, "No view was specified, cannot animate."
2234 # Create an animation scene
2235 scene = animation.AnimationScene()
2237 # We need to have the reader and the view registered with
2238 # the time keeper. This is how the scene gets its time values.
2240 tk = ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
2241 scene.TimeKeeper = tk
2243 tk = misc.TimeKeeper()
2244 scene.TimeKeeper = tk
2246 if not reader in tk.TimeSources:
2247 tk.TimeSources.append(reader)
2248 if not view in tk.Views:
2249 tk.Views.append(view)
2253 scene.ViewModules = [view]
2254 # Update the reader to get the time information
2255 reader.UpdatePipelineInformation()
2256 # Animate from 1st time step to last
2257 scene.StartTime = reader.TimestepValues.GetData()[0]
2258 scene.EndTime = reader.TimestepValues.GetData()[-1]
2260 # Each frame will correspond to a time step
2261 scene.PlayMode = 2 #Snap To Timesteps
2263 # Create a special animation cue for time.
2264 cue = animation.TimeAnimationCue()
2265 cue.AnimatedProxy = view
2266 cue.AnimatedPropertyName = "ViewTime"
2270 writer = vtkSMAnimationSceneImageWriter()
2271 writer.SetFileName(filename)
2272 writer.SetFrameRate(1)
2273 writer.SetAnimationScene(scene.SMProxy)
2275 # Now save the animation.
2276 if not writer.Save():
2277 raise RuntimeError, "Saving of animation failed!"
2282 def GetProgressPrintingIsEnabled():
2283 return progressObserverTag is not None
2285 def SetProgressPrintingEnabled(value):
2286 """Is not supported because of not supported observers"""
2289 def ToggleProgressPrinting():
2290 """Turn on/off printing of progress. See SetProgressPrintingEnabled."""
2291 SetProgressPrintingEnabled(not GetProgressPrintingIsEnabled())
2294 """Although not required, this can be called at exit to cleanup."""
2295 global progressObserverTag
2296 # Make sure to remove the observer
2297 if progressObserverTag:
2298 ToggleProgressPrinting()
2299 vtkInitializationHelper.Finalize()
2303 def _getPyProxy(smproxy, outputPort=0):
2304 """Returns a python wrapper for a server manager proxy. This method
2305 first checks if there is already such an object by looking in the
2306 _pyproxies group and returns it if found. Otherwise, it creates a
2307 new one. Proxies register themselves in _pyproxies upon creation."""
2310 if (smproxy, outputPort) in _pyproxies:
2311 return _pyproxies[(smproxy, outputPort)]()
2313 xmlName = smproxy.GetXMLName()
2314 if smproxy.GetXMLLabel():
2315 xmlName = smproxy.GetXMLLabel()
2316 classForProxy = _findClassForProxy(_make_name_valid(xmlName), smproxy.GetXMLGroup())
2318 retVal = classForProxy(proxy=smproxy, port=outputPort)
2320 retVal = Proxy(proxy=smproxy, port=outputPort)
2323 def _makeUpdateCameraMethod(rv):
2324 """ This internal method is used to create observer methods """
2325 if not hasattr(rv(), "BlockUpdateCamera"):
2326 rv().add_attribute("BlockUpdateCamera", False)
2327 def UpdateCamera(obj, string):
2328 if not rv().BlockUpdateCamera:
2329 # used to avoid some nasty recursion that occurs when interacting in
2331 rv().BlockUpdateCamera = True
2332 rv().SynchronizeCameraProperties()
2333 rv().BlockUpdateCamera = False
2336 def _createInitialize(group, name):
2337 """Internal method to create an Initialize() method for the sub-classes
2341 def aInitialize(self, connection=None, update=True):
2343 connection = ActiveConnection
2345 raise RuntimeError,\
2346 'Cannot create a proxy without a session.'
2347 if not connection.Session.GetProxyDefinitionManager().HasDefinition(pgroup, pname):
2348 error_msg = "The connection does not provide any definition for %s." % pname
2349 raise RuntimeError, error_msg
2350 self.InitializeFromProxy(\
2351 CreateProxy(pgroup, pname, connection.Session), update)
2354 def _createGetProperty(pName):
2355 """Internal method to create a GetXXX() method where XXX == pName."""
2357 def getProperty(self):
2358 return self.GetPropertyValue(propName)
2361 def _createSetProperty(pName):
2362 """Internal method to create a SetXXX() method where XXX == pName."""
2364 def setProperty(self, value):
2365 return self.SetPropertyWithName(propName, value)
2368 def _findClassForProxy(xmlName, xmlGroup):
2369 """Given the xmlName for a proxy, returns a Proxy class. Note
2370 that if there are duplicates, the first one is returned."""
2371 global sources, filters, writers, rendering, animation, implicit_functions,\
2372 piecewise_functions, extended_sources, misc
2375 if xmlGroup == "sources":
2376 return sources.__dict__[xmlName]
2377 elif xmlGroup == "filters":
2378 return filters.__dict__[xmlName]
2379 elif xmlGroup == "implicit_functions":
2380 return implicit_functions.__dict__[xmlName]
2381 elif xmlGroup == "piecewise_functions":
2382 return piecewise_functions.__dict__[xmlName]
2383 elif xmlGroup == "writers":
2384 return writers.__dict__[xmlName]
2385 elif xmlGroup == "extended_sources":
2386 return extended_sources.__dict__[xmlName]
2387 elif xmlName in rendering.__dict__:
2388 return rendering.__dict__[xmlName]
2389 elif xmlName in animation.__dict__:
2390 return animation.__dict__[xmlName]
2391 elif xmlName in misc.__dict__:
2392 return misc.__dict__[xmlName]
2396 def _printProgress(caller, event):
2397 """The default event handler for progress. Prints algorithm
2398 name and 1 '.' per 10% progress."""
2399 global currentAlgorithm, currentProgress
2401 pm = vtkProcessModule.GetProcessModule()
2402 progress = pm.GetLastProgress() / 10
2403 # If we got a 100% as the first thing, ignore
2404 # This is to get around the fact that some vtk
2405 # algorithms report 100% more than once (which is
2407 if not currentAlgorithm and progress == 10:
2409 alg = pm.GetLastProgressName()
2410 if alg != currentAlgorithm and alg:
2411 if currentAlgorithm:
2412 while currentProgress <= 10:
2414 sys.stdout.write(".")
2415 currentProgress += 1
2419 currentAlgorithm = alg
2420 while currentProgress <= progress:
2422 sys.stdout.write(".")
2423 #sys.stdout.write("%d " % pm.GetLastProgress())
2424 currentProgress += 1
2427 currentAlgorithm = None
2430 def updateModules(m):
2431 """Called when a plugin is loaded, this method updates
2432 the proxy class object in all known modules."""
2434 createModule("sources", m.sources)
2435 createModule("filters", m.filters)
2436 createModule("writers", m.writers)
2437 createModule("representations", m.rendering)
2438 createModule("views", m.rendering)
2439 createModule("lookup_tables", m.rendering)
2440 createModule("textures", m.rendering)
2441 createModule('cameramanipulators', m.rendering)
2442 createModule("animation", m.animation)
2443 createModule("misc", m.misc)
2444 createModule('animation_keyframes', m.animation)
2445 createModule('implicit_functions', m.implicit_functions)
2446 createModule('piecewise_functions', m.piecewise_functions)
2447 createModule("extended_sources", m.extended_sources)
2448 createModule("incremental_point_locators", m.misc)
2450 def _createModules(m):
2451 """Called when the module is loaded, this creates sub-
2452 modules for all know proxy groups."""
2454 m.sources = createModule('sources')
2455 m.filters = createModule('filters')
2456 m.writers = createModule('writers')
2457 m.rendering = createModule('representations')
2458 createModule('views', m.rendering)
2459 createModule("lookup_tables", m.rendering)
2460 createModule("textures", m.rendering)
2461 createModule('cameramanipulators', m.rendering)
2462 m.animation = createModule('animation')
2463 createModule('animation_keyframes', m.animation)
2464 m.implicit_functions = createModule('implicit_functions')
2465 m.piecewise_functions = createModule('piecewise_functions')
2466 m.extended_sources = createModule("extended_sources")
2467 m.misc = createModule("misc")
2468 createModule("incremental_point_locators", m.misc)
2470 class PVModule(object):
2473 def _make_name_valid(name):
2474 """Make a string into a valid Python variable name."""
2478 valid_chars = "_%s%s" % (string.ascii_letters, string.digits)
2479 name = str().join([c for c in name if c in valid_chars])
2480 if not name[0].isalpha():
2484 def createModule(groupName, mdl=None):
2485 """Populates a module with proxy classes defined in the given group.
2486 If mdl is not specified, it also creates the module"""
2487 global ActiveConnection
2489 if not ActiveConnection:
2490 raise RuntimeError, "Please connect to a server using \"Connect\""
2492 pxm = ProxyManager()
2493 # Use prototypes to find all proxy types.
2494 pxm.InstantiateGroupPrototypes(groupName)
2500 definitionIter = pxm.NewDefinitionIterator(groupName)
2501 for i in definitionIter:
2502 proxyName = i['key']
2503 proto = pxm.GetPrototypeProxy(groupName, proxyName)
2505 print "Error while loading %s/%s %s"%(groupName, i['group'], proxyName)
2508 if proto.GetXMLLabel():
2509 pname = proto.GetXMLLabel()
2510 pname = _make_name_valid(pname)
2513 if pname in mdl.__dict__:
2515 print "Warning: %s is being overwritten. This may point to an issue in the ParaView configuration files" % pname
2517 # Create an Initialize() method for this sub-class.
2518 cdict['Initialize'] = _createInitialize(groupName, proxyName)
2519 iter = PropertyIterator(proto)
2520 # Add all properties as python properties.
2522 propName = iter.GetKey()
2523 if (prop.GetInformationOnly() and propName != "TimestepValues" ) \
2524 or prop.GetIsInternal():
2527 names = [iter.PropertyLabel]
2530 if prop.GetDocumentation():
2531 propDoc = prop.GetDocumentation().GetDescription()
2533 name = _make_name_valid(name)
2535 cdict[name] = property(_createGetProperty(propName),
2536 _createSetProperty(propName),
2539 # Add the documentation as the class __doc__
2540 if proto.GetDocumentation() and \
2541 proto.GetDocumentation().GetDescription():
2542 doc = proto.GetDocumentation().GetDescription()
2545 cdict['__doc__'] = doc
2546 # Create the new type
2547 if proto.GetXMLName() == "ExodusIIReader":
2548 superclasses = (ExodusIIReaderProxy,)
2549 elif proto.IsA("vtkSMSourceProxy"):
2550 superclasses = (SourceProxy,)
2551 elif proto.IsA("vtkSMViewLayoutProxy"):
2552 superclasses = (ViewLayoutProxy,)
2554 superclasses = (Proxy,)
2556 cobj = type(pname, superclasses, cdict)
2557 # Add it to the modules dictionary
2558 mdl.__dict__[pname] = cobj
2562 def __determineGroup(proxy):
2563 """Internal method"""
2566 xmlgroup = proxy.GetXMLGroup()
2567 xmlname = proxy.GetXMLName()
2568 if xmlgroup == "sources":
2569 if xmlname in ["BlockSelectionSource",
2570 "FrustumSelectionSource",
2571 "GlobalIDSelectionSource",
2572 "PedigreeIDSelectionSource",
2573 "IDSelectionSource",
2574 "CompositeDataIDSelectionSource",
2575 "HierarchicalDataIDSelectionSource",
2576 "ThresholdSelectionSource",
2577 "LocationSelectionSource"]:
2578 return "selection_sources"
2580 elif xmlgroup == "filters":
2582 elif xmlgroup == "representations":
2583 if xmlname == "ScalarBarWidgetRepresentation":
2584 return "scalar_bars"
2585 return "representations"
2586 elif xmlgroup == "animation_keyframes":
2591 def __determineName(proxy, group):
2592 global __nameCounter
2593 name = _make_name_valid(proxy.GetXMLLabel())
2596 if not __nameCounter.has_key(name):
2597 __nameCounter[name] = 1
2600 __nameCounter[name] += 1
2601 val = __nameCounter[name]
2602 return "%s%d" % (name, val)
2604 def __getName(proxy, group):
2605 pxm = ProxyManager(proxy.GetSession())
2606 if isinstance(proxy, Proxy):
2607 proxy = proxy.SMProxy
2608 return pxm.GetProxyName(group, proxy)
2610 class MissingRegistrationInformation(Exception):
2611 """Exception for missing registration information. Raised when a name or group
2612 is not specified or when a group cannot be deduced."""
2615 class MissingProxy(Exception):
2616 """Exception fired when the requested proxy is missing."""
2619 def Register(proxy, **extraArgs):
2620 """Registers a proxy with the proxy manager. If no 'registrationGroup' is
2621 specified, then the group is inferred from the type of the proxy.
2622 'registrationName' may be specified to register with a particular name
2623 otherwise a default name will be created."""
2624 # TODO: handle duplicate registration
2625 if "registrationGroup" in extraArgs:
2626 registrationGroup = extraArgs["registrationGroup"]
2628 registrationGroup = __determineGroup(proxy)
2630 if "registrationName" in extraArgs:
2631 registrationName = extraArgs["registrationName"]
2633 registrationName = __determineName(proxy, registrationGroup)
2634 if registrationGroup and registrationName:
2635 pxm = ProxyManager()
2636 pxm.RegisterProxy(registrationGroup, registrationName, proxy)
2638 raise MissingRegistrationInformation, "Registration error %s %s." % (registrationGroup, registrationName)
2639 return (registrationGroup, registrationName)
2641 def UnRegister(proxy, **extraArgs):
2642 """UnRegisters proxies registered using Register()."""
2643 if "registrationGroup" in extraArgs:
2644 registrationGroup = extraArgs["registrationGroup"]
2646 registrationGroup = __determineGroup(proxy)
2648 if "registrationName" in extraArgs:
2649 registrationName = extraArgs["registrationName"]
2651 registrationName = __getName(proxy, registrationGroup)
2653 if registrationGroup and registrationName:
2654 pxm = ProxyManager()
2655 pxm.UnRegisterProxy(registrationGroup, registrationName, proxy)
2657 raise RuntimeError, "UnRegistration error."
2658 return (registrationGroup, registrationName)
2661 """This simple demonstration creates a sphere, renders it and delivers
2662 it to the client using Fetch. It returns a tuple of (data, render
2664 if not ActiveConnection:
2666 ss = sources.Sphere(Radius=2, ThetaResolution=32)
2667 shr = filters.Shrink(Input=OutputPort(ss,0))
2669 app = filters.AppendDatasets()
2670 app.Input = [shr, cs]
2671 rv = CreateRenderView()
2672 rep = CreateRepresentation(app, rv)
2679 def demo2(fname="/Users/berk/Work/ParaViewData/Data/disk_out_ref.ex2"):
2680 """This method demonstrates the user of a reader, representation and
2681 view. It also demonstrates how meta-data can be obtained using proxies.
2682 Make sure to pass the full path to an exodus file. Also note that certain
2683 parameters are hard-coded for disk_out_ref.ex2 which can be found
2684 in ParaViewData. This method returns the render view."""
2685 if not ActiveConnection:
2687 # Create the exodus reader and specify a file name
2688 reader = sources.ExodusIIReader(FileName=fname)
2689 # Get the list of point arrays.
2690 arraySelection = reader.PointVariables
2691 print arraySelection.Available
2693 arraySelection.SetData(arraySelection.Available)
2695 # Next create a default render view appropriate for the session type.
2696 rv = CreateRenderView()
2697 # Create the matching representation
2698 rep = CreateRepresentation(reader, rv)
2699 rep.Representation = 1 # Wireframe
2700 # Black background is not pretty
2701 rv.Background = [0.4, 0.4, 0.6]
2703 # Reset the camera to include the whole thing
2706 # Change the elevation of the camera. See VTK documentation of vtkCamera
2707 # for camera parameters.
2708 c = rv.GetActiveCamera()
2711 # Now that the reader execute, let's get some information about it's
2713 pdi = reader[0].PointData
2714 # This prints a list of all read point data arrays as well as their
2716 print 'Number of point arrays:', len(pdi)
2717 for i in range(len(pdi)):
2719 print "----------------"
2720 print "Array:", i, ai.Name, ":"
2721 numComps = ai.GetNumberOfComponents()
2722 print "Number of components:", numComps
2723 for j in range(numComps):
2724 print "Range:", ai.GetRange(j)
2725 # White is boring. Let's color the geometry using a variable.
2726 # First create a lookup table. This object controls how scalar
2727 # values are mapped to colors. See VTK documentation for
2729 lt = rendering.PVLookupTable()
2730 # Assign it to the representation
2731 rep.LookupTable = lt
2732 # Color by point array called Pres
2733 rep.ColorAttributeType = 0 # point data
2734 rep.ColorArrayName = "Pres"
2735 # Add to RGB points. These are tuples of 4 values. First one is
2736 # the scalar values, the other 3 the RGB values. This list has
2737 # 2 points: Pres: 0.00678, color: blue, Pres: 0.0288, color: red
2738 lt.RGBPoints = [0.00678, 0, 0, 1, 0.0288, 1, 0, 0]
2739 lt.ColorSpace = 1 # HSV
2744 """This method demonstrates the use of servermanager with numpy as
2745 well as pylab for plotting. It creates an artificial data sources,
2746 probes it with a line, delivers the result to the client using Fetch
2747 and plots it using pylab. This demo requires numpy and pylab installed.
2748 It returns a tuple of (data, render view)."""
2749 import paraview.numpy_support
2752 if not ActiveConnection:
2754 # Create a synthetic data source
2755 source = sources.Wavelet()
2756 # Let's get some information about the data. First, for the
2758 source.UpdatePipeline()
2760 di = source.GetDataInformation()
2761 print "Data type:", di.GetPrettyDataTypeString()
2762 print "Extent:", di.GetExtent()
2763 print "Array name:", \
2764 source[0].PointData[0].Name
2766 rv = CreateRenderView()
2768 rep1 = CreateRepresentation(source, rv)
2769 rep1.Representation = 3 # outline
2771 # Let's apply a contour filter
2772 cf = filters.Contour(Input=source, ContourValues=[200])
2774 # Select the array to contour by
2775 #cf.SelectInputScalars = 'RTData'
2777 rep2 = CreateRepresentation(cf, rv)
2779 rv.Background = (0.4, 0.4, 0.6)
2780 # Reset the camera to include the whole thing
2785 # Now, let's probe the data
2786 probe = filters.ResampleWithDataset(Input=source)
2788 line = sources.Line(Resolution=60)
2789 # that spans the dataset
2790 bounds = di.GetBounds()
2791 print "Bounds: ", bounds
2792 line.Point1 = bounds[0:6:2]
2793 line.Point2 = bounds[1:6:2]
2797 # Render with the line
2798 rep3 = CreateRepresentation(line, rv)
2801 # Now deliver it to the client. Remember, this is for small data.
2803 # Convert it to a numpy array
2804 data = paraview.numpy_support.vtk_to_numpy(
2805 data.GetPointData().GetArray("RTData"))
2806 # Plot it using matplotlib
2810 return (data, rv, probe)
2812 def demo4(fname="/Users/berk/Work/ParaViewData/Data/can.ex2"):
2813 """This method demonstrates the user of AnimateReader for
2814 creating animations."""
2815 if not ActiveConnection:
2817 reader = sources.ExodusIIReader(FileName=fname)
2818 view = CreateRenderView()
2819 repr = CreateRepresentation(reader, view)
2823 c = view.GetActiveCamera()
2825 return AnimateReader(reader, view)
2829 """ Simple sphere animation"""
2830 if not ActiveConnection:
2832 sphere = sources.Sphere()
2833 view = CreateRenderView()
2834 repr = CreateRepresentation(sphere, view)
2840 # Create an animation scene
2841 scene = animation.AnimationScene()
2843 scene.ViewModules = [view]
2845 # Create a cue to animate the StartTheta property
2846 cue = animation.KeyFrameAnimationCue()
2847 cue.AnimatedProxy = sphere
2848 cue.AnimatedPropertyName = "StartTheta"
2849 # Add it to the scene's cues
2852 # Create 2 keyframes for the StartTheta track
2853 keyf0 = animation.CompositeKeyFrame()
2854 keyf0.Type = 2 # Set keyframe interpolation type to Ramp.
2855 # At time = 0, value = 0
2857 keyf0.KeyValues= [0]
2859 keyf1 = animation.CompositeKeyFrame()
2860 # At time = 1.0, value = 200
2862 keyf1.KeyValues= [200]
2865 cue.KeyFrames = [keyf0, keyf1]
2870 ASSOCIATIONS = { 'POINTS' : 0, 'CELLS' : 1, 'VERTICES' : 4, 'EDGES' : 5, 'ROWS' : 6}
2872 # Users can set the active connection which will be used by API
2873 # to create proxies etc when no connection argument is passed.
2874 # Connect() automatically sets this if it is not already set.
2875 ActiveConnection = None
2877 # Fields for multi-server support
2878 MultiServerConnections = None
2880 # API for multi-server support
2881 def enableMultiServer(multiServer=True):
2882 """This method enable the current servermanager to support several
2883 connections. Once we enable the multi-server support, the user can create
2884 as many connection as he want and switch from one to another in order to
2885 create and manage proxy."""
2886 global MultiServerConnections, ActiveConnection
2887 if not multiServer and MultiServerConnections:
2888 raise RuntimeError, "Once we enable Multi-Server support we can not get back"
2889 MultiServerConnections = []
2890 if ActiveConnection:
2891 MultiServerConnections.append(ActiveConnection)
2893 def switchActiveConnection(newActiveConnection=None):
2894 """Switch active connection to be the provided one or if none just pick the
2896 global MultiServerConnections, ActiveConnection
2897 if MultiServerConnections == None:
2898 raise RuntimeError, "enableMultiServer() must be called before"
2900 # Manage the case when no connection is provided
2901 if newActiveConnection:
2902 ActiveConnection = newActiveConnection
2903 __exposeActiveModules__()
2904 # Update active session for ParaView
2905 if vtkSMProxyManager.GetProxyManager().GetActiveSession() != ActiveConnection.Session:
2906 vtkSMProxyManager.GetProxyManager().SetActiveSession(ActiveConnection.Session)
2907 return ActiveConnection
2909 for connection in MultiServerConnections:
2910 if connection != ActiveConnection:
2911 ActiveConnection = connection
2912 __exposeActiveModules__()
2913 # Update active session for ParaView
2914 if vtkSMProxyManager.GetProxyManager().GetActiveSession() != ActiveConnection.Session:
2915 vtkSMProxyManager.GetProxyManager().SetActiveSession(ActiveConnection.Session)
2916 return ActiveConnection
2919 # Needs to be called when paraview module is loaded from python instead
2920 # of pvpython, pvbatch or GUI.
2921 if not vtkProcessModule.GetProcessModule():
2922 # pvoptions = None Not applicable for SALOME Python console
2923 # if paraview.options.batch:
2924 # pvoptions = vtkPVOptions();
2925 # pvoptions.SetProcessType(0x40)
2926 # if paraview.options.symmetric:
2927 # pvoptions.SetSymmetricMPIMode(True)
2928 vtkInitializationHelper.Initialize(sys.executable,
2929 vtkProcessModule.PROCESS_CLIENT, pvoptions)
2931 # Initialize progress printing. Can be turned off by calling
2932 # ToggleProgressPrinting() again.
2933 progressObserverTag = None
2934 currentAlgorithm = False
2937 ToggleProgressPrinting()
2941 # Create needed sub-modules
2942 # We can no longer create modules, unless we have connected to a server.
2945 # Set up our custom importer (if possible)
2946 loader = _ModuleLoader()
2947 sys.meta_path.append(loader)
2949 def __InitAfterConnect__(connection):
2951 This function is called everytime after a server connection is made.
2952 Since the ProxyManager and all proxy definitions are changed every time a
2953 new connection is made, we re-create all the modules
2955 _createModules(connection.Modules)
2956 ## VSV fromFilter is alwais False for SALOME because it can't be changed from ParaView code
2957 #if not paraview.fromFilter:
2958 # fromFilter is set when this module is imported from the programmable
2960 # global _defUpdater
2961 # _defUpdater = __DefinitionUpdater()
2962 connection.AttachDefinitionUpdater()
2965 def __exposeActiveModules__():
2966 """Update servermanager submodules to point to the current
2967 ActiveConnection.Modules.*"""
2968 # Expose all active module to the current servermanager module
2969 if ActiveConnection:
2970 for m in [mName for mName in dir(ActiveConnection.Modules) if mName[0] != '_' ]:
2971 exec "global %s;%s = ActiveConnection.Modules.%s" % (m,m,m)
2973 # Definitions for working in SALOME GUI mode
2974 #aParams = myParavis.GetConnectionParameters()
2975 #ActiveConnection = Connect()
2976 ##Connection(aParams[0])
2977 #ActiveConnection.SetHost(aParams[1], aParams[2], aParams[3], aParams[4], aParams[5])
2978 #ToggleProgressPrinting()
2983 if hasattr(sys, "ps1"):
2984 # session is interactive.
2985 print vtkSMProxyManager.GetParaViewSourceVersion();
2987 def GetConnectionFromId(id):
2988 for connection in MultiServerConnections:
2989 if connection.ID == id:
2993 def GetConnectionFromSession(session):
2994 for connection in MultiServerConnections:
2995 if connection.Session == session:
2999 def GetConnectionAt(index):
3000 return MultiServerConnections[index]
3002 def GetNumberOfConnections():
3003 return len(MultiServerConnections)