1 # Copyright (C) 2010-2014 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, or (at your option) any later version.
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
54 # VTN: Avoid paraview.* instructions in this file.
55 # It leads to problems during execution.
57 def _wrap_property(proxy, smproperty):
58 """ Internal function.
59 Given a server manager property and its domains, returns the
60 appropriate python object.
63 if smproperty.IsA("vtkSMStringVectorProperty"):
64 al = smproperty.GetDomain("array_list")
65 if al and al.IsA("vtkSMArraySelectionDomain") and \
66 smproperty.GetRepeatable():
67 property = ArrayListProperty(proxy, smproperty)
68 elif al and al.IsA("vtkSMArrayListDomain") and smproperty.GetNumberOfElements() == 5:
69 property = ArraySelectionProperty(proxy, smproperty)
71 iter = smproperty.NewDomainIterator()
73 while not iter.IsAtEnd():
74 # Refer to BUG #9710 to see why optional domains need to be
76 if iter.GetDomain().IsA("vtkSMFileListDomain") and \
77 iter.GetDomain().GetIsOptional() == 0 :
83 property = FileNameProperty(proxy, smproperty)
84 elif _make_name_valid(smproperty.GetXMLLabel()) == 'ColorArrayName':
85 property = ColorArrayProperty(proxy, smproperty)
87 property = VectorProperty(proxy, smproperty)
88 elif smproperty.IsA("vtkSMVectorProperty"):
89 if smproperty.IsA("vtkSMIntVectorProperty") and \
90 smproperty.GetDomain("enum"):
91 property = EnumerationProperty(proxy, smproperty)
93 property = VectorProperty(proxy, smproperty)
94 elif smproperty.IsA("vtkSMInputProperty"):
95 property = InputProperty(proxy, smproperty)
96 elif smproperty.IsA("vtkSMProxyProperty"):
97 property = ProxyProperty(proxy, smproperty)
99 property = Property(proxy, smproperty)
103 """Proxy for a server side object. A proxy manages the lifetime of
104 one or more server manager objects. It also provides an interface
105 to set and get the properties of the server side objects. These
106 properties are presented as Python properties. For example,
107 you can set a property Foo using the following:
110 proxy.Foo.SetData((1,2))
112 proxy.Foo[0:2] = (1,2)
113 For more information, see the documentation of the property which
117 This class also provides an iterator which can be used to iterate
120 proxy = Proxy(proxy=smproxy)
121 for property in proxy:
125 This is a python class that wraps a vtkSMProxy.. Makes it easier to
128 proxy.GetProperty("Foo").SetElement(0, 1)
129 proxy.GetProperty("Foo").SetElement(0, 2)
133 proxy.Foo.SetData((1,2))
135 proxy.Foo[0:2] = (1,2)
137 proxy.GetProperty("Foo").GetElement(0)
139 proxy.Foo.GetData()[0]
142 For proxy properties, you can use append:
143 proxy.GetProperty("Bar").AddProxy(foo)
145 proxy.Bar.append(foo)
146 Properties support most of the list API. See VectorProperty and
147 ProxyProperty documentation for details.
149 Please note that some of the methods accessible through the Proxy
150 class are not listed by help() because the Proxy objects forward
151 unresolved attributes to the underlying object. To get the full list,
152 see also dir(proxy.SMProxy). See also the doxygen based documentation
153 of the vtkSMProxy C++ class.
156 def __init__(self, **args):
157 """ Default constructor. It can be used to initialize properties
158 by passing keyword arguments where the key is the name of the
159 property. In addition registrationGroup and registrationName (optional)
160 can be specified (as keyword arguments) to automatically register
161 the proxy with the proxy manager. """
162 self.add_attribute('Observed', None)
163 self.add_attribute('ObserverTag', -1)
164 self.add_attribute('_Proxy__Properties', {})
165 self.add_attribute('_Proxy__LastAttrName', None)
166 self.add_attribute('SMProxy', None)
167 self.add_attribute('Port', 0)
170 self.Port = args['port']
174 if 'no_update' in args:
175 if args['no_update']:
177 del args['no_update']
180 self.InitializeFromProxy(args['proxy'])
183 self.Initialize(None, update)
184 if 'registrationGroup' in args:
185 registrationGroup = args['registrationGroup']
186 del args['registrationGroup']
187 registrationName = self.SMProxy.GetGlobalIDAsString()
188 if 'registrationName' in args:
189 registrationName = args['registrationName']
190 del args['registrationName']
192 pxm.RegisterProxy(registrationGroup, registrationName, self.SMProxy)
194 self.UpdateVTKObjects()
195 for key in args.keys():
196 setattr(self, key, args[key])
197 # Visit all properties so that they are created
201 def __setattr__(self, name, value):
203 setter = getattr(self.__class__, name)
204 setter = setter.__set__
205 except AttributeError:
206 if not hasattr(self, name):
207 raise AttributeError("Attribute %s does not exist. " % name +
208 " This class does not allow addition of new attributes to avoid " +
209 "mistakes due to typos. Use add_attribute() if you really want " +
210 "to add this attribute.")
211 self.__dict__[name] = value
215 def add_attribute(self, name, value):
216 self.__dict__[name] = value
219 """Destructor. Cleans up all observers as well as remove
220 the proxy from the _pyproxies dictionary"""
221 # Make sure that we remove observers we added
223 observed = self.Observed
224 tag = self.ObserverTag
226 self.ObserverTag = -1
227 observed.RemoveObserver(tag)
228 if _pyproxies and self.SMProxy and (self.SMProxy, self.Port) in _pyproxies:
229 del _pyproxies[(self.SMProxy, self.Port)]
231 def InitializeFromProxy(self, aProxy, update=True):
232 """Constructor. Assigns proxy to self.SMProxy, updates the server
233 object as well as register the proxy in _pyproxies dictionary."""
235 self.SMProxy = aProxy
237 self.SMProxy.UpdateVTKObjects()
238 _pyproxies[(self.SMProxy, self.Port)] = weakref.ref(self)
240 def Initialize(self):
241 "Overridden by the subclass created automatically"
244 def __eq__(self, other):
245 "Returns true if the underlying SMProxies are the same."
246 if isinstance(other, Proxy):
248 if self.Port != other.Port:
252 ## VSV using IsSame instead ==
253 return self.SMProxy.IsSame(other.SMProxy)
254 return self.SMProxy.IsSame(other)
256 def __ne__(self, other):
257 "Returns false if the underlying SMProxies are the same."
258 return not self.__eq__(other)
261 "Creates an iterator for the properties."
262 return PropertyIterator(self)
264 def SetPropertyWithName(self, pname, arg):
265 """Generic method for setting the value of a property."""
266 prop = self.GetProperty(pname)
268 raise RuntimeError, "Property %s does not exist. Please check the property name for typos." % pname
271 def GetPropertyValue(self, name):
272 """Returns a scalar for properties with 1 elements, the property
273 itself for vectors."""
274 p = self.GetProperty(name)
275 if isinstance(p, VectorProperty):
276 if p.GetNumberOfElements() == 1 and not p.GetRepeatable():
277 if p.SMProperty.IsA("vtkSMStringVectorProperty") or not p.GetArgumentIsArray():
279 elif isinstance(p, InputProperty):
280 if not p.GetMultipleInput():
285 elif isinstance(p, ProxyProperty):
286 if not p.GetRepeatable():
293 def GetProperty(self, name):
294 """Given a property name, returns the property object."""
295 if name in self.__Properties and self.__Properties[name]():
296 return self.__Properties[name]()
297 smproperty = self.SMProxy.GetProperty(name)
298 # Maybe they are looking by the label. Try to match that.
300 iter = PropertyIterator(self)
302 if name == _make_name_valid(iter.PropertyLabel):
303 smproperty = prop.SMProperty
306 property = _wrap_property(self, smproperty)
307 if property is not None:
309 self.__Properties[name] = weakref.ref(property)
313 def ListProperties(self):
314 """Returns a list of all property names on this proxy."""
316 iter = self.__iter__()
317 for property in iter:
318 name = _make_name_valid(iter.PropertyLabel)
320 property_list.append(name)
323 def __ConvertArgumentsAndCall(self, *args):
324 """ Internal function.
325 Used to call a function on SMProxy. Converts input and
326 output values as appropriate.
330 if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
331 newArgs.append(arg.SMProxy)
334 func = getattr(self.SMProxy, self.__LastAttrName)
335 retVal = func(*newArgs)
336 if type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProxy"):
337 return _getPyProxy(retVal)
338 elif type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProperty"):
339 return _wrap_property(self, retVal)
343 def __GetActiveCamera(self):
344 """ This method handles GetActiveCamera specially. It adds
345 an observer to the camera such that everytime it is modified
346 the render view updated"""
348 c = self.SMProxy.GetActiveCamera()
349 # VSV: Observers are not supported
350 ## if not c.HasObserver("ModifiedEvent"):
351 ## self.ObserverTag =c.AddObserver("ModifiedEvent", _makeUpdateCameraMethod(weakref.ref(self)))
355 def __getattr__(self, name):
356 """With the exception of a few overloaded methods,
357 returns the SMProxy method"""
359 raise AttributeError("class %s has no attribute %s" % ("None", name))
361 # Handle GetActiveCamera specially.
362 if name == "GetActiveCamera" and \
363 hasattr(self.SMProxy, "GetActiveCamera"):
364 return self.__GetActiveCamera
365 if name == "SaveDefinition" and hasattr(self.SMProxy, "SaveDefinition"):
366 return self.__SaveDefinition
367 # If not a property, see if SMProxy has the method
369 proxyAttr = getattr(self.SMProxy, name)
370 self.__LastAttrName = name
371 return self.__ConvertArgumentsAndCall
374 return getattr(self.SMProxy, name)
376 class SourceProxy(Proxy):
377 """Proxy for a source object. This class adds a few methods to Proxy
378 that are specific to sources. It also provides access to the output
379 ports. Output ports can be accessed by name or index:
382 > op = source['some name'].
384 def UpdatePipeline(self, time=None):
385 """This method updates the server-side VTK pipeline and the associated
386 data information. Make sure to update a source to validate the output
389 self.SMProxy.UpdatePipeline(time)
391 self.SMProxy.UpdatePipeline()
392 # This is here to cause a receive
393 # on the client side so that progress works properly.
394 if ActiveConnection and ActiveConnection.IsRemote():
395 self.SMProxy.GetDataInformation()
397 def FileNameChanged(self):
398 "Called when the filename of a source proxy is changed."
399 self.UpdatePipelineInformation()
401 def UpdatePipelineInformation(self):
402 """This method updates the meta-data of the server-side VTK pipeline and
403 the associated information properties"""
404 self.SMProxy.UpdatePipelineInformation()
406 def GetDataInformation(self, idx=None):
407 """This method returns a DataInformation wrapper around a
408 vtkPVDataInformation"""
412 return DataInformation( \
413 self.SMProxy.GetDataInformation(idx), \
416 def __getitem__(self, idx):
417 """Given a slice, int or string, returns the corresponding
419 if isinstance(idx, slice):
420 indices = idx.indices(self.SMProxy.GetNumberOfOutputPorts())
422 for i in range(*indices):
423 retVal.append(OutputPort(self, i))
425 elif isinstance(idx, int):
426 if idx >= self.SMProxy.GetNumberOfOutputPorts() or idx < 0:
428 return OutputPort(self, idx)
430 return OutputPort(self, self.SMProxy.GetOutputPortIndex(idx))
432 def GetPointDataInformation(self):
433 """Returns the associated point data information."""
434 self.UpdatePipeline()
435 return FieldDataInformation(self.SMProxy, self.Port, "PointData")
437 def GetCellDataInformation(self):
438 """Returns the associated cell data information."""
439 self.UpdatePipeline()
440 return FieldDataInformation(self.SMProxy, self.Port, "CellData")
442 def GetFieldDataInformation(self):
443 """Returns the associated cell data information."""
444 self.UpdatePipeline()
445 return FieldDataInformation(self.SMProxy, self.Port, "FieldData")
447 PointData = property(GetPointDataInformation, None, None, "Returns point data information")
448 CellData = property(GetCellDataInformation, None, None, "Returns cell data information")
449 FieldData = property(GetFieldDataInformation, None, None, "Returns field data information")
452 class ExodusIIReaderProxy(SourceProxy):
453 """Special class to define convenience functions for array
456 def FileNameChanged(self):
457 "Called when the filename changes. Selects all variables."
458 SourceProxy.FileNameChanged(self)
459 self.SelectAllVariables()
461 def SelectAllVariables(self):
462 "Select all available variables for reading."
463 for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
464 'ElementVariables', 'GlobalVariables'):
465 f = getattr(self, prop)
468 def DeselectAllVariables(self):
469 "Deselects all variables."
470 for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
471 'ElementVariables', 'GlobalVariables'):
472 f = getattr(self, prop)
475 class ViewLayoutProxy(Proxy):
476 """Special class to define convenience methods for View Layout"""
478 def SplitViewHorizontal(self, view, fraction=0.5):
479 """Split the cell containing the specified view horizontally.
480 If no fraction is specified, the frame is split into equal parts.
481 On success returns a positve number that identifying the new cell
482 location that can be used to assign view to, or split further.
483 Return -1 on failure."""
484 location = self.GetViewLocation(view)
486 raise RuntimeError, "View is not present in this layout."
487 if fraction < 0.0 or fraction > 1.0:
488 raise RuntimeError, "'fraction' must be in the range [0.0, 1.0]"
489 return self.SMProxy.SplitHorizontal(location, fraction)
491 def SplitViewVertical(self, view=None, fraction=0.5):
492 """Split the cell containing the specified view horizontally.
493 If no view is specified, active view is used.
494 If no fraction is specified, the frame is split into equal parts.
495 On success returns a positve number that identifying the new cell
496 location that can be used to assign view to, or split further.
497 Return -1 on failure."""
498 location = self.GetViewLocation(view)
500 raise RuntimeError, "View is not present in this layout."
501 if fraction < 0.0 or fraction > 1.0:
502 raise RuntimeError, "'fraction' must be in the range [0.0, 1.0]"
503 return self.SMProxy.SplitVertical(location, fraction)
505 def AssignView(self, location, view):
506 """Assign a view at a particular location. Note that the view's position may
507 be changed by subsequent Split() calls. Returns true on success."""
509 if isinstance(view, Proxy):
511 return self.SMProxy.AssignView(location, view)
513 def GetViewLocation(self, view):
514 if isinstance(view, Proxy):
516 return self.SMProxy.GetViewLocation(view)
518 class Property(object):
519 """Generic property object that provides access to one of the properties of
520 a server object. This class does not allow setting/getting any values but
521 provides an interface to update a property using __call__. This can be used
522 for command properties that correspond to function calls without arguments.
525 would push a Foo property which may cause the proxy to call a Foo method
526 on the actual VTK object.
529 Python wrapper around a vtkSMProperty with a simple interface.
530 In addition to all method provided by vtkSMProperty (obtained by
531 forwarding unknown attributes requests to the underlying SMProxy),
532 Property and sub-class provide a list API.
534 Please note that some of the methods accessible through the Property
535 class are not listed by help() because the Property objects forward
536 unresolved attributes to the underlying object. To get the full list,
537 see also dir(proxy.SMProperty). See also the doxygen based documentation
538 of the vtkSMProperty C++ class.
540 def __init__(self, proxy, smproperty):
541 """Default constructor. Stores a reference to the proxy."""
543 self.SMProperty = smproperty
547 """Returns a string representation containing property name
549 if not type(self) is Property:
550 if self.GetData() is not None:
551 repr = self.GetData().__repr__()
555 repr = "Property name= "
556 name = self.Proxy.GetPropertyName(self.SMProperty)
565 """Forces a property update using InvokeCommand."""
566 if type(self) is Property:
567 self.Proxy.SMProxy.InvokeCommand(self._FindPropertyName())
569 raise RuntimeError, "Cannot invoke this property"
571 def _FindPropertyName(self):
572 "Returns the name of this property."
573 return self.Proxy.GetPropertyName(self.SMProperty)
575 def _UpdateProperty(self):
576 "Pushes the value of this property to the server."
577 # For now, we are updating all properties. This is due to an
578 # issue with the representations. Their VTK objects are not
579 # created until Input is set therefore, updating a property
580 # has no effect. Updating all properties everytime one is
581 # updated has the effect of pushing values set before Input
582 # when Input is updated.
583 # self.Proxy.SMProxy.UpdateProperty(self._FindPropertyName())
584 self.Proxy.SMProxy.UpdateVTKObjects()
586 def __getattr__(self, name):
587 "Unknown attribute requests get forwarded to SMProperty."
588 return getattr(self.SMProperty, name)
590 Name = property(_FindPropertyName, None, None,
591 "Returns the name for the property")
593 class GenericIterator(object):
594 """Iterator for container type objects"""
596 def __init__(self, obj):
604 if self.index >= len(self.Object):
609 return self.Object[idx]
611 class VectorProperty(Property):
612 """A VectorProperty provides access to one or more values. You can use
613 a slice to get one or more property values:
616 > vals = property[0:5:2]
617 You can use a slice to set one or more property values:
620 > property[1:3] = (1,2)
622 def ConvertValue(self, value):
626 """Returns the number of elements."""
627 return self.SMProperty.GetNumberOfElements()
630 """Implementation of the sequence API"""
631 return GenericIterator(self)
633 def __setitem__(self, idx, value):
634 """Given a list or tuple of values, sets a slice of values [min, max)"""
635 if isinstance(idx, slice):
636 indices = idx.indices(len(self))
637 for i, j in zip(range(*indices), value):
638 self.SMProperty.SetElement(i, self.ConvertValue(j))
639 self._UpdateProperty()
640 elif idx >= len(self) or idx < 0:
643 self.SMProperty.SetElement(idx, self.ConvertValue(value))
644 self._UpdateProperty()
646 def GetElement(self, index):
647 return self.SMProperty.GetElement(index)
649 def __getitem__(self, idx):
650 """Returns the range [min, max) of elements. Raises an IndexError
651 exception if an argument is out of bounds."""
653 if isinstance(idx, slice):
654 indices = idx.indices(ls)
656 for i in range(*indices):
657 retVal.append(self.GetElement(i))
666 return self.GetElement(idx)
669 "Returns all elements as either a list or a single value."
670 property = self.SMProperty
671 if property.GetRepeatable() or \
672 property.GetNumberOfElements() > 1:
673 return self[0:len(self)]
674 elif property.GetNumberOfElements() == 1:
675 return self.GetElement(0)
677 def SetData(self, values):
678 """Allows setting of all values at once. Requires a single value or
679 a iterable object."""
680 if not hasattr(values, "__iter__"):
682 if not self.GetRepeatable() and len(values) != self.GetNumberOfElements():
683 raise RuntimeError("This property requires %d values." % self.GetNumberOfElements())
684 if self.GetRepeatable():
686 self.SMProperty.SetNumberOfElements(0)
689 self.SMProperty.SetElement(idx, self.ConvertValue(val))
691 self._UpdateProperty()
694 "Removes all elements."
695 self.SMProperty().SetNumberOfElements(0)
696 self._UpdateProperty()
698 class ColorArrayProperty(VectorProperty):
699 """This subclass of VectorProperty handles setting of the array to
700 color by. It handles attribute type as well as well array name."""
702 def GetAvailable(self):
703 """Returns the list of available arrays as (attribute type, array name
706 for a in self.Proxy.Input.PointData:
707 arrays.append(('POINT_DATA', a.GetName()))
708 for a in self.Proxy.Input.CellData:
709 arrays.append(('CELL_DATA', a.GetName()))
712 def SetData(self, value):
713 """Overwritten to enable setting attribute type (the ColorAttributeType
714 property and the array name. The argument should be the array name
715 (in which case the first appropriate attribute type is picked) or
716 a tuple of attribute type and array name."""
717 if isinstance(value, tuple) and len(value) == 2:
720 elif isinstance(value, str):
724 raise ValueError("Expected a tuple of 2 values or a string.")
727 self.SMProperty.SetElement(0, '')
728 self._UpdateProperty()
732 for a in self.Available:
733 if a[1] == arr and (not att or att == a[0]):
739 pvoptions = vtkProcessModule.GetProcessModule().GetOptions()
740 # if this process is from a parallel batch run in symmetric mpi mode
741 # then we may not have any points or cells on some processes in which
742 # case we'll probably be missing the point and cell data too. the
743 # check below makes sure that we avoid this situation.
744 if pvoptions.GetProcessType() != 0x40 or pvoptions.GetSymmetricMPIMode() == False \
745 or len(self.Available) != 0:
746 raise ValueError("Could not locate array %s in the input." % arr)
748 catt = self.Proxy.GetProperty("ColorAttributeType")
751 self.SMProperty.SetElement(0, arr)
752 self._UpdateProperty()
754 Available = property(GetAvailable, None, None, \
755 "This read-only property returns the list of arrays that can be colored by.")
758 class EnumerationProperty(VectorProperty):
759 """Subclass of VectorProperty that is applicable for enumeration type
762 def GetElement(self, index):
763 """Returns the text for the given element if available. Returns
764 the numerical values otherwise."""
765 val = self.SMProperty.GetElement(index)
766 domain = self.SMProperty.GetDomain("enum")
767 for i in range(domain.GetNumberOfEntries()):
768 if domain.GetEntryValue(i) == val:
769 return domain.GetEntryText(i)
772 def ConvertValue(self, value):
773 """Converts value to type suitable for vtSMProperty::SetElement()"""
774 if type(value) == str:
775 domain = self.SMProperty.GetDomain("enum")
776 if domain.HasEntryText(value):
777 return domain.GetEntryValueForText(value)
779 raise ValueError("%s is not a valid value." % value)
780 return VectorProperty.ConvertValue(self, value)
782 def GetAvailable(self):
783 "Returns the list of available values for the property."
785 domain = self.SMProperty.GetDomain("enum")
786 for i in range(domain.GetNumberOfEntries()):
787 retVal.append(domain.GetEntryText(i))
790 Available = property(GetAvailable, None, None, \
791 "This read-only property contains the list of values that can be applied to this property.")
794 class FileNameProperty(VectorProperty):
795 """Property to set/get one or more file names.
796 This property updates the pipeline information everytime its value changes.
797 This is used to keep the array lists up to date."""
799 def _UpdateProperty(self):
800 "Pushes the value of this property to the server."
801 VectorProperty._UpdateProperty(self)
802 self.Proxy.FileNameChanged()
804 class ArraySelectionProperty(VectorProperty):
805 "Property to select an array to be processed by a filter."
807 def GetAssociation(self):
808 val = self.GetElement(3)
811 for key, value in ASSOCIATIONS.iteritems():
812 if value == int(val):
817 def GetArrayName(self):
818 return self.GetElement(4)
821 """Returns the number of elements."""
824 def __setitem__(self, idx, value):
825 raise RuntimeError, "This property cannot be accessed using __setitem__"
827 def __getitem__(self, idx):
828 """Returns attribute type for index 0, array name for index 1"""
829 if isinstance(idx, slice):
830 indices = idx.indices(len(self))
832 for i in range(*indices):
836 retVal.append(self.GetAssociation())
838 retVal.append(self.GetArrayName())
840 elif idx >= 2 or idx < 0:
844 return self.GetAssociation()
846 return self.GetArrayName()
848 def SetData(self, values):
849 """Allows setting of all values at once. Requires a single value,
851 if not isinstance(values, tuple) and \
852 not isinstance(values, list):
855 self.SMProperty.SetElement(4, values[0])
856 elif len(values) == 2:
857 if isinstance(values[0], str):
858 val = str(ASSOCIATIONS[values[0]])
860 # In case user didn't specify valid association,
862 val = str(ASSOCIATIONS['POINTS'])
863 self.SMProperty.SetElement(3, str(val))
864 self.SMProperty.SetElement(4, values[1])
866 raise RuntimeError, "Expected 1 or 2 values."
867 self._UpdateProperty()
869 def UpdateDefault(self):
870 "Helper method to set default values."
871 if self.SMProperty.GetNumberOfElements() != 5:
873 if self.GetElement(4) != '' or \
874 self.GetElement(3) != '':
878 if self.GetElement(i) == '':
879 self.SMProperty.SetElement(i, '0')
880 al = self.SMProperty.GetDomain("array_list")
881 al.Update(self.SMProperty)
882 al.SetDefaultValues(self.SMProperty)
884 class ArrayListProperty(VectorProperty):
885 """This property provides a simpler interface for selecting arrays.
886 Simply assign a list of arrays that should be loaded by the reader.
887 Use the Available property to get a list of available arrays."""
889 def __init__(self, proxy, smproperty):
890 VectorProperty.__init__(self, proxy, smproperty)
893 def GetAvailable(self):
894 "Returns the list of available arrays"
895 dm = self.GetDomain("array_list")
897 for i in range(dm.GetNumberOfStrings()):
898 retVal.append(dm.GetString(i))
901 Available = property(GetAvailable, None, None, \
902 "This read-only property contains the list of items that can be read by a reader.")
905 "Selects all arrays."
906 self.SetData(self.Available)
908 def DeselectAll(self):
909 "Deselects all arrays."
913 """Implementation of the sequence API"""
914 return GenericIterator(self)
917 """Returns the number of elements."""
918 return len(self.GetData())
920 def __setitem__(self, idx, value):
921 """Given a list or tuple of values, sets a slice of values [min, max)"""
923 if isinstance(idx, slice):
924 indices = idx.indices(len(self))
925 for i, j in zip(range(*indices), value):
927 self.SetData(self.__arrays)
928 elif idx >= len(self) or idx < 0:
931 self.__arrays[idx] = self.ConvertValue(value)
932 self.SetData(self.__arrays)
934 def __getitem__(self, idx):
935 """Returns the range [min, max) of elements. Raises an IndexError
936 exception if an argument is out of bounds."""
938 if isinstance(idx, slice):
939 indices = idx.indices(len(self))
941 for i in range(*indices):
942 retVal.append(self.__arrays[i])
944 elif idx >= len(self) or idx < 0:
946 return self.__arrays[idx]
948 def SetData(self, values):
949 """Allows setting of all values at once. Requires a single value,
952 iup = self.SMProperty.GetImmediateUpdate()
953 self.SMProperty.SetImmediateUpdate(False)
955 self.SMProperty.SetNumberOfElements(0)
956 if not isinstance(values, tuple) and \
957 not isinstance(values, list):
962 # The order of the two loops below are delibrately set in this way
963 # so that values passed in will take precedence.
964 # This is needed for backward compatibility of the
965 # property ElementBlocks for vtkExodusIIReader.
966 # If you attemp to change this, please verify that
967 # python state files for opening old .ex2 file (<=3.14) still works.
968 for array in self.Available:
969 if not values.__contains__(array):
970 fullvalues.append(array)
971 fullvalues.append('0')
973 for i in range(len(values)):
974 val = self.ConvertValue(values[i])
975 fullvalues.append(val)
976 fullvalues.append('1')
980 for value in fullvalues:
981 self.SMProperty.SetElement(i, value)
984 self._UpdateProperty()
985 self.SMProperty.SetImmediateUpdate(iup)
988 "Returns all elements as a list."
989 property = self.SMProperty
990 nElems = property.GetNumberOfElements()
992 raise ValueError, "The SMProperty with XML label '%s' has a size that is not a multiple of 2." % property.GetXMLLabel()
994 for i in range(0, nElems, 2):
995 if self.GetElement(i+1) != '0':
996 self.__arrays.append(self.GetElement(i))
997 return list(self.__arrays)
999 class ProxyProperty(Property):
1000 """A ProxyProperty provides access to one or more proxies. You can use
1001 a slice to get one or more property values:
1002 > proxy = property[2]
1004 > proxies = property[0:5:2]
1005 You can use a slice to set one or more property values:
1006 > property[2] = proxy
1008 > property[1:3] = (proxy1, proxy2)
1009 You can also append and delete:
1010 > property.append(proxy)
1014 You can also remove all elements with Clear().
1016 Note that some properties expect only 1 proxy and will complain if
1017 you set the number of values to be something else.
1019 def __init__(self, proxy, smproperty):
1020 """Default constructor. Stores a reference to the proxy. Also looks
1021 at domains to find valid values."""
1022 Property.__init__(self, proxy, smproperty)
1023 # Check to see if there is a proxy list domain and, if so,
1024 # initialize ourself. (Should this go in ProxyProperty?)
1025 listdomain = self.GetDomain('proxy_list')
1027 if listdomain.GetClassName() != 'vtkSMProxyListDomain':
1028 raise ValueError, "Found a 'proxy_list' domain on an InputProperty that is not a ProxyListDomain."
1030 group = "pq_helper_proxies." + proxy.GetGlobalIDAsString()
1031 if listdomain.GetNumberOfProxies() == 0:
1032 for i in xrange(listdomain.GetNumberOfProxyTypes()):
1033 igroup = listdomain.GetProxyGroup(i)
1034 name = listdomain.GetProxyName(i)
1035 iproxy = CreateProxy(igroup, name)
1036 listdomain.AddProxy(iproxy)
1037 pm.RegisterProxy(group, proxy.GetPropertyName(smproperty), iproxy)
1038 listdomain.SetDefaultValues(self.SMProperty)
1040 def GetAvailable(self):
1041 """If this proxy has a list domain, then this function returns the
1042 strings you can use to select from the domain. If there is no such
1043 list domain, the returned list is empty."""
1044 listdomain = self.GetDomain('proxy_list')
1047 for i in xrange(listdomain.GetNumberOfProxies()):
1048 proxy = listdomain.GetProxy(i)
1049 retval.append(proxy.GetXMLLabel())
1052 Available = property(GetAvailable, None, None,
1053 """This read only property is a list of strings you can
1054 use to select from the list domain. If there is no
1055 such list domain, the array is empty.""")
1058 """Implementation of the sequence API"""
1059 return GenericIterator(self)
1062 """Returns the number of elements."""
1063 return self.SMProperty.GetNumberOfProxies()
1065 def remove(self, proxy):
1066 """Removes the first occurence of the proxy from the property."""
1067 self.SMProperty.RemoveProxy(proxy.SMProxy)
1068 self._UpdateProperty()
1070 def __setitem__(self, idx, value):
1071 """Given a list or tuple of values, sets a slice of values [min, max)"""
1072 if isinstance(idx, slice):
1073 indices = idx.indices(len(self))
1074 for i, j in zip(range(*indices), value):
1075 self.SMProperty.SetProxy(i, j.SMProxy)
1076 self._UpdateProperty()
1077 elif idx >= len(self) or idx < 0:
1080 self.SMProperty.SetProxy(idx, value.SMProxy)
1081 self._UpdateProperty()
1083 def __delitem__(self,idx):
1084 """Removes the element idx"""
1085 if isinstance(idx, slice):
1086 indices = idx.indices(len(self))
1087 # Collect the elements to delete to a new list first.
1088 # Otherwise indices are screwed up during the actual
1091 for i in range(*indices):
1092 toremove.append(self[i])
1094 self.SMProperty.RemoveProxy(i.SMProxy)
1095 self._UpdateProperty()
1096 elif idx >= len(self) or idx < 0:
1099 self.SMProperty.RemoveProxy(self[idx].SMProxy)
1100 self._UpdateProperty()
1102 def __getitem__(self, idx):
1103 """Returns the range [min, max) of elements. Raises an IndexError
1104 exception if an argument is out of bounds."""
1105 if isinstance(idx, slice):
1106 indices = idx.indices(len(self))
1108 for i in range(*indices):
1109 retVal.append(_getPyProxy(self.SMProperty.GetProxy(i)))
1111 elif idx >= len(self) or idx < 0:
1113 return _getPyProxy(self.SMProperty.GetProxy(idx))
1115 def __getattr__(self, name):
1116 "Unknown attribute requests get forwarded to SMProperty."
1117 return getattr(self.SMProperty, name)
1119 def index(self, proxy):
1123 if proxy.IsSame(px):
1126 raise ValueError("proxy is not in the list.")
1128 def append(self, proxy):
1129 "Appends the given proxy to the property values."
1130 self.SMProperty.AddProxy(proxy.SMProxy)
1131 self._UpdateProperty()
1134 "Returns all elements as either a list or a single value."
1135 property = self.SMProperty
1136 if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
1137 return self[0:len(self)]
1139 if property.GetNumberOfProxies() > 0:
1140 return _getPyProxy(property.GetProxy(0))
1143 def SetData(self, values):
1144 """Allows setting of all values at once. Requires a single value,
1146 if isinstance(values, str):
1149 position = self.Available.index(values)
1151 raise ValueError, values + " is not a valid object in the domain."
1152 values = self.GetDomain('proxy_list').GetProxy(position)
1153 if not isinstance(values, tuple) and \
1154 not isinstance(values, list):
1156 self.SMProperty.RemoveAllProxies()
1157 for value in values:
1158 if isinstance(value, Proxy):
1159 value_proxy = value.SMProxy
1162 self.SMProperty.AddProxy(value_proxy)
1163 self._UpdateProperty()
1166 "Removes all elements."
1167 self.SMProperty.RemoveAllProxies()
1168 self._UpdateProperty()
1170 class InputProperty(ProxyProperty):
1171 """An InputProperty allows making pipeline connections. You can set either
1172 a source proxy or an OutputProperty to an input property:
1174 > property[0] = proxy
1176 > property[0] = OuputPort(proxy, 1)
1178 > property.append(proxy)
1180 > property.append(OutputPort(proxy, 0))
1182 def __setitem__(self, idx, value):
1183 """Given a list or tuple of values, sets a slice of values [min, max)"""
1184 if isinstance(idx, slice):
1185 indices = idx.indices(len(self))
1186 for i, j in zip(range(*indices), value):
1188 self.SMProperty.SetInputConnection(i, op.SMProxy, op.Port)
1189 self._UpdateProperty()
1190 elif idx >= len(self) or idx < 0:
1193 self.SMProperty.SetInputConnection(idx, value.SMProxy, value.Port)
1194 self._UpdateProperty()
1196 def __getitem__(self, idx):
1197 """Returns the range [min, max) of elements. Raises an IndexError
1198 exception if an argument is out of bounds."""
1199 if isinstance(idx, slice):
1200 indices = idx.indices(len(self))
1202 for i in range(*indices):
1204 if self.SMProperty.GetProxy(i):
1205 port = OutputPort(_getPyProxy(self.SMProperty.GetProxy(i)),\
1206 self.SMProperty.GetOutputPortForConnection(i))
1209 elif idx >= len(self) or idx < 0:
1211 return OutputPort(_getPyProxy(self.SMProperty.GetProxy(idx)),\
1212 self.SMProperty.GetOutputPortForConnection(idx))
1214 def append(self, value):
1215 """Appends the given proxy to the property values.
1216 Accepts Proxy or OutputPort objects."""
1217 self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
1218 self._UpdateProperty()
1221 """Returns all elements as either a list of OutputPort objects or
1222 a single OutputPort object."""
1223 property = self.SMProperty
1224 if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
1225 return self[0:len(self)]
1227 if property.GetNumberOfProxies() > 0:
1228 return OutputPort(_getPyProxy(property.GetProxy(0)),\
1229 self.SMProperty.GetOutputPortForConnection(0))
1232 def SetData(self, values):
1233 """Allows setting of all values at once. Requires a single value,
1234 a tuple or list. Accepts Proxy or OutputPort objects."""
1235 if isinstance(values, str):
1236 ProxyProperty.SetData(self, values)
1238 if not isinstance(values, tuple) and \
1239 not isinstance(values, list):
1241 self.SMProperty.RemoveAllProxies()
1242 for value in values:
1244 self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
1245 self._UpdateProperty()
1247 def _UpdateProperty(self):
1248 "Pushes the value of this property to the server."
1249 ProxyProperty._UpdateProperty(self)
1250 iter = PropertyIterator(self.Proxy)
1252 if isinstance(prop, ArraySelectionProperty):
1253 prop.UpdateDefault()
1256 class DataInformation(object):
1257 """DataInformation is a contained for meta-data associated with an
1260 DataInformation is a python wrapper around a vtkPVDataInformation.
1261 In addition to proving all methods of a vtkPVDataInformation, it provides
1262 a few convenience methods.
1264 Please note that some of the methods accessible through the DataInformation
1265 class are not listed by help() because the DataInformation objects forward
1266 unresolved attributes to the underlying object. To get the full list,
1267 see also dir(proxy.DataInformation).
1268 See also the doxygen based documentation of the vtkPVDataInformation C++
1271 def __init__(self, dataInformation, proxy, idx):
1272 """Default constructor. Requires a vtkPVDataInformation, a source proxy
1273 and an output port id."""
1274 self.DataInformation = dataInformation
1279 """****Deprecated**** There is no reason anymore to use this method
1280 explicitly, it is called automatically when one gets any value from the
1281 data information object.
1282 Update the data information if necessary. Note that this
1283 does not cause execution of the underlying object. In certain
1284 cases, you may have to call UpdatePipeline() on the proxy."""
1286 self.Proxy.GetDataInformation(self.Idx)
1288 def GetDataSetType(self):
1289 """Returns the dataset type as defined in vtkDataObjectTypes."""
1291 if not self.DataInformation:
1292 raise RuntimeError, "No data information is available"
1293 if self.DataInformation.GetCompositeDataSetType() > -1:
1294 return self.DataInformation.GetCompositeDataSetType()
1295 return self.DataInformation.GetDataSetType()
1297 def GetDataSetTypeAsString(self):
1298 """Returns the dataset type as a user-friendly string. This is
1299 not the same as the enumaration used by VTK"""
1300 return vtk.vtkDataObjectTypes.GetClassNameFromTypeId(self.GetDataSetType())
1302 def __getattr__(self, name):
1303 """Forwards unknown attribute requests to the underlying
1304 vtkPVInformation."""
1305 if not self.DataInformation:
1306 raise AttributeError("class has no attribute %s" % name)
1309 return getattr(self.DataInformation, name)
1311 class ArrayInformation(object):
1312 """Meta-information associated with an array. Use the Name
1313 attribute to get the array name.
1315 Please note that some of the methods accessible through the ArrayInformation
1316 class are not listed by help() because the ArrayInformation objects forward
1317 unresolved attributes to the underlying object.
1318 See the doxygen based documentation of the vtkPVArrayInformation C++
1319 class for a full list.
1321 def __init__(self, proxy, field, name):
1323 self.FieldData = field
1326 def __getattr__(self, name):
1327 """Forward unknown methods to vtkPVArrayInformation"""
1328 array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
1329 if not array: return None
1330 return getattr(array, name)
1333 """Returns a user-friendly representation string."""
1334 return "Array: " + self.Name
1336 def GetRange(self, component=0):
1337 """Given a component, returns its value range as a tuple of 2 values."""
1338 array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
1339 range = array.GetComponentRange(component)
1340 return (range[0], range[1])
1342 class FieldDataInformationIterator(object):
1343 """Iterator for FieldDataInformation"""
1345 def __init__(self, info, items=False):
1346 self.FieldDataInformation = info
1354 if self.index >= self.FieldDataInformation.GetNumberOfArrays():
1358 ai = self.FieldDataInformation[self.index-1]
1360 return (ai.GetName(), ai)
1365 class FieldDataInformation(object):
1366 """Meta-data for a field of an output object (point data, cell data etc...).
1367 Provides easy access to the arrays using the slice interface:
1368 > narrays = len(field_info)
1369 > for i in range(narrays):
1370 > array_info = field_info[i]
1372 Full slice interface is supported:
1373 > arrays = field_info[0:5:3]
1374 where arrays is a list.
1376 Array access by name is also possible:
1377 > array_info = field_info['Temperature']
1379 The number of arrays can also be accessed using the NumberOfArrays
1382 def __init__(self, proxy, idx, field):
1384 self.OutputPort = idx
1385 self.FieldData = field
1387 def GetFieldData(self):
1388 """Convenience method to get the underlying
1389 vtkPVDataSetAttributesInformation"""
1390 return getattr(self.Proxy.GetDataInformation(self.OutputPort), "Get%sInformation" % self.FieldData)()
1392 def GetNumberOfArrays(self):
1393 """Returns the number of arrays."""
1394 self.Proxy.UpdatePipeline()
1395 return self.GetFieldData().GetNumberOfArrays()
1397 def GetArray(self, idx):
1398 """Given an index or a string, returns an array information.
1399 Raises IndexError if the index is out of bounds."""
1400 self.Proxy.UpdatePipeline()
1401 if not self.GetFieldData().GetArrayInformation(idx):
1403 if isinstance(idx, str):
1404 return ArrayInformation(self.Proxy, self, idx)
1405 elif idx >= len(self) or idx < 0:
1407 return ArrayInformation(self.Proxy, self, self.GetFieldData().GetArrayInformation(idx).GetName())
1410 """Returns the number of arrays."""
1411 return self.GetNumberOfArrays()
1413 def __getitem__(self, idx):
1414 """Implements the [] operator. Accepts an array name."""
1415 if isinstance(idx, slice):
1416 indices = idx.indices(self.GetNumberOfArrays())
1418 for i in range(*indices):
1419 retVal.append(self.GetArray(i))
1421 return self.GetArray(idx)
1424 """Implementation of the dictionary API"""
1426 narrays = self.GetNumberOfArrays()
1427 for i in range(narrays):
1428 kys.append(self.GetArray(i).GetName())
1432 """Implementation of the dictionary API"""
1434 narrays = self.GetNumberOfArrays()
1435 for i in range(narrays):
1436 vals.append(self.GetArray(i))
1439 def iteritems(self):
1440 """Implementation of the dictionary API"""
1441 return FieldDataInformationIterator(self, True)
1444 """Implementation of the dictionary API"""
1446 narrays = self.GetNumberOfArrays()
1447 for i in range(narrays):
1448 ai = self.GetArray(i)
1449 itms.append((ai.GetName(), ai))
1452 def has_key(self, key):
1453 """Implementation of the dictionary API"""
1454 if self.GetArray(key):
1459 """Implementation of the dictionary API"""
1460 return FieldDataInformationIterator(self)
1462 def __getattr__(self, name):
1463 """Forwards unknown attributes to the underlying
1464 vtkPVDataSetAttributesInformation"""
1465 array = self.GetArray(name)
1466 if array: return array
1467 raise AttributeError("class has no attribute %s" % name)
1470 NumberOfArrays = property(GetNumberOfArrays, None, None, "Returns the number of arrays.")
1472 def OutputPort(proxy, outputPort=0):
1475 if isinstance(outputPort, str):
1476 outputPort = proxy.GetOutputPortIndex(outputPort)
1477 if outputPort >= proxy.GetNumberOfOutputPorts():
1479 if proxy.Port == outputPort:
1481 newinstance = _getPyProxy(proxy.SMProxy, outputPort)
1482 newinstance.Port = outputPort
1483 newinstance._Proxy__Properties = proxy._Proxy__Properties
1486 class ProxyManager(object):
1487 """When running scripts from the python shell in the ParaView application,
1488 registering proxies with the proxy manager is the only mechanism to
1489 notify the graphical user interface (GUI) that a proxy
1490 exists. Therefore, unless a proxy is registered, it will not show up in
1491 the user interface. Also, the proxy manager is the only way to get
1492 access to proxies created using the GUI. Proxies created using the GUI
1493 are automatically registered under an appropriate group (sources,
1494 filters, representations and views). To get access to these objects,
1495 you can use proxyManager.GetProxy(group, name). The name is the same
1496 as the name shown in the pipeline browser.
1498 This class is a python wrapper for vtkSMProxyManager. Note that the
1499 underlying vtkSMProxyManager is a singleton. All instances of this
1500 class will refer to the same object. In addition to all methods provided by
1501 vtkSMProxyManager (all unknown attribute requests are forwarded
1502 to the vtkSMProxyManager), this class provides several convenience
1505 Please note that some of the methods accessible through the ProxyManager
1506 class are not listed by help() because the ProxyManager objects forwards
1507 unresolved attributes to the underlying object. To get the full list,
1508 see also dir(proxy.SMProxyManager). See also the doxygen based documentation
1509 of the vtkSMProxyManager C++ class.
1512 def __init__(self, session=None):
1513 """Constructor. Assigned self.SMProxyManager to
1514 vtkSMProxyManager.GetProxyManager()."""
1515 global ActiveConnection
1517 session = ActiveConnection.Session
1518 self.SMProxyManager = session.GetSessionProxyManager()
1520 def RegisterProxy(self, group, name, aProxy):
1521 """Registers a proxy (either SMProxy or proxy) with the
1523 if isinstance(aProxy, Proxy):
1524 self.SMProxyManager.RegisterProxy(group, name, aProxy.SMProxy)
1526 self.SMProxyManager.RegisterProxy(group, name, aProxy)
1528 def NewProxy(self, group, name):
1529 """Creates a new proxy of given group and name and returns an SMProxy.
1530 Note that this is a server manager object. You should normally create
1531 proxies using the class objects. For example:
1532 obj = servermanager.sources.SphereSource()"""
1533 if not self.SMProxyManager:
1535 aProxy = self.SMProxyManager.NewProxy(group, name, "NULL")
1538 aProxy.UnRegister(None)
1541 def GetProxy(self, group, name):
1542 """Returns a Proxy registered under a group and name"""
1543 if not self.SMProxyManager:
1545 aProxy = self.SMProxyManager.GetProxy(group, name)
1548 return _getPyProxy(aProxy)
1550 def GetPrototypeProxy(self, group, name):
1551 """Returns a prototype proxy given a group and name. This is an
1552 SMProxy. This is a low-level method. You should not normally
1554 if not self.SMProxyManager:
1556 aProxy = self.SMProxyManager.GetPrototypeProxy(group, name)
1561 def GetProxiesInGroup(self, groupname):
1562 """Returns a map of proxies in a particular group."""
1564 iter = self.NewGroupIterator(groupname)
1566 proxies[(iter.GetKey(), aProxy.GetGlobalIDAsString())] = aProxy
1569 def UnRegisterProxy(self, groupname, proxyname, aProxy):
1570 """Unregisters a proxy."""
1571 if not self.SMProxyManager:
1573 if aProxy != None and isinstance(aProxy,Proxy):
1574 aProxy = aProxy.SMProxy
1576 self.SMProxyManager.UnRegisterProxy(groupname, proxyname, aProxy)
1578 def GetProxies(self, groupname, proxyname):
1579 """Returns all proxies registered under the given group with the
1580 given name. Note that it is possible to register more than one
1581 proxy with the same name in the same group. Because the proxies
1582 are different, there is no conflict. Use this method instead of
1583 GetProxy() if you know that there are more than one proxy registered
1585 if not self.SMProxyManager:
1587 collection = vtk.vtkCollection()
1589 self.SMProxyManager.GetProxies(groupname, proxyname, collection)
1590 for i in range(0, collection.GetNumberOfItems()):
1591 aProxy = _getPyProxy(collection.GetItemAsObject(i))
1593 result.append(aProxy)
1598 """Returns a new ProxyIterator."""
1599 iter = ProxyIterator()
1603 def NewGroupIterator(self, group_name):
1604 """Returns a ProxyIterator for a group. The resulting object
1605 can be used to traverse the proxies that are in the given
1607 iter = self.__iter__()
1608 iter.SetModeToOneGroup()
1609 iter.Begin(group_name)
1612 def NewDefinitionIterator(self, groupname=None):
1613 """Returns an iterator that can be used to iterate over
1614 all groups and types of proxies that the proxy manager
1617 if groupname != None:
1618 iter = ProxyDefinitionIterator(self.GetProxyDefinitionManager().NewSingleGroupIterator(groupname,0))
1620 iter = ProxyDefinitionIterator(self.GetProxyDefinitionManager().NewIterator(0))
1624 def __ConvertArgumentsAndCall(self, *args):
1627 if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
1628 newArgs.append(arg.SMProxy)
1631 func = getattr(self.SMProxyManager, self.__LastAttrName)
1632 retVal = func(*newArgs)
1633 if type(retVal) is type(self.SMProxyManager) and retVal.IsA("vtkSMProxy"):
1634 return _getPyProxy(retVal)
1638 def __getattr__(self, name):
1639 """Returns attribute from the ProxyManager"""
1641 pmAttr = getattr(self.SMProxyManager, name)
1642 self.__LastAttrName = name
1643 return self.__ConvertArgumentsAndCall
1646 return getattr(self.SMProxyManager, name)
1648 def LoadState(self, filename, loader = None):
1649 self.SMProxyManager.LoadXMLState(filename, loader)
1651 def SaveState(self, filename):
1652 self.SMProxyManager.SaveXMLState(filename)
1654 class PropertyIterator(object):
1655 """Wrapper for a vtkSMPropertyIterator class to satisfy
1656 the python iterator protocol. Note that the list of
1657 properties can also be obtained from the class object's
1659 See the doxygen documentation for vtkSMPropertyIterator C++
1663 def __init__(self, aProxy):
1664 self.SMIterator = aProxy.NewPropertyIterator()
1666 self.SMIterator.UnRegister(None)
1667 self.SMIterator.Begin()
1669 self.PropertyLabel = None
1676 if not self.SMIterator:
1679 if self.SMIterator.IsAtEnd():
1682 self.Key = self.SMIterator.GetKey()
1683 self.PropertyLabel = self.SMIterator.GetPropertyLabel()
1684 self.SMIterator.Next()
1685 return self.Proxy.GetProperty(self.Key)
1688 """Returns the proxy for the property last returned by the call to
1693 """Returns the key for the property last returned by the call to
1697 def GetProperty(self):
1698 """Returns the property last returned by the call to 'next()' """
1699 return self.Proxy.GetProperty(self.Key)
1701 def __getattr__(self, name):
1702 """returns attributes from the vtkSMPropertyIterator."""
1703 return getattr(self.SMIterator, name)
1705 class ProxyDefinitionIterator(object):
1706 """Wrapper for a vtkPVProxyDefinitionIterator class to satisfy
1707 the python iterator protocol.
1708 See the doxygen documentation of the vtkPVProxyDefinitionIterator
1709 C++ class for more information."""
1710 def __init__(self, iter):
1711 self.SMIterator = iter
1713 self.SMIterator.UnRegister(None)
1714 self.SMIterator.InitTraversal()
1722 if self.SMIterator.IsDoneWithTraversal():
1726 self.Group = self.SMIterator.GetGroupName()
1727 self.Key = self.SMIterator.GetProxyName()
1728 self.SMIterator.GoToNextItem()
1729 return {"group": self.Group, "key":self.Key }
1731 def GetProxyName(self):
1732 """Returns the key for the proxy definition last returned by the call
1737 """Returns the group for the proxy definition last returned by the
1738 call to 'next()' """
1741 def __getattr__(self, name):
1742 """returns attributes from the vtkPVProxyDefinitionIterator."""
1743 return getattr(self.SMIterator, name)
1746 class ProxyIterator(object):
1747 """Wrapper for a vtkSMProxyIterator class to satisfy the
1748 python iterator protocol.
1749 See the doxygen documentation of vtkSMProxyIterator C++ class for
1753 self.SMIterator = vtkSMProxyIterator()
1754 self.SMIterator.SetSession(ActiveConnection.Session)
1755 self.SMIterator.Begin()
1764 if self.SMIterator.IsAtEnd():
1770 self.AProxy = _getPyProxy(self.SMIterator.GetProxy())
1771 self.Group = self.SMIterator.GetGroup()
1772 self.Key = self.SMIterator.GetKey()
1773 self.SMIterator.Next()
1777 """Returns the proxy last returned by the call to 'next()'"""
1781 """Returns the key for the proxy last returned by the call to
1786 """Returns the group for the proxy last returned by the call to
1790 def __getattr__(self, name):
1791 """returns attributes from the vtkSMProxyIterator."""
1792 return getattr(self.SMIterator, name)
1794 # Caution: Observers must be global methods otherwise we run into memory
1795 # leak when the interpreter get reset from the C++ layer.
1796 def _update_definitions(caller, event):
1797 updateModules(ActiveConnection.Modules)
1799 class Connection(object):
1801 This is a python representation for a session/connection.
1803 def __init__(self, connectionId, session):
1804 """Default constructor. Creates a Connection with the given
1805 ID, all other data members initialized to None."""
1806 global MultiServerConnections
1807 global ActiveConnection
1808 self.ID = connectionId
1809 self.Session = session
1810 self.Modules = PVModule()
1812 self.DefinitionObserverTag = 0
1813 self.CustomDefinitionObserverTag = 0
1814 if MultiServerConnections == None and ActiveConnection:
1815 raise RuntimeError, "Concurrent connections not supported!"
1816 if MultiServerConnections != None and not self in MultiServerConnections:
1817 MultiServerConnections.append(self)
1818 ActiveConnection = self
1819 __InitAfterConnect__(self)
1820 __exposeActiveModules__()
1822 def __eq__(self, other):
1823 "Returns true if the connection ids are the same."
1824 return (self.ID == other.ID)
1827 """User friendly string representation"""
1828 return "Connection (%s) [%d]" % (self.Session.GetURI(), self.ID)
1831 """Get URI of the connection"""
1832 return self.Session.GetURI()
1835 """Returns True if the connection to a remote server, False if
1836 it is local (built-in)"""
1837 if self.Session.IsA("vtkSMSessionClient"):
1841 def GetNumberOfDataPartitions(self):
1842 """Returns the number of partitions on the data server for this
1844 return self.Session.GetServerInformation().GetNumberOfProcesses()
1846 def AttachDefinitionUpdater(self):
1847 """Attach observer to automatically update modules when needed."""
1848 # VTN: Observers are not supported
1849 # ProxyDefinitionsUpdated = 2000
1850 ## self.DefinitionObserverTag = self.Session.GetProxyDefinitionManager().AddObserver(2000, _update_definitions)
1851 # CompoundProxyDefinitionsUpdated = 2001
1852 ## self.CustomDefinitionObserverTag = self.Session.GetProxyDefinitionManager().AddObserver(2001, _update_definitions)
1856 if self.DefinitionObserverTag:
1857 self.Session.GetProxyDefinitionManager().RemoveObserver(self.DefinitionObserverTag)
1858 self.Session.GetProxyDefinitionManager().RemoveObserver(self.CustomDefinitionObserverTag)
1867 def SaveState(filename):
1868 """Given a state filename, saves the state of objects registered
1869 with the proxy manager."""
1871 pm.SaveState(filename)
1873 def LoadState(filename, connection=None):
1874 """Given a state filename and an optional connection, loads the server
1877 connection = ActiveConnection
1879 raise RuntimeError, "Cannot load state without a connection"
1881 pm.LoadState(filename, None)
1882 views = GetRenderViews()
1884 # Make sure that the client window size matches the
1885 # ViewSize property. In paraview, the GUI takes care
1887 if view.GetClassName() == "vtkSMIceTDesktopRenderViewProxy":
1888 view.GetRenderWindow().SetSize(view.ViewSize[0], \
1893 Method used to initialize the Python Shell from the ParaView GUI.
1895 global fromGUI, ActiveConnection
1897 # print "from paraview.simple import *"
1899 # ToggleProgressPrinting() ### FIXME COLLABORATION
1900 enableMultiServer(vtkProcessModule.GetProcessModule().GetMultipleSessionsSupport())
1901 iter = vtkProcessModule.GetProcessModule().NewSessionIterator();
1902 iter.InitTraversal()
1903 ActiveConnection = None
1904 activeSession = vtkSMProxyManager.GetProxyManager().GetActiveSession()
1905 tmpActiveConnection = None
1906 while not iter.IsDoneWithTraversal():
1907 c = Connection(iter.GetCurrentSessionId(), iter.GetCurrentSession())
1908 if c.Session == activeSession:
1909 tmpActiveConnection = c
1911 iter.UnRegister(None)
1912 if tmpActiveConnection:
1913 ActiveConnection = tmpActiveConnection
1915 def Connect(ds_host=None, ds_port=11111, rs_host=None, rs_port=22221):
1917 Use this function call to create a new session. On success,
1918 it returns a vtkSMSession object that abstracts the connection.
1919 Otherwise, it returns None.
1920 There are several ways in which this function can be called:
1921 * When called with no arguments, it creates a new session
1922 to the built-in server on the client itself.
1923 * When called with ds_host and ds_port arguments, it
1924 attempts to connect to a server(data and render server on the same server)
1925 on the indicated host:port.
1926 * When called with ds_host, ds_port, rs_host, rs_port, it
1927 creates a new connection to the data server on ds_host:ds_port and to the
1928 render server on rs_host: rs_port.
1931 session = vtkSMSession()
1932 elif rs_host == None:
1933 session = vtkSMSessionClient()
1934 session.Connect("cs://%s:%d" % (ds_host, ds_port))
1936 session = vtkSMSessionClient()
1937 session.Connect("cdsrs://%s:%d/%s:%d" % (ds_host, ds_port, rs_host, rs_port))
1938 id = vtkProcessModule.GetProcessModule().RegisterSession(session)
1939 connection = Connection(id, session)
1942 def ReverseConnect(port=11111):
1944 Use this function call to create a new session. On success,
1945 it returns a Session object that abstracts the connection.
1946 Otherwise, it returns None.
1947 In reverse connection mode, the client waits for a connection
1948 from the server (client has to be started first). The server
1949 then connects to the client (run pvserver with -rc and -ch
1951 The optional port specified the port to listen to.
1953 session = vtkSMSessionClient()
1954 session.Connect("csrc://hostname:" + port)
1955 id = vtkProcessModule.GetProcessModule().RegisterSession(session)
1956 connection = Connection(id, session)
1959 def Disconnect(session=None):
1960 """Disconnects the connection. Make sure to clear the proxy manager
1962 global ActiveConnection
1963 global MultiServerConnections
1966 # Let the UI know that we want to disconnect
1967 ActiveConnection.Session.InvokeEvent('ExitEvent')
1969 if ActiveConnection and (not session or session == ActiveConnection.Session):
1970 session = ActiveConnection.Session
1971 if MultiServerConnections:
1972 MultiServerConnections.remove(ActiveConnection)
1973 ActiveConnection.close()
1974 ActiveConnection = None
1975 switchActiveConnection()
1977 ActiveConnection.close()
1978 ActiveConnection = None
1979 elif MultiServerConnections:
1980 for connection in MultiServerConnections:
1981 if connection.Session == session:
1983 MultiServerConnections.remove(connection)
1985 vtkProcessModule.GetProcessModule().UnRegisterSession(session)
1988 def CreateProxy(xml_group, xml_name, session=None):
1989 """Creates a proxy. If session is set, the proxy's session is
1990 set accordingly. If session is None, the current Session is used, if
1991 present. You should not have to use method normally. Instantiate the
1992 appropriate class from the appropriate module, for example:
1993 sph = servermanager.sources.SphereSource()"""
1994 global ActiveConnection
1996 session = ActiveConnection.Session
1998 raise RuntimeError, "Cannot create objects without a session."
1999 pxm = ProxyManager(session)
2000 return pxm.NewProxy(xml_group, xml_name)
2002 def GetRenderView(connection=None):
2003 """Return the render view in use. If more than one render view is in
2004 use, return the first one."""
2006 render_module = None
2007 for aProxy in ProxyManager():
2008 if aProxy.IsA("vtkSMRenderViewProxy"):
2009 render_module = aProxy
2011 return render_module
2013 def GetRenderViews(connection=None):
2014 """Returns the set of all render views."""
2016 for aProxy in ProxyManager():
2017 if aProxy.IsA("vtkSMRenderViewProxy"):
2018 render_modules.append(aProxy)
2019 return render_modules
2021 def GetContextViews(connection=None):
2022 """Returns the set of all context views."""
2023 context_modules = []
2024 for aProxy in ProxyManager():
2025 if aProxy.IsA("vtkSMContextViewProxy"):
2026 context_modules.append(aProxy)
2027 return context_modules
2029 def CreateRenderView(session=None, **extraArgs):
2030 """Creates a render window on the particular session. If session
2031 is not specified, then the active session is used, if available.
2033 This method can also be used to initialize properties by passing
2034 keyword arguments where the key is the name of the property. In addition
2035 registrationGroup and registrationName (optional) can be specified (as
2036 keyword arguments) to automatically register the proxy with the proxy
2038 return _create_view("RenderView", session, **extraArgs)
2040 def _create_view(view_xml_name, session=None, **extraArgs):
2041 """Creates a view on the particular session. If session
2042 is not specified, then the active session is used, if available.
2043 This method can also be used to initialize properties by passing
2044 keyword arguments where the key is the name of the property."""
2046 session = ActiveConnection.Session
2048 raise RuntimeError, "Cannot create view without session."
2049 pxm = ProxyManager()
2052 view_module = CreateProxy("views", view_xml_name, session)
2055 extraArgs['proxy'] = view_module
2056 python_proxy_name = _make_name_valid(view_module.GetXMLName())
2057 proxy = rendering.__dict__[python_proxy_name](**extraArgs)
2060 def GetRepresentation(aProxy, view):
2061 for rep in view.Representations:
2063 try: isRep = rep.Input.IsSame(aProxy)
2064 except: isRep = False
2065 if isRep: return rep
2068 def CreateRepresentation(aProxy, view, **extraArgs):
2069 """Creates a representation for the proxy and adds it to the render
2072 This method can also be used to initialize properties by passing
2073 keyword arguments where the key is the name of the property.In addition
2074 registrationGroup and registrationName (optional) can be specified (as
2075 keyword arguments) to automatically register the proxy with the proxy
2078 This method tries to create the best possible representation for the given
2079 proxy in the given view. Additionally, the user can specify proxyName
2080 (optional) to create a representation of a particular type."""
2084 raise RuntimeError, "proxy argument cannot be None."
2086 raise RuntimeError, "view argument cannot be None."
2087 if "proxyName" in extraArgs:
2088 display = CreateProxy("representations", extraArgs['proxyName'], None)
2089 del extraArgs['proxyName']
2091 display = view.SMProxy.CreateDefaultRepresentation(aProxy.SMProxy, 0)
2093 display.UnRegister(None)
2096 extraArgs['proxy'] = display
2097 proxy = rendering.__dict__[display.GetXMLName()](**extraArgs)
2098 proxy.Input = aProxy
2099 proxy.UpdateVTKObjects()
2100 view.Representations.append(proxy)
2103 class _ModuleLoader(object):
2104 def find_module(self, fullname, path=None):
2105 if vtkPVPythonModule.HasModule(fullname):
2109 def load_module(self, fullname):
2111 moduleInfo = vtkPVPythonModule.GetModule(fullname)
2114 module = sys.modules.setdefault(fullname, imp.new_module(fullname))
2115 module.__file__ = "<%s>" % moduleInfo.GetFullName()
2116 module.__loader__ = self
2117 if moduleInfo.GetIsPackage:
2118 module.__path__ = moduleInfo.GetFullName()
2119 code = compile(moduleInfo.GetSource(), module.__file__, 'exec')
2120 exec code in module.__dict__
2123 def LoadXML(xmlstring):
2124 """DEPRECATED. Given a server manager XML as a string, parse and process it."""
2125 raise RuntimeError, "Deprecated. Use LoadPlugin(...) instead."
2128 def LoadPlugin(filename, remote=True, connection=None):
2129 """ Given a filename and a session (optional, otherwise uses
2130 ActiveConnection), loads a plugin. It then updates the sources,
2131 filters and rendering modules."""
2134 connection = ActiveConnection
2136 raise RuntimeError, "Cannot load a plugin without a connection."
2137 plm = vtkSMProxyManager.GetProxyManager().GetPluginManager()
2140 status = plm.LoadRemotePlugin(filename, connection.Session)
2142 status = plm.LoadLocalPlugin(filename)
2144 # shouldn't the extension check happend before attempting to load the plugin?
2146 raise RuntimeError, "Problem loading plugin %s" % (filename)
2148 # we should never have to call this. The modules should update automatically.
2149 updateModules(connection.Modules)
2152 def Fetch(input, arg1=None, arg2=None, idx=0):
2154 A convenience method that moves data from the server to the client,
2155 optionally performing some operation on the data as it moves.
2156 The input argument is the name of the (proxy for a) source or filter
2157 whose output is needed on the client.
2159 You can use Fetch to do three things:
2161 If arg1 is None (the default) then all of the data is brought to the client.
2162 In parallel runs an appropriate append Filter merges the
2163 data on each processor into one data object. The filter chosen will be
2164 vtkAppendPolyData for vtkPolyData, vtkAppendRectilinearGrid for
2165 vtkRectilinearGrid, vtkMultiBlockDataGroupFilter for vtkCompositeData,
2166 and vtkAppendFilter for anything else.
2168 If arg1 is an integer then one particular processor's output is brought to
2169 the client. In serial runs the arg is ignored. If you have a filter that
2170 computes results in parallel and brings them to the root node, then set
2173 If arg1 and arg2 are a algorithms, for example vtkMinMax, the algorithm
2174 will be applied to the data to obtain some result. Here arg1 will be
2175 applied pre-gather and arg2 will be applied post-gather. In parallel
2176 runs the algorithm will be run on each processor to make intermediate
2177 results and then again on the root processor over all of the
2178 intermediate results to create a global result.
2180 Optional argument idx is used to specify the output port number to fetch the
2181 data from. Default is port 0.
2186 reducer = filters.ReductionFilter(Input=OutputPort(input,idx))
2188 #create the pipeline that reduces and transmits the data
2190 cdinfo = input.GetDataInformation(idx).GetCompositeDataInformation()
2191 if cdinfo.GetDataIsComposite():
2192 print "use composite data append"
2193 reducer.PostGatherHelperName = "vtkMultiBlockDataGroupFilter"
2195 elif input.GetDataInformation(idx).GetDataClassName() == "vtkPolyData":
2196 print "use append poly data filter"
2197 reducer.PostGatherHelperName = "vtkAppendPolyData"
2199 elif input.GetDataInformation(idx).GetDataClassName() == "vtkRectilinearGrid":
2200 print "use append rectilinear grid filter"
2201 reducer.PostGatherHelperName = "vtkAppendRectilinearGrid"
2203 elif input.GetDataInformation(idx).IsA("vtkDataSet"):
2204 print "use unstructured append filter"
2205 reducer.PostGatherHelperName = "vtkAppendFilter"
2207 elif type(arg1) is types.IntType:
2208 reducer.PassThrough = arg1
2211 reducer.PreGatherHelper = arg1
2212 reducer.PostGatherHelper = arg2
2215 reducer.UpdatePipeline()
2216 dataInfo = reducer.GetDataInformation(0)
2217 dataType = dataInfo.GetDataSetType()
2218 if dataInfo.GetCompositeDataSetType() > 0:
2219 dataType = dataInfo.GetCompositeDataSetType()
2221 fetcher = filters.ClientServerMoveData(Input=reducer)
2222 fetcher.OutputDataType = dataType
2223 fetcher.WholeExtent = dataInfo.GetExtent()[:]
2225 fetcher.UpdatePipeline()
2227 op = fetcher.GetClientSideObject().GetOutputDataObject(0)
2228 opc = op.NewInstance()
2230 opc.UnRegister(None)
2233 def AnimateReader(reader, view, filename=None):
2234 """This is a utility function that, given a reader and a view
2235 animates over all time steps of the reader. If the optional
2236 filename is provided, a movie is created (type depends on the
2237 extension of the filename."""
2239 raise RuntimeError, "No reader was specified, cannot animate."
2241 raise RuntimeError, "No view was specified, cannot animate."
2242 # Create an animation scene
2243 scene = animation.AnimationScene()
2245 # We need to have the reader and the view registered with
2246 # the time keeper. This is how the scene gets its time values.
2248 tk = ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
2249 scene.TimeKeeper = tk
2251 tk = misc.TimeKeeper()
2252 scene.TimeKeeper = tk
2254 if not reader in tk.TimeSources:
2255 tk.TimeSources.append(reader)
2256 if not view in tk.Views:
2257 tk.Views.append(view)
2261 scene.ViewModules = [view]
2262 # Update the reader to get the time information
2263 reader.UpdatePipelineInformation()
2264 # Animate from 1st time step to last
2265 scene.StartTime = reader.TimestepValues.GetData()[0]
2266 scene.EndTime = reader.TimestepValues.GetData()[-1]
2268 # Each frame will correspond to a time step
2269 scene.PlayMode = 2 #Snap To Timesteps
2271 # Create a special animation cue for time.
2272 cue = animation.TimeAnimationCue()
2273 cue.AnimatedProxy = view
2274 cue.AnimatedPropertyName = "ViewTime"
2278 writer = vtkSMAnimationSceneImageWriter()
2279 writer.SetFileName(filename)
2280 writer.SetFrameRate(1)
2281 writer.SetAnimationScene(scene.SMProxy)
2283 # Now save the animation.
2284 if not writer.Save():
2285 raise RuntimeError, "Saving of animation failed!"
2290 def GetProgressPrintingIsEnabled():
2291 return progressObserverTag is not None
2293 def SetProgressPrintingEnabled(value):
2294 """Is not supported because of not supported observers"""
2297 def ToggleProgressPrinting():
2298 """Turn on/off printing of progress. See SetProgressPrintingEnabled."""
2299 SetProgressPrintingEnabled(not GetProgressPrintingIsEnabled())
2302 """Although not required, this can be called at exit to cleanup."""
2303 global progressObserverTag
2304 # Make sure to remove the observer
2305 if progressObserverTag:
2306 ToggleProgressPrinting()
2307 vtkInitializationHelper.Finalize()
2311 def _getPyProxy(smproxy, outputPort=0):
2312 """Returns a python wrapper for a server manager proxy. This method
2313 first checks if there is already such an object by looking in the
2314 _pyproxies group and returns it if found. Otherwise, it creates a
2315 new one. Proxies register themselves in _pyproxies upon creation."""
2318 if (smproxy, outputPort) in _pyproxies:
2319 return _pyproxies[(smproxy, outputPort)]()
2321 xmlName = smproxy.GetXMLName()
2322 if smproxy.GetXMLLabel():
2323 xmlName = smproxy.GetXMLLabel()
2324 classForProxy = _findClassForProxy(_make_name_valid(xmlName), smproxy.GetXMLGroup())
2326 retVal = classForProxy(proxy=smproxy, port=outputPort)
2328 retVal = Proxy(proxy=smproxy, port=outputPort)
2331 def _makeUpdateCameraMethod(rv):
2332 """ This internal method is used to create observer methods """
2333 if not hasattr(rv(), "BlockUpdateCamera"):
2334 rv().add_attribute("BlockUpdateCamera", False)
2335 def UpdateCamera(obj, string):
2336 if not rv().BlockUpdateCamera:
2337 # used to avoid some nasty recursion that occurs when interacting in
2339 rv().BlockUpdateCamera = True
2340 rv().SynchronizeCameraProperties()
2341 rv().BlockUpdateCamera = False
2344 def _createInitialize(group, name):
2345 """Internal method to create an Initialize() method for the sub-classes
2349 def aInitialize(self, connection=None, update=True):
2351 connection = ActiveConnection
2353 raise RuntimeError,\
2354 'Cannot create a proxy without a session.'
2355 if not connection.Session.GetProxyDefinitionManager().HasDefinition(pgroup, pname):
2356 error_msg = "The connection does not provide any definition for %s." % pname
2357 raise RuntimeError, error_msg
2358 self.InitializeFromProxy(\
2359 CreateProxy(pgroup, pname, connection.Session), update)
2362 def _createGetProperty(pName):
2363 """Internal method to create a GetXXX() method where XXX == pName."""
2365 def getProperty(self):
2366 return self.GetPropertyValue(propName)
2369 def _createSetProperty(pName):
2370 """Internal method to create a SetXXX() method where XXX == pName."""
2372 def setProperty(self, value):
2373 return self.SetPropertyWithName(propName, value)
2376 def _findClassForProxy(xmlName, xmlGroup):
2377 """Given the xmlName for a proxy, returns a Proxy class. Note
2378 that if there are duplicates, the first one is returned."""
2379 global sources, filters, writers, rendering, animation, implicit_functions,\
2380 piecewise_functions, extended_sources, misc
2383 if xmlGroup == "sources":
2384 return sources.__dict__[xmlName]
2385 elif xmlGroup == "filters":
2386 return filters.__dict__[xmlName]
2387 elif xmlGroup == "implicit_functions":
2388 return implicit_functions.__dict__[xmlName]
2389 elif xmlGroup == "piecewise_functions":
2390 return piecewise_functions.__dict__[xmlName]
2391 elif xmlGroup == "writers":
2392 return writers.__dict__[xmlName]
2393 elif xmlGroup == "extended_sources":
2394 return extended_sources.__dict__[xmlName]
2395 elif xmlName in rendering.__dict__:
2396 return rendering.__dict__[xmlName]
2397 elif xmlName in animation.__dict__:
2398 return animation.__dict__[xmlName]
2399 elif xmlName in misc.__dict__:
2400 return misc.__dict__[xmlName]
2404 def _printProgress(caller, event):
2405 """The default event handler for progress. Prints algorithm
2406 name and 1 '.' per 10% progress."""
2407 global currentAlgorithm, currentProgress
2409 pm = vtkProcessModule.GetProcessModule()
2410 progress = pm.GetLastProgress() / 10
2411 # If we got a 100% as the first thing, ignore
2412 # This is to get around the fact that some vtk
2413 # algorithms report 100% more than once (which is
2415 if not currentAlgorithm and progress == 10:
2417 alg = pm.GetLastProgressName()
2418 if alg != currentAlgorithm and alg:
2419 if currentAlgorithm:
2420 while currentProgress <= 10:
2422 sys.stdout.write(".")
2423 currentProgress += 1
2427 currentAlgorithm = alg
2428 while currentProgress <= progress:
2430 sys.stdout.write(".")
2431 #sys.stdout.write("%d " % pm.GetLastProgress())
2432 currentProgress += 1
2435 currentAlgorithm = None
2438 def updateModules(m):
2439 """Called when a plugin is loaded, this method updates
2440 the proxy class object in all known modules."""
2442 createModule("sources", m.sources)
2443 createModule("filters", m.filters)
2444 createModule("writers", m.writers)
2445 createModule("representations", m.rendering)
2446 createModule("views", m.rendering)
2447 createModule("lookup_tables", m.rendering)
2448 createModule("textures", m.rendering)
2449 createModule('cameramanipulators', m.rendering)
2450 createModule("animation", m.animation)
2451 createModule("misc", m.misc)
2452 createModule('animation_keyframes', m.animation)
2453 createModule('implicit_functions', m.implicit_functions)
2454 createModule('piecewise_functions', m.piecewise_functions)
2455 createModule("extended_sources", m.extended_sources)
2456 createModule("incremental_point_locators", m.misc)
2458 def _createModules(m):
2459 """Called when the module is loaded, this creates sub-
2460 modules for all know proxy groups."""
2462 m.sources = createModule('sources')
2463 m.filters = createModule('filters')
2464 m.writers = createModule('writers')
2465 m.rendering = createModule('representations')
2466 createModule('views', m.rendering)
2467 createModule("lookup_tables", m.rendering)
2468 createModule("textures", m.rendering)
2469 createModule('cameramanipulators', m.rendering)
2470 m.animation = createModule('animation')
2471 createModule('animation_keyframes', m.animation)
2472 m.implicit_functions = createModule('implicit_functions')
2473 m.piecewise_functions = createModule('piecewise_functions')
2474 m.extended_sources = createModule("extended_sources")
2475 m.misc = createModule("misc")
2476 createModule("incremental_point_locators", m.misc)
2478 class PVModule(object):
2481 def _make_name_valid(name):
2482 """Make a string into a valid Python variable name."""
2486 valid_chars = "_%s%s" % (string.ascii_letters, string.digits)
2487 name = str().join([c for c in name if c in valid_chars])
2488 if not name[0].isalpha():
2492 def createModule(groupName, mdl=None):
2493 """Populates a module with proxy classes defined in the given group.
2494 If mdl is not specified, it also creates the module"""
2495 global ActiveConnection
2497 if not ActiveConnection:
2498 raise RuntimeError, "Please connect to a server using \"Connect\""
2500 pxm = ProxyManager()
2501 # Use prototypes to find all proxy types.
2502 pxm.InstantiateGroupPrototypes(groupName)
2508 definitionIter = pxm.NewDefinitionIterator(groupName)
2509 for i in definitionIter:
2510 proxyName = i['key']
2511 proto = pxm.GetPrototypeProxy(groupName, proxyName)
2513 print "Error while loading %s/%s %s"%(groupName, i['group'], proxyName)
2516 if proto.GetXMLLabel():
2517 pname = proto.GetXMLLabel()
2518 pname = _make_name_valid(pname)
2521 if pname in mdl.__dict__:
2523 print "Warning: %s is being overwritten. This may point to an issue in the ParaView configuration files" % pname
2525 # Create an Initialize() method for this sub-class.
2526 cdict['Initialize'] = _createInitialize(groupName, proxyName)
2527 iter = PropertyIterator(proto)
2528 # Add all properties as python properties.
2530 propName = iter.GetKey()
2531 if (prop.GetInformationOnly() and propName != "TimestepValues" ) \
2532 or prop.GetIsInternal():
2535 names = [iter.PropertyLabel]
2538 if prop.GetDocumentation():
2539 propDoc = prop.GetDocumentation().GetDescription()
2541 name = _make_name_valid(name)
2543 cdict[name] = property(_createGetProperty(propName),
2544 _createSetProperty(propName),
2547 # Add the documentation as the class __doc__
2548 if proto.GetDocumentation() and \
2549 proto.GetDocumentation().GetDescription():
2550 doc = proto.GetDocumentation().GetDescription()
2553 cdict['__doc__'] = doc
2554 # Create the new type
2555 if proto.GetXMLName() == "ExodusIIReader":
2556 superclasses = (ExodusIIReaderProxy,)
2557 elif proto.IsA("vtkSMSourceProxy"):
2558 superclasses = (SourceProxy,)
2559 elif proto.IsA("vtkSMViewLayoutProxy"):
2560 superclasses = (ViewLayoutProxy,)
2562 superclasses = (Proxy,)
2564 cobj = type(pname, superclasses, cdict)
2565 # Add it to the modules dictionary
2566 mdl.__dict__[pname] = cobj
2570 def __determineGroup(proxy):
2571 """Internal method"""
2574 xmlgroup = proxy.GetXMLGroup()
2575 xmlname = proxy.GetXMLName()
2576 if xmlgroup == "sources":
2577 if xmlname in ["BlockSelectionSource",
2578 "FrustumSelectionSource",
2579 "GlobalIDSelectionSource",
2580 "PedigreeIDSelectionSource",
2581 "IDSelectionSource",
2582 "CompositeDataIDSelectionSource",
2583 "HierarchicalDataIDSelectionSource",
2584 "ThresholdSelectionSource",
2585 "LocationSelectionSource"]:
2586 return "selection_sources"
2588 elif xmlgroup == "filters":
2590 elif xmlgroup == "representations":
2591 if xmlname == "ScalarBarWidgetRepresentation":
2592 return "scalar_bars"
2593 return "representations"
2594 elif xmlgroup == "animation_keyframes":
2599 def __determineName(proxy, group):
2600 global __nameCounter
2601 name = _make_name_valid(proxy.GetXMLLabel())
2604 if not __nameCounter.has_key(name):
2605 __nameCounter[name] = 1
2608 __nameCounter[name] += 1
2609 val = __nameCounter[name]
2610 return "%s%d" % (name, val)
2612 def __getName(proxy, group):
2613 pxm = ProxyManager(proxy.GetSession())
2614 if isinstance(proxy, Proxy):
2615 proxy = proxy.SMProxy
2616 return pxm.GetProxyName(group, proxy)
2618 class MissingRegistrationInformation(Exception):
2619 """Exception for missing registration information. Raised when a name or group
2620 is not specified or when a group cannot be deduced."""
2623 class MissingProxy(Exception):
2624 """Exception fired when the requested proxy is missing."""
2627 def Register(proxy, **extraArgs):
2628 """Registers a proxy with the proxy manager. If no 'registrationGroup' is
2629 specified, then the group is inferred from the type of the proxy.
2630 'registrationName' may be specified to register with a particular name
2631 otherwise a default name will be created."""
2632 # TODO: handle duplicate registration
2633 if "registrationGroup" in extraArgs:
2634 registrationGroup = extraArgs["registrationGroup"]
2636 registrationGroup = __determineGroup(proxy)
2638 if "registrationName" in extraArgs:
2639 registrationName = extraArgs["registrationName"]
2641 registrationName = __determineName(proxy, registrationGroup)
2642 if registrationGroup and registrationName:
2643 pxm = ProxyManager()
2644 pxm.RegisterProxy(registrationGroup, registrationName, proxy)
2646 raise MissingRegistrationInformation, "Registration error %s %s." % (registrationGroup, registrationName)
2647 return (registrationGroup, registrationName)
2649 def UnRegister(proxy, **extraArgs):
2650 """UnRegisters proxies registered using Register()."""
2651 if "registrationGroup" in extraArgs:
2652 registrationGroup = extraArgs["registrationGroup"]
2654 registrationGroup = __determineGroup(proxy)
2656 if "registrationName" in extraArgs:
2657 registrationName = extraArgs["registrationName"]
2659 registrationName = __getName(proxy, registrationGroup)
2661 if registrationGroup and registrationName:
2662 pxm = ProxyManager()
2663 pxm.UnRegisterProxy(registrationGroup, registrationName, proxy)
2665 raise RuntimeError, "UnRegistration error."
2666 return (registrationGroup, registrationName)
2669 """This simple demonstration creates a sphere, renders it and delivers
2670 it to the client using Fetch. It returns a tuple of (data, render
2672 if not ActiveConnection:
2674 ss = sources.Sphere(Radius=2, ThetaResolution=32)
2675 shr = filters.Shrink(Input=OutputPort(ss,0))
2677 app = filters.AppendDatasets()
2678 app.Input = [shr, cs]
2679 rv = CreateRenderView()
2680 rep = CreateRepresentation(app, rv)
2687 def demo2(fname="/Users/berk/Work/ParaViewData/Data/disk_out_ref.ex2"):
2688 """This method demonstrates the user of a reader, representation and
2689 view. It also demonstrates how meta-data can be obtained using proxies.
2690 Make sure to pass the full path to an exodus file. Also note that certain
2691 parameters are hard-coded for disk_out_ref.ex2 which can be found
2692 in ParaViewData. This method returns the render view."""
2693 if not ActiveConnection:
2695 # Create the exodus reader and specify a file name
2696 reader = sources.ExodusIIReader(FileName=fname)
2697 # Get the list of point arrays.
2698 arraySelection = reader.PointVariables
2699 print arraySelection.Available
2701 arraySelection.SetData(arraySelection.Available)
2703 # Next create a default render view appropriate for the session type.
2704 rv = CreateRenderView()
2705 # Create the matching representation
2706 rep = CreateRepresentation(reader, rv)
2707 rep.Representation = 1 # Wireframe
2708 # Black background is not pretty
2709 rv.Background = [0.4, 0.4, 0.6]
2711 # Reset the camera to include the whole thing
2714 # Change the elevation of the camera. See VTK documentation of vtkCamera
2715 # for camera parameters.
2716 c = rv.GetActiveCamera()
2719 # Now that the reader execute, let's get some information about it's
2721 pdi = reader[0].PointData
2722 # This prints a list of all read point data arrays as well as their
2724 print 'Number of point arrays:', len(pdi)
2725 for i in range(len(pdi)):
2727 print "----------------"
2728 print "Array:", i, ai.Name, ":"
2729 numComps = ai.GetNumberOfComponents()
2730 print "Number of components:", numComps
2731 for j in range(numComps):
2732 print "Range:", ai.GetRange(j)
2733 # White is boring. Let's color the geometry using a variable.
2734 # First create a lookup table. This object controls how scalar
2735 # values are mapped to colors. See VTK documentation for
2737 lt = rendering.PVLookupTable()
2738 # Assign it to the representation
2739 rep.LookupTable = lt
2740 # Color by point array called Pres
2741 rep.ColorAttributeType = 0 # point data
2742 rep.ColorArrayName = "Pres"
2743 # Add to RGB points. These are tuples of 4 values. First one is
2744 # the scalar values, the other 3 the RGB values. This list has
2745 # 2 points: Pres: 0.00678, color: blue, Pres: 0.0288, color: red
2746 lt.RGBPoints = [0.00678, 0, 0, 1, 0.0288, 1, 0, 0]
2747 lt.ColorSpace = 1 # HSV
2752 """This method demonstrates the use of servermanager with numpy as
2753 well as pylab for plotting. It creates an artificial data sources,
2754 probes it with a line, delivers the result to the client using Fetch
2755 and plots it using pylab. This demo requires numpy and pylab installed.
2756 It returns a tuple of (data, render view)."""
2757 import paraview.numpy_support
2760 if not ActiveConnection:
2762 # Create a synthetic data source
2763 source = sources.Wavelet()
2764 # Let's get some information about the data. First, for the
2766 source.UpdatePipeline()
2768 di = source.GetDataInformation()
2769 print "Data type:", di.GetPrettyDataTypeString()
2770 print "Extent:", di.GetExtent()
2771 print "Array name:", \
2772 source[0].PointData[0].Name
2774 rv = CreateRenderView()
2776 rep1 = CreateRepresentation(source, rv)
2777 rep1.Representation = 3 # outline
2779 # Let's apply a contour filter
2780 cf = filters.Contour(Input=source, ContourValues=[200])
2782 # Select the array to contour by
2783 #cf.SelectInputScalars = 'RTData'
2785 rep2 = CreateRepresentation(cf, rv)
2787 rv.Background = (0.4, 0.4, 0.6)
2788 # Reset the camera to include the whole thing
2793 # Now, let's probe the data
2794 probe = filters.ResampleWithDataset(Input=source)
2796 line = sources.Line(Resolution=60)
2797 # that spans the dataset
2798 bounds = di.GetBounds()
2799 print "Bounds: ", bounds
2800 line.Point1 = bounds[0:6:2]
2801 line.Point2 = bounds[1:6:2]
2805 # Render with the line
2806 rep3 = CreateRepresentation(line, rv)
2809 # Now deliver it to the client. Remember, this is for small data.
2811 # Convert it to a numpy array
2812 data = paraview.numpy_support.vtk_to_numpy(
2813 data.GetPointData().GetArray("RTData"))
2814 # Plot it using matplotlib
2818 return (data, rv, probe)
2820 def demo4(fname="/Users/berk/Work/ParaViewData/Data/can.ex2"):
2821 """This method demonstrates the user of AnimateReader for
2822 creating animations."""
2823 if not ActiveConnection:
2825 reader = sources.ExodusIIReader(FileName=fname)
2826 view = CreateRenderView()
2827 repr = CreateRepresentation(reader, view)
2831 c = view.GetActiveCamera()
2833 return AnimateReader(reader, view)
2837 """ Simple sphere animation"""
2838 if not ActiveConnection:
2840 sphere = sources.Sphere()
2841 view = CreateRenderView()
2842 repr = CreateRepresentation(sphere, view)
2848 # Create an animation scene
2849 scene = animation.AnimationScene()
2851 scene.ViewModules = [view]
2853 # Create a cue to animate the StartTheta property
2854 cue = animation.KeyFrameAnimationCue()
2855 cue.AnimatedProxy = sphere
2856 cue.AnimatedPropertyName = "StartTheta"
2857 # Add it to the scene's cues
2860 # Create 2 keyframes for the StartTheta track
2861 keyf0 = animation.CompositeKeyFrame()
2862 keyf0.Type = 2 # Set keyframe interpolation type to Ramp.
2863 # At time = 0, value = 0
2865 keyf0.KeyValues= [0]
2867 keyf1 = animation.CompositeKeyFrame()
2868 # At time = 1.0, value = 200
2870 keyf1.KeyValues= [200]
2873 cue.KeyFrames = [keyf0, keyf1]
2878 ASSOCIATIONS = { 'POINTS' : 0, 'CELLS' : 1, 'VERTICES' : 4, 'EDGES' : 5, 'ROWS' : 6}
2880 # Users can set the active connection which will be used by API
2881 # to create proxies etc when no connection argument is passed.
2882 # Connect() automatically sets this if it is not already set.
2883 ActiveConnection = None
2885 # Fields for multi-server support
2886 MultiServerConnections = None
2888 # API for multi-server support
2889 def enableMultiServer(multiServer=True):
2890 """This method enable the current servermanager to support several
2891 connections. Once we enable the multi-server support, the user can create
2892 as many connection as he want and switch from one to another in order to
2893 create and manage proxy."""
2894 global MultiServerConnections, ActiveConnection
2895 if not multiServer and MultiServerConnections:
2896 raise RuntimeError, "Once we enable Multi-Server support we can not get back"
2897 MultiServerConnections = []
2898 if ActiveConnection:
2899 MultiServerConnections.append(ActiveConnection)
2901 def switchActiveConnection(newActiveConnection=None):
2902 """Switch active connection to be the provided one or if none just pick the
2904 global MultiServerConnections, ActiveConnection
2905 if MultiServerConnections == None:
2906 raise RuntimeError, "enableMultiServer() must be called before"
2908 # Manage the case when no connection is provided
2909 if newActiveConnection:
2910 ActiveConnection = newActiveConnection
2911 __exposeActiveModules__()
2912 # Update active session for ParaView
2913 if vtkSMProxyManager.GetProxyManager().GetActiveSession() != ActiveConnection.Session:
2914 vtkSMProxyManager.GetProxyManager().SetActiveSession(ActiveConnection.Session)
2915 return ActiveConnection
2917 for connection in MultiServerConnections:
2918 if connection != ActiveConnection:
2919 ActiveConnection = connection
2920 __exposeActiveModules__()
2921 # Update active session for ParaView
2922 if vtkSMProxyManager.GetProxyManager().GetActiveSession() != ActiveConnection.Session:
2923 vtkSMProxyManager.GetProxyManager().SetActiveSession(ActiveConnection.Session)
2924 return ActiveConnection
2927 # Needs to be called when paraview module is loaded from python instead
2928 # of pvpython, pvbatch or GUI.
2929 if not vtkProcessModule.GetProcessModule():
2930 # pvoptions = None Not applicable for SALOME Python console
2931 # if paraview.options.batch:
2932 # pvoptions = vtkPVOptions();
2933 # pvoptions.SetProcessType(0x40)
2934 # if paraview.options.symmetric:
2935 # pvoptions.SetSymmetricMPIMode(True)
2936 vtkInitializationHelper.Initialize(sys.executable,
2937 vtkProcessModule.PROCESS_CLIENT, pvoptions)
2939 # Initialize progress printing. Can be turned off by calling
2940 # ToggleProgressPrinting() again.
2941 progressObserverTag = None
2942 currentAlgorithm = False
2945 ToggleProgressPrinting()
2949 # Create needed sub-modules
2950 # We can no longer create modules, unless we have connected to a server.
2953 # Set up our custom importer (if possible)
2954 loader = _ModuleLoader()
2955 sys.meta_path.append(loader)
2957 def __InitAfterConnect__(connection):
2959 This function is called everytime after a server connection is made.
2960 Since the ProxyManager and all proxy definitions are changed every time a
2961 new connection is made, we re-create all the modules
2963 _createModules(connection.Modules)
2964 ## VSV fromFilter is alwais False for SALOME because it can't be changed from ParaView code
2965 #if not paraview.fromFilter:
2966 # fromFilter is set when this module is imported from the programmable
2968 # global _defUpdater
2969 # _defUpdater = __DefinitionUpdater()
2970 connection.AttachDefinitionUpdater()
2973 def __exposeActiveModules__():
2974 """Update servermanager submodules to point to the current
2975 ActiveConnection.Modules.*"""
2976 # Expose all active module to the current servermanager module
2977 if ActiveConnection:
2978 for m in [mName for mName in dir(ActiveConnection.Modules) if mName[0] != '_' ]:
2979 exec "global %s;%s = ActiveConnection.Modules.%s" % (m,m,m)
2981 # Definitions for working in SALOME GUI mode
2982 #aParams = myParavis.GetConnectionParameters()
2983 #ActiveConnection = Connect()
2984 ##Connection(aParams[0])
2985 #ActiveConnection.SetHost(aParams[1], aParams[2], aParams[3], aParams[4], aParams[5])
2986 #ToggleProgressPrinting()
2991 if hasattr(sys, "ps1"):
2992 # session is interactive.
2993 print vtkSMProxyManager.GetParaViewSourceVersion();
2995 def GetConnectionFromId(id):
2996 for connection in MultiServerConnections:
2997 if connection.ID == id:
3001 def GetConnectionFromSession(session):
3002 for connection in MultiServerConnections:
3003 if connection.Session == session:
3007 def GetConnectionAt(index):
3008 return MultiServerConnections[index]
3010 def GetNumberOfConnections():
3011 return len(MultiServerConnections)
3013 #VTN: Problem during execution
3014 #atexit.register(vtkPythonProgrammableFilter.DeleteGlobalPythonInterpretor)