Salome HOME
9bce3efcc460a330c3f67c9738810f3aef3a2fd2
[modules/paravis.git] / src / PV_SWIG / paravisSM.py
1 r"""servermanager is a module for using paraview server manager in Python.
2 One can always use the server manager API directly. However, this module
3 provides an interface easier to use from Python by wrapping several VTK
4 classes around Python classes.
5
6 Note that, upon load, this module will create several sub-modules: sources,
7 filters and rendering. These modules can be used to instantiate specific
8 proxy types. For a list, try "dir(servermanager.sources)"
9
10 A simple example:
11   from paraview.servermanager import *
12
13   # Creates a new built-in connection and makes it the active connection.
14   Connect()
15
16   # Creates a new render view on the active connection.
17   renModule = CreateRenderView()
18
19   # Create a new sphere proxy on the active connection and register it
20   # in the sources group.
21   sphere = sources.SphereSource(registrationGroup="sources", ThetaResolution=16, PhiResolution=32)
22
23   # Create a representation for the sphere proxy and adds it to the render
24   # module.
25   display = CreateRepresentation(sphere, renModule)
26
27   renModule.StillRender()
28 """
29
30
31
32 import re, os, new, sys
33 from paravis import *
34
35
36
37 def _wrap_property(proxy, smproperty):
38     """ Internal function.
39     Given a server manager property and its domains, returns the
40     appropriate python object.
41     """
42     property = None
43     if smproperty.IsA("vtkSMStringVectorProperty"):
44         al = smproperty.GetDomain("array_list")
45         if  al and al.IsA("vtkSMArraySelectionDomain") and smproperty.GetRepeatable():
46             property = ArrayListProperty(proxy, smproperty)
47         elif al and al.IsA("vtkSMArrayListDomain") and smproperty.GetNumberOfElements() == 5:
48             property = ArraySelectionProperty(proxy, smproperty)
49         else:
50             iter = smproperty.NewDomainIterator()
51             isFileName = False
52             while not iter.IsAtEnd():
53                 if iter.GetDomain().IsA("vtkSMFileListDomain"):
54                     isFileName = True
55                     break
56                 iter.Next()
57             iter.UnRegister(None)
58             if isFileName:
59                 property = FileNameProperty(proxy, smproperty)
60             elif _make_name_valid(smproperty.GetXMLLabel()) == 'ColorArrayName':
61                 property = ColorArrayProperty(proxy, smproperty)
62             else:
63                 property = VectorProperty(proxy, smproperty)
64     elif smproperty.IsA("vtkSMVectorProperty"):
65         if smproperty.IsA("vtkSMIntVectorProperty") and smproperty.GetDomain("enum"):
66             property = EnumerationProperty(proxy, smproperty)
67         else:
68             property = VectorProperty(proxy, smproperty)
69     elif smproperty.IsA("vtkSMInputProperty"):
70         property = InputProperty(proxy, smproperty)
71     elif smproperty.IsA("vtkSMProxyProperty"):
72         property = ProxyProperty(proxy, smproperty)
73     else:
74         property = Property(proxy, smproperty)
75     return property
76
77 class Proxy(object):
78     """Proxy for a server side object. A proxy manages the lifetime of
79     one or more server manager objects. It also provides an interface
80     to set and get the properties of the server side objects. These
81     properties are presented as Python properties. For example,
82     you can set a property Foo using the following:
83      proxy.Foo = (1,2)
84     or
85      proxy.Foo.SetData((1,2))
86     or
87      proxy.Foo[0:2] = (1,2)
88     For more information, see the documentation of the property which
89     you can obtain with
90     help(proxy.Foo).
91
92     This class also provides an iterator which can be used to iterate
93     over all properties.
94     eg:
95       proxy = Proxy(proxy=smproxy)
96       for property in proxy:
97           print property
98
99     For advanced users:
100     This is a python class that wraps a vtkSMProxy.. Makes it easier to
101     set/get properties.
102     Instead of:
103      proxy.GetProperty("Foo").SetElement(0, 1)
104      proxy.GetProperty("Foo").SetElement(0, 2)
105     you can do:
106      proxy.Foo = (1,2)
107     or
108      proxy.Foo.SetData((1,2))
109     or
110      proxy.Foo[0:2] = (1,2)
111     Instead of:
112       proxy.GetProperty("Foo").GetElement(0)
113     you can do:
114       proxy.Foo.GetData()[0]
115     or
116       proxy.Foo[0]
117     For proxy properties, you can use append:
118      proxy.GetProperty("Bar").AddProxy(foo)
119     you can do:
120      proxy.Bar.append(foo)
121     Properties support most of the list API. See VectorProperty and
122     ProxyProperty documentation for details.
123
124     Please note that some of the methods accessible through the Proxy
125     class are not listed by help() because the Proxy objects forward
126     unresolved attributes to the underlying object. To get the full list,
127     see also dir(proxy.SMProxy). See also the doxygen based documentation
128     of the vtkSMProxy C++ class.
129     """
130
131     def __init__(self, **args):
132         """ Default constructor. It can be used to initialize properties
133         by passing keyword arguments where the key is the name of the
134         property. In addition registrationGroup and registrationName (optional)
135         can be specified (as keyword arguments) to automatically register
136         the proxy with the proxy manager. """
137         self.add_attribute('Observed', None)
138         self.add_attribute('ObserverTag', -1)
139         self.add_attribute('_Proxy__Properties', {})
140         self.add_attribute('_Proxy__LastAttrName', None)
141         self.add_attribute('SMProxy', None)
142         self.add_attribute('Port', 0)
143
144         if 'port' in args:
145             self.Port = args['port']
146             del args['port']
147
148         if 'proxy' in args:
149             self.InitializeFromProxy(args['proxy'])
150             del args['proxy']
151         else:
152             self.Initialize()
153         if 'registrationGroup' in args:
154             registrationGroup = args['registrationGroup']
155             del args['registrationGroup']
156             registrationName = self.SMProxy.GetSelfIDAsString()
157             if 'registrationName' in args:
158                 registrationName = args['registrationName']
159                 del args['registrationName']
160             pxm = ProxyManager()
161             pxm.RegisterProxy(registrationGroup, registrationName, self.SMProxy)
162         for key in args.keys():
163             setattr(self, key, args[key])
164         self.UpdateVTKObjects()
165         # Visit all properties so that they are created
166         for prop in self:
167             pass
168
169     def __setattr__(self, name, value):
170         try:
171             setter = getattr(self.__class__, name)
172             setter = setter.__set__
173         except AttributeError:
174             if not hasattr(self, name):
175                 raise AttributeError("Attribute %s does not exist. " % name +
176                   " This class does not allow addition of new attributes to avoid " +
177                   "mistakes due to typos. Use add_attribute() if you really want " +
178                   "to add this attribute.")
179             self.__dict__[name] = value
180         else:
181             setter(self, value)
182
183     def add_attribute(self, name, value):
184         self.__dict__[name] = value
185
186     def __del__(self):
187         """Destructor. Cleans up all observers as well as remove
188         the proxy from the _pyproxies dictionary"""
189         # Make sure that we remove observers we added
190         if self.Observed:
191             observed = self.Observed
192             tag = self.ObserverTag
193             self.Observed = None
194             self.ObserverTag = -1
195             observed.RemoveObserver(tag)
196         if self.SMProxy and (self.SMProxy, self.Port) in _pyproxies:
197             del _pyproxies[(self.SMProxy, self.Port)]
198
199     def InitializeFromProxy(self, aProxy):
200         """Constructor. Assigns proxy to self.SMProxy, updates the server
201         object as well as register the proxy in _pyproxies dictionary."""
202         import weakref
203         self.SMProxy = aProxy
204         self.SMProxy.UpdateVTKObjects()
205         _pyproxies[(self.SMProxy, self.Port)] = weakref.ref(self)
206
207     def Initialize(self):
208         "Overridden by the subclass created automatically"
209         pass
210
211     def __eq__(self, other):
212         "Returns true if the underlying SMProxies are the same."
213         if isinstance(other, Proxy):
214             ## VSV using IsSame instead
215             return self.SMProxy.IsSame(other.SMProxy)
216         return self.SMProxy.IsSame(other)
217
218     def __ne__(self, other):
219         "Returns false if the underlying SMProxies are the same."
220         return not self.__eq__(other)
221
222     def __iter__(self):
223         "Creates an iterator for the properties."
224         return PropertyIterator(self)
225
226     def SetPropertyWithName(self, pname, arg):
227         """Generic method for setting the value of a property."""
228         prop = self.GetProperty(pname)
229         if prop is None:
230             raise RuntimeError, "Property %s does not exist. Please check the property name for typos." % pname
231         prop.SetData(arg)
232
233     def GetPropertyValue(self, name):
234         """Returns a scalar for properties with 1 elements, the property
235         itself for vectors."""
236         p = self.GetProperty(name)
237         if isinstance(p, VectorProperty):
238             if p.GetNumberOfElements() == 1 and not p.GetRepeatable():
239                 if p.SMProperty.IsA("vtkSMStringVectorProperty") or not p.GetArgumentIsArray():
240                     return p[0]
241         elif isinstance(p, InputProperty):
242             if not p.GetMultipleInput():
243                 if len(p) > 0:
244                     return p[0]
245                 else:
246                     return None
247         elif isinstance(p, ProxyProperty):
248             if not p.GetRepeatable():
249                 if len(p) > 0:
250                     return p[0]
251                 else:
252                     return None
253         return p
254
255     def GetProperty(self, name):
256         """Given a property name, returns the property object."""
257         if name in self.__Properties and self.__Properties[name]():
258             return self.__Properties[name]()
259         smproperty = self.SMProxy.GetProperty(name)
260         # Maybe they are looking by the label. Try to match that.
261         if not smproperty:
262             iter = PropertyIterator(self)
263             for prop in iter:
264                 if name == _make_name_valid(iter.PropertyLabel):
265                     smproperty = prop.SMProperty
266                     break
267         if smproperty:
268             property = _wrap_property(self, smproperty)
269             if property is not None:
270                 import weakref
271                 self.__Properties[name] = weakref.ref(property)
272             return property
273         return None
274
275     def ListProperties(self):
276         """Returns a list of all property names on this proxy."""
277         property_list = []
278         iter = self.__iter__()
279         for property in iter:
280             name = _make_name_valid(iter.PropertyLabel)
281             if name:
282                 property_list.append(name)
283         return property_list
284
285     def __ConvertArgumentsAndCall(self, *args):
286         """ Internal function.
287         Used to call a function on SMProxy. Converts input and
288         output values as appropriate.
289         """
290         newArgs = []
291         for arg in args:
292             if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
293                 newArgs.append(arg.SMProxy)
294             else:
295                 newArgs.append(arg)
296         func = getattr(self.SMProxy, self.__LastAttrName)
297         retVal = func(*newArgs)
298         if type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProxy"):
299             return _getPyProxy(retVal)
300         elif type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProperty"):
301             return _wrap_property(self, retVal)
302         else:
303             return retVal
304
305     def __GetActiveCamera(self):
306         """ This method handles GetActiveCamera specially. It adds
307         an observer to the camera such that everytime it is modified
308         the render view updated"""
309         import weakref
310         c = self.SMProxy.GetActiveCamera()
311         # VSV: Observers are not supported
312 ##         if not c.HasObserver("ModifiedEvent"):
313 ##             self.ObserverTag =c.AddObserver("ModifiedEvent", _makeUpdateCameraMethod(weakref.ref(self)))
314 ##             self.Observed = c
315         return c
316
317     def __getattr__(self, name):
318         """With the exception of a few overloaded methods,
319         returns the SMProxy method"""
320         if not self.SMProxy:
321             raise AttributeError("class has no attribute %s" % name)
322             return None
323         # Handle GetActiveCamera specially.
324         if name == "GetActiveCamera" and \
325            hasattr(self.SMProxy, "GetActiveCamera"):
326             return self.__GetActiveCamera
327         if name == "SaveDefinition" and hasattr(self.SMProxy, "SaveDefinition"):
328             return self.__SaveDefinition
329         # If not a property, see if SMProxy has the method
330         try:
331             proxyAttr = getattr(self.SMProxy, name)
332             self.__LastAttrName = name
333             return self.__ConvertArgumentsAndCall
334         except:
335             pass
336         return getattr(self.SMProxy, name)
337
338 class SourceProxy(Proxy):
339     """Proxy for a source object. This class adds a few methods to Proxy
340     that are specific to sources. It also provides access to the output
341     ports. Output ports can be accessed by name or index:
342     > op = source[0]
343     or
344     > op = source['some name'].
345     """
346     def UpdatePipeline(self, time=None):
347         """This method updates the server-side VTK pipeline and the associated
348         data information. Make sure to update a source to validate the output
349         meta-data."""
350         if time:
351             self.SMProxy.UpdatePipeline(time)
352         else:
353             self.SMProxy.UpdatePipeline()
354         # Fetch the new information. This is also here to cause a receive
355         # on the client side so that progress works properly.
356         self.SMProxy.GetDataInformation()
357
358     def FileNameChanged(self):
359         "Called when the filename of a source proxy is changed."
360         self.UpdatePipelineInformation()
361
362     def UpdatePipelineInformation(self):
363         """This method updates the meta-data of the server-side VTK pipeline and
364         the associated information properties"""
365         self.SMProxy.UpdatePipelineInformation()
366
367     def GetDataInformation(self, idx=None):
368         """This method returns a DataInformation wrapper around a
369         vtkPVDataInformation"""
370         if not idx:
371             idx = self.Port
372         if self.SMProxy:
373             return DataInformation( \
374                 self.SMProxy.GetDataInformation(idx), \
375                 self.SMProxy, idx)
376
377     def __getitem__(self, idx):
378         """Given a slice, int or string, returns the corresponding
379         output port"""
380         if isinstance(idx, slice):
381             indices = idx.indices(self.SMProxy.GetNumberOfOutputPorts())
382             retVal = []
383             for i in range(*indices):
384                 retVal.append(OutputPort(self, i))
385             return retVal
386         elif isinstance(idx, int):
387             if idx >= self.SMProxy.GetNumberOfOutputPorts() or idx < 0:
388                 raise IndexError
389             return OutputPort(self, idx)
390         else:
391             return OutputPort(self, self.SMProxy.GetOutputPortIndex(idx))
392
393     def GetPointDataInformation(self):
394         """Returns the associated point data information."""
395         self.UpdatePipeline()
396         return FieldDataInformation(self.SMProxy, self.Port, "PointData")
397
398     def GetCellDataInformation(self):
399         """Returns the associated cell data information."""
400         self.UpdatePipeline()
401         return FieldDataInformation(self.SMProxy, self.Port, "CellData")
402
403     def GetFieldDataInformation(self):
404         """Returns the associated cell data information."""
405         self.UpdatePipeline()
406         return FieldDataInformation(self.SMProxy, self.Port, "FieldData")
407
408     PointData = property(GetPointDataInformation, None, None, "Returns point data information")
409     CellData = property(GetCellDataInformation, None, None, "Returns cell data information")
410     FieldData = property(GetFieldDataInformation, None, None, "Returns field data information")
411
412
413 class ExodusIIReaderProxy(SourceProxy):
414     """Special class to define convenience functions for array
415     selection."""
416
417     def FileNameChanged(self):
418         "Called when the filename changes. Selects all variables."
419         SourceProxy.FileNameChanged(self)
420         self.SelectAllVariables()
421
422     def SelectAllVariables(self):
423         "Select all available variables for reading."
424         for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
425                      'ElementVariables', 'GlobalVariables'):
426             f = getattr(self, prop)
427             f.SelectAll()
428
429     def DeselectAllVariables(self):
430         "Deselects all variables."
431         for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
432                      'ElementVariables', 'GlobalVariables'):
433             f = getattr(self, prop)
434             f.DeselectAll()
435
436 class Property(object):
437     """Generic property object that provides access to one of the properties of
438     a server object. This class does not allow setting/getting any values but
439     provides an interface to update a property using __call__. This can be used
440     for command properties that correspond to function calls without arguments.
441     For example,
442     > proxy.Foo()
443     would push a Foo property which may cause the proxy to call a Foo method
444     on the actual VTK object.
445
446     For advanced users:
447     Python wrapper around a vtkSMProperty with a simple interface.
448     In addition to all method provided by vtkSMProperty (obtained by
449     forwarding unknown attributes requests to the underlying SMProxy),
450     Property and sub-class provide a list API.
451
452     Please note that some of the methods accessible through the Property
453     class are not listed by help() because the Property objects forward
454     unresolved attributes to the underlying object. To get the full list,
455     see also dir(proxy.SMProperty). See also the doxygen based documentation
456     of the vtkSMProperty C++ class.
457     """
458     def __init__(self, proxy, smproperty):
459         """Default constructor. Stores a reference to the proxy."""
460         import weakref
461         self.SMProperty = smproperty
462         self.Proxy = proxy
463
464     def __repr__(self):
465         """Returns a string representation containing property name
466         and value"""
467         if not type(self) is Property:
468             if self.GetData() is not None:
469                 repr = self.GetData().__repr__()
470             else:
471                 repr = "None"
472         else:
473             repr = "Property name= "
474             name = self.Proxy.GetPropertyName(self.SMProperty)
475             if name:
476                 repr += name
477             else:
478                 repr += "Unknown"
479
480         return repr
481
482     def __call__(self):
483         """Forces a property update using InvokeCommand."""
484         if type(self) is Property:
485             self.Proxy.SMProxy.InvokeCommand(self._FindPropertyName())
486         else:
487             raise RuntimeError, "Cannot invoke this property"
488
489     def _FindPropertyName(self):
490         "Returns the name of this property."
491         return self.Proxy.GetPropertyName(self.SMProperty)
492
493     def _UpdateProperty(self):
494         "Pushes the value of this property to the server."
495         # For now, we are updating all properties. This is due to an
496         # issue with the representations. Their VTK objects are not
497         # created until Input is set therefore, updating a property
498         # has no effect. Updating all properties everytime one is
499         # updated has the effect of pushing values set before Input
500         # when Input is updated.
501         # self.Proxy.SMProxy.UpdateProperty(self._FindPropertyName())
502         self.Proxy.SMProxy.UpdateVTKObjects()
503
504     def __getattr__(self, name):
505         "Unknown attribute requests get forwarded to SMProperty."
506         return getattr(self.SMProperty, name)
507
508 class GenericIterator(object):
509     """Iterator for container type objects"""
510
511     def __init__(self, obj):
512         self.Object = obj
513         self.index = 0
514
515     def __iter__(self):
516         return self
517
518     def next(self):
519         if self.index >= len(self.Object):
520             raise StopIteration
521
522         idx = self.index
523         self.index += 1
524         return self.Object[idx]
525
526 class VectorProperty(Property):
527     """A VectorProperty provides access to one or more values. You can use
528     a slice to get one or more property values:
529     > val = property[2]
530     or
531     > vals = property[0:5:2]
532     You can use a slice to set one or more property values:
533     > property[2] = val
534     or
535     > property[1:3] = (1,2)
536     """
537     def ConvertValue(self, value):
538         return value
539
540     def __len__(self):
541         """Returns the number of elements."""
542         return self.SMProperty.GetNumberOfElements()
543
544     def __iter__(self):
545         """Implementation of the sequence API"""
546         return GenericIterator(self)
547
548     def __setitem__(self, idx, value):
549         """Given a list or tuple of values, sets a slice of values [min, max)"""
550         if isinstance(idx, slice):
551             indices = idx.indices(len(self))
552             for i, j in zip(range(*indices), value):
553                 self.SMProperty.SetElement(i, self.ConvertValue(j))
554             self._UpdateProperty()
555         elif idx >= len(self) or idx < 0:
556             raise IndexError
557         else:
558             self.SMProperty.SetElement(idx, self.ConvertValue(value))
559             self._UpdateProperty()
560
561     def GetElement(self, index):
562         return self.SMProperty.GetElement(index)
563
564     def __getitem__(self, idx):
565         """Returns the range [min, max) of elements. Raises an IndexError
566         exception if an argument is out of bounds."""
567         ls = len(self)
568         if isinstance(idx, slice):
569             indices = idx.indices(ls)
570             retVal = []
571             for i in range(*indices):
572                retVal.append(self.GetElement(i))
573             return retVal
574         elif idx >= ls:
575             raise IndexError
576         elif idx < 0:
577             idx = ls + idx
578             if idx < 0:
579                 raise IndexError
580
581         return self.GetElement(idx)
582
583     def GetData(self):
584         "Returns all elements as either a list or a single value."
585         property = self.SMProperty
586         if property.GetRepeatable() or \
587            property.GetNumberOfElements() > 1:
588             return self[0:len(self)]
589         elif property.GetNumberOfElements() == 1:
590             return self.GetElement(0)
591
592     def SetData(self, values):
593         """Allows setting of all values at once. Requires a single value or
594         a iterable object."""
595         if not hasattr(values, "__iter__"):
596             values = (values,)
597         iup = self.SMProperty.GetImmediateUpdate()
598         self.SMProperty.SetImmediateUpdate(False)
599         if not self.GetRepeatable() and len(values) != self.GetNumberOfElements():
600             raise RuntimeError("This property requires %d values." % self.GetNumberOfElements())
601         if self.GetRepeatable():
602             # Clean up first
603             self.SMProperty.SetNumberOfElements(0)
604         idx = 0
605         for val in values:
606             self.SMProperty.SetElement(idx, self.ConvertValue(val))
607             idx += 1
608         self.SMProperty.SetImmediateUpdate(iup)
609         self._UpdateProperty()
610
611     def Clear(self):
612         "Removes all elements."
613         self.SMProperty().SetNumberOfElements(0)
614         self._UpdateProperty()
615
616 class ColorArrayProperty(VectorProperty):
617     """This subclass of VectorProperty handles setting of the array to
618     color by. It handles attribute type as well as well array name."""
619
620     def GetAvailable(self):
621         """"Returns the list of available arrays as (attribute type, array name
622         tuples."""
623         arrays = []
624         for a in self.Proxy.Input.PointData:
625             arrays.append(('POINT_DATA', a.GetName()))
626         for a in self.Proxy.Input.CellData:
627             arrays.append(('CELL_DATA', a.GetName()))
628         return arrays
629
630     def SetData(self, value):
631         """Overwritten to enable setting attribute type (the ColorAttributeType
632         property and the array name. The argument should be the array name
633         (in which case the first appropriate attribute type is picked) or
634         a tuple of attribute type and array name."""
635         if isinstance(value, tuple) and len(value) == 2:
636             att = value[0]
637             arr = value[1]
638         elif isinstance(value, str):
639             att = None
640             arr = value
641         else:
642             raise ValueError("Expected a tuple of 2 values or a string.")
643
644         if not arr:
645             self.SMProperty.SetElement(0, '')
646             self._UpdateProperty()
647             return
648
649         found = False
650         for a in self.Available:
651             if a[1] == arr and (not att or att == a[0]):
652                 att = a[0]
653                 found = True
654                 break
655
656         if  not found:
657             raise ValueError("Could not locate array %s in the input." % arr)
658
659         catt = self.Proxy.GetProperty("ColorAttributeType")
660         catt.SetData(att)
661         self.SMProperty.SetElement(0, arr)
662         self._UpdateProperty()
663
664     Available = property(GetAvailable, None, None, \
665         "This read-only property returns the list of arrays that can be colored by.")
666
667
668 class EnumerationProperty(VectorProperty):
669     """"Subclass of VectorProperty that is applicable for enumeration type
670     properties."""
671
672     def GetElement(self, index):
673         """Returns the text for the given element if available. Returns
674         the numerical values otherwise."""
675         val = self.SMProperty.GetElement(index)
676         domain = self.SMProperty.GetDomain("enum")
677         for i in range(domain.GetNumberOfEntries()):
678             if domain.GetEntryValue(i) == val:
679                 return domain.GetEntryText(i)
680         return val
681
682     def ConvertValue(self, value):
683         """Converts value to type suitable for vtSMProperty::SetElement()"""
684         if type(value) == str:
685             domain = self.SMProperty.GetDomain("enum")
686             if domain.HasEntryText(value):
687                 return domain.GetEntryValueForText(value)
688             else:
689                 raise ValueError("%s is not a valid value." % value)
690         return VectorProperty.ConvertValue(self, value)
691
692     def GetAvailable(self):
693         "Returns the list of available values for the property."
694         retVal = []
695         domain = self.SMProperty.GetDomain("enum")
696         for i in range(domain.GetNumberOfEntries()):
697             retVal.append(domain.GetEntryText(i))
698         return retVal
699
700     Available = property(GetAvailable, None, None, \
701         "This read-only property contains the list of values that can be applied to this property.")
702
703
704 class FileNameProperty(VectorProperty):
705     """Property to set/get one or more file names.
706     This property updates the pipeline information everytime its value changes.
707     This is used to keep the array lists up to date."""
708
709     def _UpdateProperty(self):
710         "Pushes the value of this property to the server."
711         VectorProperty._UpdateProperty(self)
712         self.Proxy.FileNameChanged()
713
714 class ArraySelectionProperty(VectorProperty):
715     "Property to select an array to be processed by a filter."
716
717     def GetAssociation(self):
718         val = self.GetElement(3)
719         if val == "":
720             return None
721         for key, value in ASSOCIATIONS.iteritems():
722             if value == int(val):
723                 return key
724
725         return None
726
727     def GetArrayName(self):
728         return self.GetElement(4)
729
730     def __len__(self):
731         """Returns the number of elements."""
732         return 2
733
734     def __setitem__(self, idx, value):
735         raise RuntimeError, "This property cannot be accessed using __setitem__"
736
737     def __getitem__(self, idx):
738         """Returns attribute type for index 0, array name for index 1"""
739         if isinstance(idx, slice):
740             indices = idx.indices(len(self))
741             retVal = []
742             for i in range(*indices):
743                 if i >= 2 or i < 0:
744                     raise IndexError
745                 if i == 0:
746                     retVal.append(self.GetAssociation())
747                 else:
748                     retVal.append(self.GetArrayName())
749             return retVal
750         elif idx >= 2 or idx < 0:
751             raise IndexError
752
753         if i == 0:
754             return self.GetAssociation()
755         else:
756             return self.GetArrayName()
757
758     def SetData(self, values):
759         """Allows setting of all values at once. Requires a single value,
760         a tuple or list."""
761         if not isinstance(values, tuple) and \
762            not isinstance(values, list):
763             values = (values,)
764         if len(values) == 1:
765             self.SMProperty.SetElement(4, values[0])
766         elif len(values) == 2:
767             if isinstance(values[0], str):
768                 val = str(ASSOCIATIONS[values[0]])
769             self.SMProperty.SetElement(3,  str(val))
770             self.SMProperty.SetElement(4, values[1])
771         else:
772             raise RuntimeError, "Expected 1 or 2 values."
773         self._UpdateProperty()
774
775     def UpdateDefault(self):
776         "Helper method to set default values."
777         if self.SMProperty.GetNumberOfElements() != 5:
778             return
779         if self.GetElement(4) != '' or \
780             self.GetElement(3) != '':
781             return
782
783         for i in range(0,3):
784             if self.GetElement(i) == '':
785                 self.SMProperty.SetElement(i, '0')
786         al = self.SMProperty.GetDomain("array_list")
787         al.Update(self.SMProperty)
788         al.SetDefaultValues(self.SMProperty)
789
790 class ArrayListProperty(VectorProperty):
791     """This property provides a simpler interface for selecting arrays.
792     Simply assign a list of arrays that should be loaded by the reader.
793     Use the Available property to get a list of available arrays."""
794
795     def __init__(self, proxy, smproperty):
796         VectorProperty.__init__(self, proxy, smproperty)
797         self.__arrays = []
798
799     def GetAvailable(self):
800         "Returns the list of available arrays"
801         dm = self.GetDomain("array_list")
802         retVal = []
803         for i in range(dm.GetNumberOfStrings()):
804             retVal.append(dm.GetString(i))
805         return retVal
806
807     Available = property(GetAvailable, None, None, \
808         "This read-only property contains the list of items that can be read by a reader.")
809
810     def SelectAll(self):
811         "Selects all arrays."
812         self.SetData(self.Available)
813
814     def DeselectAll(self):
815         "Deselects all arrays."
816         self.SetData([])
817
818     def __iter__(self):
819         """Implementation of the sequence API"""
820         return GenericIterator(self)
821
822     def __len__(self):
823         """Returns the number of elements."""
824         return len(self.GetData())
825
826     def __setitem__(self, idx, value):
827       """Given a list or tuple of values, sets a slice of values [min, max)"""
828       self.GetData()
829       if isinstance(idx, slice):
830           indices = idx.indices(len(self))
831           for i, j in zip(range(*indices), value):
832               self.__arrays[i] = j
833           self.SetData(self.__arrays)
834       elif idx >= len(self) or idx < 0:
835           raise IndexError
836       else:
837           self.__arrays[idx] = self.ConvertValue(value)
838           self.SetData(self.__arrays)
839
840     def __getitem__(self, idx):
841       """Returns the range [min, max) of elements. Raises an IndexError
842       exception if an argument is out of bounds."""
843       self.GetData()
844       if isinstance(idx, slice):
845           indices = idx.indices(len(self))
846           retVal = []
847           for i in range(*indices):
848               retVal.append(self.__arrays[i])
849           return retVal
850       elif idx >= len(self) or idx < 0:
851           raise IndexError
852       return self.__arrays[idx]
853
854     def SetData(self, values):
855         """Allows setting of all values at once. Requires a single value,
856         a tuple or list."""
857         # Clean up first
858         iup = self.SMProperty.GetImmediateUpdate()
859         self.SMProperty.SetImmediateUpdate(False)
860         # Clean up first
861         self.SMProperty.SetNumberOfElements(0)
862         if not isinstance(values, tuple) and \
863            not isinstance(values, list):
864             values = (values,)
865         fullvalues = []
866         for i in range(len(values)):
867             val = self.ConvertValue(values[i])
868             fullvalues.append(val)
869             fullvalues.append('1')
870         for array in self.Available:
871             if not values.__contains__(array):
872                 fullvalues.append(array)
873                 fullvalues.append('0')
874         i = 0
875         for value in fullvalues:
876             self.SMProperty.SetElement(i, value)
877             i += 1
878
879         self._UpdateProperty()
880         self.SMProperty.SetImmediateUpdate(iup)
881
882     def GetData(self):
883         "Returns all elements as a list."
884         property = self.SMProperty
885         nElems = property.GetNumberOfElements()
886         if nElems%2 != 0:
887             raise ValueError, "The SMProperty with XML label '%s' has a size that is not a multiple of 2." % property.GetXMLLabel()
888         self.__arrays = []
889         for i in range(0, nElems, 2):
890             if self.GetElement(i+1) != '0':
891                 self.__arrays.append(self.GetElement(i))
892         return list(self.__arrays)
893
894 class ProxyProperty(Property):
895     """A ProxyProperty provides access to one or more proxies. You can use
896     a slice to get one or more property values:
897     > proxy = property[2]
898     or
899     > proxies = property[0:5:2]
900     You can use a slice to set one or more property values:
901     > property[2] = proxy
902     or
903     > property[1:3] = (proxy1, proxy2)
904     You can also append and delete:
905     > property.append(proxy)
906     and
907     > del property[1:2]
908
909     You can also remove all elements with Clear().
910
911     Note that some properties expect only 1 proxy and will complain if
912     you set the number of values to be something else.
913     """
914     def __init__(self, proxy, smproperty):
915         """Default constructor.  Stores a reference to the proxy.  Also looks
916         at domains to find valid values."""
917         Property.__init__(self, proxy, smproperty)
918         # Check to see if there is a proxy list domain and, if so,
919         # initialize ourself. (Should this go in ProxyProperty?)
920         listdomain = self.GetDomain('proxy_list')
921         if listdomain:
922             if listdomain.GetClassName() != 'vtkSMProxyListDomain':
923                 raise ValueError, "Found a 'proxy_list' domain on an InputProperty that is not a ProxyListDomain."
924             pm = ProxyManager()
925             group = "pq_helper_proxies." + proxy.GetSelfIDAsString()
926             if listdomain.GetNumberOfProxies() == 0:
927                 for i in xrange(listdomain.GetNumberOfProxyTypes()):
928                     igroup = listdomain.GetProxyGroup(i)
929                     name = listdomain.GetProxyName(i)
930                     iproxy = CreateProxy(igroup, name)
931                     listdomain.AddProxy(iproxy)
932                     pm.RegisterProxy(group, proxy.GetPropertyName(smproperty), iproxy)
933                 listdomain.SetDefaultValues(self.SMProperty)
934
935     def GetAvailable(self):
936         """If this proxy has a list domain, then this function returns the
937         strings you can use to select from the domain.  If there is no such
938         list domain, the returned list is empty."""
939         listdomain = self.GetDomain('proxy_list')
940         retval = []
941         if listdomain:
942             for i in xrange(listdomain.GetNumberOfProxies()):
943                 proxy = listdomain.GetProxy(i)
944                 retval.append(proxy.GetXMLLabel())
945         return retval
946
947     Available = property(GetAvailable, None, None,
948                          """This read only property is a list of strings you can
949                          use to select from the list domain.  If there is no
950                          such list domain, the array is empty.""")
951
952     def __iter__(self):
953         """Implementation of the sequence API"""
954         return GenericIterator(self)
955
956     def __len__(self):
957         """Returns the number of elements."""
958         return self.SMProperty.GetNumberOfProxies()
959
960     def remove(self, proxy):
961         """Removes the first occurence of the proxy from the property."""
962         self.SMProperty.RemoveProxy(proxy.SMProxy)
963         self._UpdateProperty()
964         
965     def __setitem__(self, idx, value):
966       """Given a list or tuple of values, sets a slice of values [min, max)"""
967       if isinstance(idx, slice):
968         indices = idx.indices(len(self))
969         for i, j in zip(range(*indices), value):
970           self.SMProperty.SetProxy(i, j.SMProxy)
971         self._UpdateProperty()
972       elif idx >= len(self) or idx < 0:
973         raise IndexError
974       else:
975         self.SMProperty.SetProxy(idx, value.SMProxy)
976         self._UpdateProperty()
977
978     def __delitem__(self,idx):
979       """Removes the element idx"""
980       if isinstance(idx, slice):
981         indices = idx.indices(len(self))
982         # Collect the elements to delete to a new list first.
983         # Otherwise indices are screwed up during the actual
984         # remove loop.
985         toremove = []
986         for i in range(*indices):
987           toremove.append(self[i])
988         for i in toremove:
989           self.SMProperty.RemoveProxy(i.SMProxy)
990         self._UpdateProperty()
991       elif idx >= len(self) or idx < 0:
992         raise IndexError
993       else:
994         self.SMProperty.RemoveProxy(self[idx].SMProxy)
995         self._UpdateProperty()
996
997     def __getitem__(self, idx):
998       """Returns the range [min, max) of elements. Raises an IndexError
999       exception if an argument is out of bounds."""
1000       if isinstance(idx, slice):
1001         indices = idx.indices(len(self))
1002         retVal = []
1003         for i in range(*indices):
1004           retVal.append(_getPyProxy(self.SMProperty.GetProxy(i)))
1005         return retVal
1006       elif idx >= len(self) or idx < 0:
1007         raise IndexError
1008       return _getPyProxy(self.SMProperty.GetProxy(idx))
1009
1010     def __getattr__(self, name):
1011         "Unknown attribute requests get forwarded to SMProperty."
1012         return getattr(self.SMProperty, name)
1013
1014     def index(self, proxy):
1015         idx = 0
1016         for px in self:
1017             ## VSV: ==
1018             if proxy.IsSame(px):
1019                 return idx
1020             idx += 1
1021         raise ValueError("proxy is not in the list.")
1022
1023     def append(self, proxy):
1024         "Appends the given proxy to the property values."
1025         self.SMProperty.AddProxy(proxy.SMProxy)
1026         self._UpdateProperty()
1027
1028     def GetData(self):
1029         "Returns all elements as either a list or a single value."
1030         property = self.SMProperty
1031         if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
1032             return self[0:len(self)]
1033         else:
1034             if property.GetNumberOfProxies() > 0:
1035                 return _getPyProxy(property.GetProxy(0))
1036         return None
1037
1038     def SetData(self, values):
1039         """Allows setting of all values at once. Requires a single value,
1040         a tuple or list."""
1041         if isinstance(values, str):
1042             position = -1
1043             try:
1044                 position = self.Available.index(values)
1045             except:
1046                 raise ValueError, values + " is not a valid object in the domain."
1047             values = self.GetDomain('proxy_list').GetProxy(position)
1048         if not isinstance(values, tuple) and \
1049            not isinstance(values, list):
1050             values = (values,)
1051         self.SMProperty.RemoveAllProxies()
1052         for value in values:
1053             if isinstance(value, Proxy):
1054                 value_proxy = value.SMProxy
1055             else:
1056                 value_proxy = value
1057             self.SMProperty.AddProxy(value_proxy)
1058         self._UpdateProperty()
1059
1060     def Clear(self):
1061         "Removes all elements."
1062         self.SMProperty.RemoveAllProxies()
1063         self._UpdateProperty()
1064
1065 class InputProperty(ProxyProperty):
1066     """An InputProperty allows making pipeline connections. You can set either
1067     a source proxy or an OutputProperty to an input property:
1068
1069     > property[0] = proxy
1070     or
1071     > property[0] = OuputPort(proxy, 1)
1072
1073     > property.append(proxy)
1074     or
1075     > property.append(OutputPort(proxy, 0))
1076     """
1077     def __setitem__(self, idx, value):
1078       """Given a list or tuple of values, sets a slice of values [min, max)"""
1079       if isinstance(idx, slice):
1080         indices = idx.indices(len(self))
1081         for i, j in zip(range(*indices), value):
1082           op = value[i-min]
1083           self.SMProperty.SetInputConnection(i, op.SMProxy, op.Port)
1084         self._UpdateProperty()
1085       elif idx >= len(self) or idx < 0:
1086         raise IndexError
1087       else:
1088         self.SMProperty.SetInputConnection(idx, value.SMProxy, value.Port)
1089         self._UpdateProperty()
1090
1091     def __getitem__(self, idx):
1092       """Returns the range [min, max) of elements. Raises an IndexError
1093       exception if an argument is out of bounds."""
1094       if isinstance(idx, slice):
1095         indices = idx.indices(len(self))
1096         retVal = []
1097         for i in range(*indices):
1098             port = None
1099             if self.SMProperty.GetProxy(i):
1100                 port = OutputPort(_getPyProxy(self.SMProperty.GetProxy(i)),\
1101                                   self.SMProperty.GetOutputPortForConnection(i))
1102             retVal.append(port)
1103         return retVal
1104       elif idx >= len(self) or idx < 0:
1105         raise IndexError
1106       return OutputPort(_getPyProxy(self.SMProperty.GetProxy(idx)),\
1107                         self.SMProperty.GetOutputPortForConnection(idx))
1108
1109     def append(self, value):
1110         """Appends the given proxy to the property values.
1111         Accepts Proxy or OutputPort objects."""
1112         self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
1113         self._UpdateProperty()
1114
1115     def GetData(self):
1116         """Returns all elements as either a list of OutputPort objects or
1117         a single OutputPort object."""
1118         property = self.SMProperty
1119         if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
1120             return self[0:len(self)]
1121         else:
1122             if property.GetNumberOfProxies() > 0:
1123                 return OutputPort(_getPyProxy(property.GetProxy(0)),\
1124                                   self.SMProperty.GetOutputPortForConnection(0))
1125         return None
1126
1127     def SetData(self, values):
1128         """Allows setting of all values at once. Requires a single value,
1129         a tuple or list. Accepts Proxy or OutputPort objects."""
1130         if isinstance(values, str):
1131             ProxyProperty.SetData(self, values)
1132             return
1133         if not isinstance(values, tuple) and \
1134            not isinstance(values, list):
1135             values = (values,)
1136         self.SMProperty.RemoveAllProxies()
1137         for value in values:
1138             if value:
1139                 self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
1140         self._UpdateProperty()
1141
1142     def _UpdateProperty(self):
1143         "Pushes the value of this property to the server."
1144         ProxyProperty._UpdateProperty(self)
1145         iter = PropertyIterator(self.Proxy)
1146         for prop in iter:
1147             if isinstance(prop, ArraySelectionProperty):
1148                 prop.UpdateDefault()
1149
1150
1151 class DataInformation(object):
1152     """DataInformation is a contained for meta-data associated with an
1153     output data.
1154
1155     DataInformation is a python wrapper around a vtkPVDataInformation.
1156     In addition to proving all methods of a vtkPVDataInformation, it provides
1157     a few convenience methods.
1158
1159     Please note that some of the methods accessible through the DataInformation
1160     class are not listed by help() because the DataInformation objects forward
1161     unresolved attributes to the underlying object. To get the full list,
1162     see also dir(proxy.DataInformation).
1163     See also the doxygen based documentation of the vtkPVDataInformation C++
1164     class.
1165     """
1166     def __init__(self, dataInformation, proxy, idx):
1167         """Default constructor. Requires a vtkPVDataInformation, a source proxy
1168         and an output port id."""
1169         self.DataInformation = dataInformation
1170         self.Proxy = proxy
1171         self.Idx = idx
1172
1173     def Update(self):
1174         """****Deprecated**** There is no reason anymore to use this method
1175         explicitly, it is called automatically when one gets any value from the
1176         data information object.
1177         Update the data information if necessary. Note that this
1178         does not cause execution of the underlying object. In certain
1179         cases, you may have to call UpdatePipeline() on the proxy."""
1180         if self.Proxy:
1181             self.Proxy.GetDataInformation(self.Idx)
1182
1183     def GetDataSetType(self):
1184         """Returns the dataset type as defined in vtkDataObjectTypes."""
1185         self.Update()
1186         if not self.DataInformation:
1187             raise RuntimeError, "No data information is available"
1188         if self.DataInformation.GetCompositeDataSetType() > -1:
1189             return self.DataInformation.GetCompositeDataSetType()
1190         return self.DataInformation.GetDataSetType()
1191
1192     def GetDataSetTypeAsString(self):
1193         """Returns the dataset type as a user-friendly string. This is
1194         not the same as the enumaration used by VTK"""
1195         return vtkDataObjectTypes.GetClassNameFromTypeId(self.GetDataSetType())
1196
1197     def __getattr__(self, name):
1198         """Forwards unknown attribute requests to the underlying
1199         vtkPVInformation."""
1200         if not self.DataInformation:
1201             raise AttributeError("class has no attribute %s" % name)
1202             return None
1203         self.Update()
1204         return getattr(self.DataInformation, name)
1205
1206 class ArrayInformation(object):
1207     """Meta-information associated with an array. Use the Name
1208     attribute to get the array name.
1209
1210     Please note that some of the methods accessible through the ArrayInformation
1211     class are not listed by help() because the ArrayInformation objects forward
1212     unresolved attributes to the underlying object.
1213     See the doxygen based documentation of the vtkPVArrayInformation C++
1214     class for a full list.
1215     """
1216     def __init__(self, proxy, field, name):
1217         self.Proxy = proxy
1218         self.FieldData = field
1219         self.Name = name
1220
1221     def __getattr__(self, name):
1222         """Forward unknown methods to vtkPVArrayInformation"""
1223         array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
1224         if not array: return None
1225         return getattr(array, name)
1226
1227     def __repr__(self):
1228         """Returns a user-friendly representation string."""
1229         return "Array: " + self.Name
1230
1231     def GetRange(self, component=0):
1232         """Given a component, returns its value range as a tuple of 2 values."""
1233         array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
1234         range = array.GetComponentRange(component)
1235         return (range[0], range[1])
1236
1237
1238 class FieldDataInformationIterator(object):
1239     """Iterator for FieldDataInformation"""
1240
1241     def __init__(self, info, items=False):
1242         self.FieldDataInformation = info
1243         self.index = 0
1244         self.items = items
1245
1246     def __iter__(self):
1247         return self
1248
1249     def next(self):
1250         if self.index >= self.FieldDataInformation.GetNumberOfArrays():
1251             raise StopIteration
1252
1253         self.index += 1
1254         ai = self.FieldDataInformation[self.index-1]
1255         if self.items:
1256             return (ai.GetName(), ai)
1257         else:
1258             return ai
1259
1260
1261 class FieldDataInformation(object):
1262     """Meta-data for a field of an output object (point data, cell data etc...).
1263     Provides easy access to the arrays using the slice interface:
1264     > narrays = len(field_info)
1265     > for i in range(narrays):
1266     >   array_info = field_info[i]
1267
1268     Full slice interface is supported:
1269     > arrays = field_info[0:5:3]
1270     where arrays is a list.
1271
1272     Array access by name is also possible:
1273     > array_info = field_info['Temperature']
1274
1275     The number of arrays can also be accessed using the NumberOfArrays
1276     property.
1277     """
1278     def __init__(self, proxy, idx, field):
1279         self.Proxy = proxy
1280         self.OutputPort = idx
1281         self.FieldData = field
1282
1283     def GetFieldData(self):
1284         """Convenience method to get the underlying
1285         vtkPVDataSetAttributesInformation"""
1286         return getattr(self.Proxy.GetDataInformation(self.OutputPort), "Get%sInformation" % self.FieldData)()
1287
1288     def GetNumberOfArrays(self):
1289         """Returns the number of arrays."""
1290         self.Proxy.UpdatePipeline()
1291         return self.GetFieldData().GetNumberOfArrays()
1292
1293     def GetArray(self, idx):
1294         """Given an index or a string, returns an array information.
1295         Raises IndexError if the index is out of bounds."""
1296         self.Proxy.UpdatePipeline()
1297         if not self.GetFieldData().GetArrayInformation(idx):
1298             return None
1299         if isinstance(idx, str):
1300             return ArrayInformation(self.Proxy, self, idx)
1301         elif idx >= len(self) or idx < 0:
1302             raise IndexError
1303         return ArrayInformation(self.Proxy, self, self.GetFieldData().GetArrayInformation(idx).GetName())
1304
1305     def __len__(self):
1306         """Returns the number of arrays."""
1307         return self.GetNumberOfArrays()
1308
1309     def __getitem__(self, idx):
1310         """Implements the [] operator. Accepts an array name."""
1311         if isinstance(idx, slice):
1312             indices = idx.indices(self.GetNumberOfArrays())
1313             retVal = []
1314             for i in range(*indices):
1315                 retVal.append(self.GetArray(i))
1316             return retVal
1317         return self.GetArray(idx)
1318
1319     def keys(self):
1320         """Implementation of the dictionary API"""
1321         kys = []
1322         narrays = self.GetNumberOfArrays()
1323         for i in range(narrays):
1324             kys.append(self.GetArray(i).GetName())
1325         return kys
1326
1327     def values(self):
1328         """Implementation of the dictionary API"""
1329         vals = []
1330         narrays = self.GetNumberOfArrays()
1331         for i in range(narrays):
1332             vals.append(self.GetArray(i))
1333         return vals
1334
1335     def iteritems(self):
1336         """Implementation of the dictionary API"""
1337         return FieldDataInformationIterator(self, True)
1338
1339     def items(self):
1340         """Implementation of the dictionary API"""
1341         itms = []
1342         narrays = self.GetNumberOfArrays()
1343         for i in range(narrays):
1344             ai = self.GetArray(i)
1345             itms.append((ai.GetName(), ai))
1346         return itms
1347
1348     def has_key(self, key):
1349         """Implementation of the dictionary API"""
1350         if self.GetArray(key):
1351             return True
1352         return False
1353
1354     def __iter__(self):
1355         """Implementation of the dictionary API"""
1356         return FieldDataInformationIterator(self)
1357
1358     def __getattr__(self, name):
1359         """Forwards unknown attributes to the underlying
1360         vtkPVDataSetAttributesInformation"""
1361         array = self.GetArray(name)
1362         if array: return array
1363         raise AttributeError("class has no attribute %s" % name)
1364         return None
1365
1366     NumberOfArrays = property(GetNumberOfArrays, None, None, "Returns the number of arrays.")
1367
1368 def OutputPort(proxy, outputPort=0):
1369     if not Proxy:
1370         return None
1371     if isinstance(outputPort, str):
1372         outputPort = proxy.GetOutputPortIndex(outputPort)
1373     if outputPort >= proxy.GetNumberOfOutputPorts():
1374         return None
1375     if proxy.Port == outputPort:
1376         return proxy
1377     newinstance = _getPyProxy(proxy.SMProxy, outputPort)
1378     newinstance.Port = outputPort
1379     newinstance._Proxy__Properties = proxy._Proxy__Properties
1380     return newinstance
1381
1382 class ProxyManager(object):
1383     """When running scripts from the python shell in the ParaView application,
1384     registering proxies with the proxy manager is the ony mechanism to
1385     notify the graphical user interface (GUI) that a proxy
1386     exists. Therefore, unless a proxy is registered, it will not show up in
1387     the user interface. Also, the proxy manager is the only way to get
1388     access to proxies created using the GUI. Proxies created using the GUI
1389     are automatically registered under an appropriate group (sources,
1390     filters, representations and views). To get access to these objects,
1391     you can use proxyManager.GetProxy(group, name). The name is the same
1392     as the name shown in the pipeline browser.
1393
1394     This class is a python wrapper for vtkSMProxyManager. Note that the
1395     underlying vtkSMProxyManager is a singleton. All instances of this
1396     class wil refer to the same object. In addition to all methods provided by
1397     vtkSMProxyManager (all unknown attribute requests are forwarded
1398     to the vtkSMProxyManager), this class provides several convenience
1399     methods.
1400
1401     Please note that some of the methods accessible through the ProxyManager
1402     class are not listed by help() because the ProxyManager objects forwards
1403     unresolved attributes to the underlying object. To get the full list,
1404     see also dir(proxy.SMProxyManager). See also the doxygen based documentation
1405     of the vtkSMProxyManager C++ class.
1406     """
1407
1408     def __init__(self):
1409         """Constructor. Assigned self.SMProxyManager to
1410         vtkSMObject.GetPropertyManager()."""
1411         self.SMProxyManager = vtkSMObject.GetProxyManager()
1412
1413     def RegisterProxy(self, group, name, aProxy):
1414         """Registers a proxy (either SMProxy or proxy) with the
1415         server manager"""
1416         if isinstance(aProxy, Proxy):
1417             self.SMProxyManager.RegisterProxy(group, name, aProxy.SMProxy)
1418         else:
1419             self.SMProxyManager.RegisterProxy(group, name, aProxy)
1420
1421     def NewProxy(self, group, name):
1422         """Creates a new proxy of given group and name and returns an SMProxy.
1423         Note that this is a server manager object. You should normally create
1424         proxies using the class objects. For example:
1425         obj = servermanager.sources.SphereSource()"""
1426         if not self.SMProxyManager:
1427             return None
1428         aProxy = self.SMProxyManager.NewProxy(group, name)
1429         if not aProxy:
1430             return None
1431         aProxy.UnRegister(None)
1432         return aProxy
1433
1434     def GetProxy(self, group, name):
1435         """Returns a Proxy registered under a group and name"""
1436         if not self.SMProxyManager:
1437             return None
1438         aProxy = self.SMProxyManager.GetProxy(group, name)
1439         if not aProxy:
1440             return None
1441         return _getPyProxy(aProxy)
1442
1443     def GetPrototypeProxy(self, group, name):
1444         """Returns a prototype proxy given a group and name. This is an
1445         SMProxy. This is a low-level method. You should not normally
1446         have to call it."""
1447         if not self.SMProxyManager:
1448             return None
1449         aProxy = self.SMProxyManager.GetPrototypeProxy(group, name)
1450         if not aProxy:
1451             return None
1452         return aProxy
1453
1454     def GetProxiesOnConnection(self, connection):
1455         """Returns a map of proxies registered with the proxy manager
1456            on the particular connection."""
1457         proxy_groups = {}
1458         iter = self.NewConnectionIterator(connection)
1459         for proxy in iter:
1460             if not proxy_groups.has_key(iter.GetGroup()):
1461                 proxy_groups[iter.GetGroup()] = {}
1462             group = proxy_groups[iter.GetGroup()]
1463             group[(iter.GetKey(), proxy.GetSelfIDAsString())] = proxy
1464         return proxy_groups
1465
1466     def GetProxiesInGroup(self, groupname, connection=None):
1467         """Returns a map of proxies in a particular group.
1468          If connection is not None, then only those proxies
1469          in the group that are on the particular connection
1470          are returned.
1471         """
1472         proxies = {}
1473         iter = self.NewGroupIterator(groupname)
1474         for aProxy in iter:
1475             proxies[(iter.GetKey(), aProxy.GetSelfIDAsString())] = aProxy
1476         return proxies
1477
1478     def UnRegisterProxy(self, groupname, proxyname, aProxy):
1479         """Unregisters a proxy."""
1480         if not self.SMProxyManager:
1481             return
1482         if aProxy != None and isinstance(aProxy,Proxy):
1483             aProxy = aProxy.SMProxy
1484         if aProxy:
1485             self.SMProxyManager.UnRegisterProxy(groupname, proxyname, aProxy)
1486
1487     def GetProxies(self, groupname, proxyname):
1488         """Returns all proxies registered under the given group with the
1489         given name. Note that it is possible to register more than one
1490         proxy with the same name in the same group. Because the proxies
1491         are different, there is no conflict. Use this method instead of
1492         GetProxy() if you know that there are more than one proxy registered
1493         with this name."""
1494         if not self.SMProxyManager:
1495             return []
1496         collection = vtkCollection()
1497         result = []
1498         self.SMProxyManager.GetProxies(groupname, proxyname, collection)
1499         for i in range(0, collection.GetNumberOfItems()):
1500             aProxy = _getPyProxy(collection.GetItemAsObject(i))
1501             if aProxy:
1502                 result.append(aProxy)
1503
1504         return result
1505
1506     def __iter__(self):
1507         """Returns a new ProxyIterator."""
1508         iter = ProxyIterator()
1509         if ActiveConnection:
1510             iter.SetConnectionID(ActiveConnection.ID)
1511         iter.Begin()
1512         return iter
1513
1514     def NewGroupIterator(self, group_name, connection=None):
1515         """Returns a ProxyIterator for a group. The resulting object
1516         can be used to traverse the proxies that are in the given
1517         group."""
1518         iter = self.__iter__()
1519         if not connection:
1520             connection = ActiveConnection
1521         if connection:
1522             iter.SetConnectionID(connection.ID)
1523         iter.SetModeToOneGroup()
1524         iter.Begin(group_name)
1525         return iter
1526
1527     def NewConnectionIterator(self, connection=None):
1528         """Returns a ProxyIterator for a given connection. This can be
1529         used to travers ALL proxies managed by the proxy manager."""
1530         iter = self.__iter__()
1531         if not connection:
1532             connection = ActiveConnection
1533         if connection:
1534             iter.SetConnectionID(connection.ID)
1535         iter.Begin()
1536         return iter
1537
1538     def NewDefinitionIterator(self, groupname=None):
1539         """Returns an iterator that can be used to iterate over
1540            all groups and types of proxies that the proxy manager
1541            can create."""
1542         iter = ProxyDefinitionIterator()
1543         if groupname != None:
1544             iter.SetModeToOneGroup()
1545             iter.Begin(groupname)
1546         return iter
1547
1548     def __ConvertArgumentsAndCall(self, *args):
1549       newArgs = []
1550       for arg in args:
1551           if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
1552               newArgs.append(arg.SMProxy)
1553           else:
1554               newArgs.append(arg)
1555       func = getattr(self.SMProxyManager, self.__LastAttrName)
1556       retVal = func(*newArgs)
1557       if type(retVal) is type(self.SMProxyManager) and retVal.IsA("vtkSMProxy"):
1558           return _getPyProxy(retVal)
1559       else:
1560           return retVal
1561
1562     def __getattr__(self, name):
1563         """Returns attribute from the ProxyManager"""
1564         try:
1565             pmAttr = getattr(self.SMProxyManager, name)
1566             self.__LastAttrName = name
1567             return self.__ConvertArgumentsAndCall
1568         except:
1569             pass
1570         return getattr(self.SMProxyManager, name)
1571
1572
1573 class PropertyIterator(object):
1574     """Wrapper for a vtkSMPropertyIterator class to satisfy
1575        the python iterator protocol. Note that the list of
1576        properties can also be obtained from the class object's
1577        dictionary.
1578        See the doxygen documentation for vtkSMPropertyIterator C++
1579        class for details.
1580        """
1581
1582     def __init__(self, aProxy):
1583         self.SMIterator = aProxy.NewPropertyIterator()
1584         if self.SMIterator:
1585             self.SMIterator.UnRegister(None)
1586             self.SMIterator.Begin()
1587         self.Key = None
1588         self.PropertyLabel = None
1589         self.Proxy = aProxy
1590
1591     def __iter__(self):
1592         return self
1593
1594     def next(self):
1595         if not self.SMIterator:
1596             raise StopIteration
1597
1598         if self.SMIterator.IsAtEnd():
1599             self.Key = None
1600             raise StopIteration
1601         self.Key = self.SMIterator.GetKey()
1602         self.PropertyLabel = self.SMIterator.GetPropertyLabel()
1603         self.SMIterator.Next()
1604         return self.Proxy.GetProperty(self.Key)
1605
1606     def GetProxy(self):
1607         """Returns the proxy for the property last returned by the call to
1608         'next()'"""
1609         return self.Proxy
1610
1611     def GetKey(self):
1612         """Returns the key for the property last returned by the call to
1613         'next()' """
1614         return self.Key
1615
1616     def GetProperty(self):
1617         """Returns the property last returned by the call to 'next()' """
1618         return self.Proxy.GetProperty(self.Key)
1619
1620     def __getattr__(self, name):
1621         """returns attributes from the vtkSMProxyIterator."""
1622         return getattr(self.SMIterator, name)
1623
1624 class ProxyDefinitionIterator(object):
1625     """Wrapper for a vtkSMProxyDefinitionIterator class to satisfy
1626        the python iterator protocol.
1627        See the doxygen documentation of the vtkSMProxyDefinitionIterator
1628        C++ class for more information."""
1629     def __init__(self):
1630         self.SMIterator = vtkSMProxyDefinitionIterator()
1631         self.Group = None
1632         self.Key = None
1633
1634     def __iter__(self):
1635         return self
1636
1637     def next(self):
1638         if self.SMIterator.IsAtEnd():
1639             self.Group = None
1640             self.Key = None
1641             raise StopIteration
1642         self.Group = self.SMIterator.GetGroup()
1643         self.Key = self.SMIterator.GetKey()
1644         self.SMIterator.Next()
1645         return {"group": self.Group, "key":self.Key }
1646
1647     def GetKey(self):
1648         """Returns the key for the proxy definition last returned by the call
1649         to 'next()' """
1650         return self.Key
1651
1652     def GetGroup(self):
1653         """Returns the group for the proxy definition last returned by the
1654         call to 'next()' """
1655         return self.Group
1656
1657     def __getattr__(self, name):
1658         """returns attributes from the vtkSMProxyDefinitionIterator."""
1659         return getattr(self.SMIterator, name)
1660
1661
1662 class ProxyIterator(object):
1663     """Wrapper for a vtkSMProxyIterator class to satisfy the
1664      python iterator protocol.
1665      See the doxygen documentation of vtkSMProxyIterator C++ class for
1666      more information.
1667      """
1668     def __init__(self):
1669         self.SMIterator = vtkSMProxyIterator()
1670         self.SMIterator.Begin()
1671         self.AProxy = None
1672         self.Group = None
1673         self.Key = None
1674
1675     def __iter__(self):
1676         return self
1677
1678     def next(self):
1679         if self.SMIterator.IsAtEnd():
1680             self.AProxy = None
1681             self.Group = None
1682             self.Key = None
1683             raise StopIteration
1684             return None
1685         self.AProxy = _getPyProxy(self.SMIterator.GetProxy())
1686         self.Group = self.SMIterator.GetGroup()
1687         self.Key = self.SMIterator.GetKey()
1688         self.SMIterator.Next()
1689         return self.AProxy
1690
1691     def GetProxy(self):
1692         """Returns the proxy last returned by the call to 'next()'"""
1693         return self.AProxy
1694
1695     def GetKey(self):
1696         """Returns the key for the proxy last returned by the call to
1697         'next()' """
1698         return self.Key
1699
1700     def GetGroup(self):
1701         """Returns the group for the proxy last returned by the call to
1702         'next()' """
1703         return self.Group
1704
1705     def __getattr__(self, name):
1706         """returns attributes from the vtkSMProxyIterator."""
1707         return getattr(self.SMIterator, name)
1708
1709 class Connection(object):
1710     """
1711       This is a python representation for a connection.
1712     """
1713     def __init__(self, connectionId):
1714         """Default constructor. Creates a Connection with the given
1715         ID, all other data members initialized to None."""
1716         self.ID = connectionId
1717         self.Hostname = None
1718         self.Port = None
1719         self.RSHostname = None
1720         self.RSPort = None
1721         self.Reverse = False
1722         return
1723
1724     def __eq__(self, other):
1725         "Returns true if the connection ids are the same."
1726         return self.ID == other.ID
1727
1728     def SetHost(self, ds_host=None, ds_port=None, rs_host=None, rs_port=None,
1729       reverse=False):
1730         """
1731           Set the hostname of a given connection. Used by Connect().
1732           If all args are None, it's assumed to be a built-in connection i.e.
1733           connection scheme = builtin.
1734         """
1735         self.Hostname = ds_host
1736         self.Port = ds_port
1737         self.RSHostname = rs_host
1738         self.RSPort = rs_port
1739         self.Reversed = reverse
1740         return
1741
1742     def __repr__(self):
1743         """User friendly string representation"""
1744         if not self.Hostname:
1745            return "Connection (builtin[%d]:)" % self.ID
1746         if not self.RSHostname:
1747             return "Connection (%s:%d)" % (self.Hostname, self.Port)
1748         return "Connection data(%s:%d), render(%s:%d)" % \
1749             (self.Hostname, self.Port, self.RSHostname, self.RSPort)
1750
1751     def GetURI(self):
1752         """Get URI of the connection"""
1753         if not self.Hostname or self.Hostname == "builtin":
1754             return "builtin:"
1755         if self.Reversed:
1756             if not self.RSHostname:
1757                 return "csrc://%s:%d" % (self.Hostname, self.Port)
1758             return "cdsrsrc://%s:%d//%s:%d" % (self.Hostname, self.Port,
1759               self.RSHostname, self.RSPort)
1760         if not self.RSHostname:
1761             return "cs://%s:%d" % (self.Hostname, self.Port)
1762         return "cdsrs://%s:%d//%s:%d" % (self.Hostname, self.Port,
1763           self.RSHostname, self.RSPort)
1764
1765     def IsRemote(self):
1766         """Returns True if the connection to a remote server, False if
1767         it is local (built-in)"""
1768         pm = vtkProcessModule.GetProcessModule()
1769         if pm.IsRemote(self.ID):
1770             return True
1771         return False
1772
1773     def GetNumberOfDataPartitions(self):
1774         """Returns the number of partitions on the data server for this
1775            connection"""
1776         pm = vtkProcessModule.GetProcessModule()
1777         return pm.GetNumberOfPartitions(self.ID)
1778
1779
1780 ## These are methods to create a new connection.
1781 ## One can connect to a server, (data-server,render-server)
1782 ## or simply create a built-in connection.
1783 ## Note: these are internal methods. Use Connect() instead.
1784 def _connectServer(host, port, rc=False):
1785     """Connect to a host:port. Returns the connection object if successfully
1786     connected with the server. Internal method, use Connect() instead."""
1787     pm =  vtkProcessModule.GetProcessModule()
1788     if not rc:
1789         cid = pm.ConnectToRemote(host, port)
1790         if not cid:
1791             return None
1792         conn = Connection(cid)
1793     else:
1794         pm.AcceptConnectionsOnPort(port)
1795         print "Waiting for connection..."
1796         while True:
1797             cid = pm.MonitorConnections(10)
1798             if cid > 0:
1799                 conn = Connection(cid)
1800                 break
1801         pm.StopAcceptingAllConnections()
1802     conn.SetHost(host, port, None, None, rc)
1803     return conn
1804
1805 def _connectDsRs(ds_host, ds_port, rs_host, rs_port):
1806     """Connect to a dataserver at (ds_host:ds_port) and to a render server
1807     at (rs_host:rs_port).
1808     Returns the connection object if successfully connected
1809     with the server. Internal method, use Connect() instead."""
1810     pm =  vtkProcessModule.GetProcessModule()
1811     cid = pm.ConnectToRemote(ds_host, ds_port, rs_host, rs_port)
1812     if not cid:
1813         return None
1814     conn = Connection(cid)
1815     conn.SetHost(ds_host, ds_port, rs_host, rs_port)
1816     return conn
1817
1818 def _connectSelf():
1819     """Creates a new self connection.Internal method, use Connect() instead."""
1820     pm =  vtkProcessModule.GetProcessModule()
1821     pmOptions = pm.GetOptions()
1822     if pmOptions.GetProcessType() == 0x40: # PVBATCH
1823         return Connection(vtkProcessModuleConnectionManager.GetRootServerConnectionID())
1824     cid = pm.ConnectToSelf()
1825     if not cid:
1826         return None
1827     conn = Connection(cid)
1828     conn.SetHost("builtin", cid)
1829     return conn
1830
1831 def SaveState(filename):
1832     """Given a state filename, saves the state of objects registered
1833     with the proxy manager."""
1834     pm = ProxyManager()
1835     pm.SaveState(filename)
1836
1837 def LoadState(filename, connection=None):
1838     """Given a state filename and an optional connection, loads the server
1839     manager state."""
1840     if not connection:
1841         connection = ActiveConnection
1842     if not connection:
1843         raise RuntimeError, "Cannot load state without a connection"
1844     loader = vtkSMPQStateLoader()
1845     pm = ProxyManager()
1846     pm.LoadState(filename, ActiveConnection.ID, loader)
1847     views = GetRenderViews()
1848     for view in views:
1849         # Make sure that the client window size matches the
1850         # ViewSize property. In paraview, the GUI takes care
1851         # of this.
1852         if view.GetClassName() == "vtkSMIceTDesktopRenderViewProxy":
1853             view.GetRenderWindow().SetSize(view.ViewSize[0], \
1854                                            view.ViewSize[1])
1855
1856 def Connect(ds_host=None, ds_port=11111, rs_host=None, rs_port=11111):
1857     """
1858     Use this function call to create a new connection. On success,
1859     it returns a Connection object that abstracts the connection.
1860     Otherwise, it returns None.
1861     There are several ways in which this function can be called:
1862     * When called with no arguments, it creates a new connection
1863       to the built-in server on the client itself.
1864     * When called with ds_host and ds_port arguments, it
1865       attempts to connect to a server(data and render server on the same server)
1866       on the indicated host:port.
1867     * When called with ds_host, ds_port, rs_host, rs_port, it
1868       creates a new connection to the data server on ds_host:ds_port and to the
1869       render server on rs_host: rs_port.
1870     """
1871     global ActiveConnection
1872     global fromGUI
1873     if fromGUI:
1874         raise RuntimeError, "Cannot create a connection through python. Use the GUI to setup the connection."
1875     if ds_host == None:
1876         connectionId = _connectSelf()
1877     elif rs_host == None:
1878         connectionId = _connectServer(ds_host, ds_port)
1879     else:
1880         connectionId = _connectDsRs(ds_host, ds_port, rs_host, rs_port)
1881     if not ActiveConnection:
1882         ActiveConnection = connectionId
1883     return connectionId
1884        
1885 def ReverseConnect(port=11111):
1886     """
1887     Use this function call to create a new connection. On success,
1888     it returns a Connection object that abstracts the connection.
1889     Otherwise, it returns None.
1890     In reverse connection mode, the client waits for a connection
1891     from the server (client has to be started first). The server
1892     then connects to the client (run pvserver with -rc and -ch
1893     option).
1894     The optional port specified the port to listen to.
1895     """
1896     global ActiveConnection
1897     global fromGUI
1898     if fromGUI:
1899         raise RuntimeError, "Cannot create a connection through python. Use the GUI to setup the connection."
1900     connectionId = _connectServer("Reverse connection", port, True)
1901     if not ActiveConnection:
1902         ActiveConnection = connectionId
1903     return connectionId
1904
1905 def Disconnect(connection=None):
1906     """Disconnects the connection. Make sure to clear the proxy manager
1907     first."""
1908     global ActiveConnection
1909     global fromGUI
1910     if fromGUI:
1911         raise RuntimeError, "Cannot disconnect through python. Use the GUI to disconnect."
1912     if not connection or connection == ActiveConnection:
1913         connection = ActiveConnection
1914         ActiveConnection = None
1915     if connection:
1916         pm =  vtkProcessModule.GetProcessModule()
1917         pm.Disconnect(connection.ID)
1918     return
1919
1920 def CreateProxy(xml_group, xml_name, connection=None):
1921     """Creates a proxy. If connection is set, the proxy's connection ID is
1922     set accordingly. If connection is None, ActiveConnection is used, if
1923     present. You should not have to use method normally. Instantiate the
1924     appropriate class from the appropriate module, for example:
1925     sph = servermanager.sources.SphereSource()"""
1926
1927     pxm = ProxyManager()
1928     aProxy = pxm.NewProxy(xml_group, xml_name)
1929     if not aProxy:
1930         return None
1931     if not connection:
1932         connection = ActiveConnection
1933     if connection:
1934         aProxy.SetConnectionID(connection.ID)
1935     return aProxy
1936
1937 def GetRenderView(connection=None):
1938     """Return the render view in use.  If more than one render view is in
1939     use, return the first one."""
1940
1941     if not connection:
1942         connection = ActiveConnection
1943     render_module = None
1944     for aProxy in ProxyManager().NewConnectionIterator(connection):
1945         if aProxy.IsA("vtkSMRenderViewProxy"):
1946             render_module = aProxy
1947             break
1948     return render_module
1949
1950 def GetRenderViews(connection=None):
1951     """Returns the set of all render views."""
1952
1953     if not connection:
1954         connection = ActiveConnection
1955     render_modules = []
1956     for aProxy in ProxyManager().NewConnectionIterator(connection):
1957         if aProxy.IsA("vtkSMRenderViewProxy"):
1958             render_modules.append(aProxy)
1959     return render_modules
1960
1961 def CreateRenderView(connection=None, **extraArgs):
1962     """Creates a render window on the particular connection. If connection
1963     is not specified, then the active connection is used, if available.
1964
1965     This method can also be used to initialize properties by passing
1966     keyword arguments where the key is the name of the property. In addition
1967     registrationGroup and registrationName (optional) can be specified (as
1968     keyword arguments) to automatically register the proxy with the proxy
1969     manager."""
1970     return _create_view("RenderView", connection, **extraArgs)
1971
1972 def _create_view(view_xml_name, connection=None, **extraArgs):
1973     """Creates a view on the particular connection. If connection
1974     is not specified, then the active connection is used, if available.
1975     This method can also be used to initialize properties by passing
1976     keyword arguments where the key is the name of the property."""
1977     if not connection:
1978         connection = ActiveConnection
1979     if not connection:
1980         raise RuntimeError, "Cannot create view without connection."
1981     pxm = ProxyManager()
1982     prototype = pxm.GetPrototypeProxy("views", view_xml_name)
1983     proxy_xml_name = prototype.GetSuggestedViewType(connection.ID)
1984     view_module = None
1985     if proxy_xml_name:
1986         view_module = CreateProxy("views", proxy_xml_name, connection)
1987     if not view_module:
1988         return None
1989     extraArgs['proxy'] = view_module
1990     proxy = rendering.__dict__[view_module.GetXMLName()](**extraArgs)
1991     return proxy
1992
1993 def GetRepresentation(aProxy, view):
1994     for rep in view.Representations:
1995         #VSV: ==
1996         try: isRep = rep.Input.IsSame(aProxy)
1997         except: isRep = False
1998         if isRep: return rep
1999     return None
2000
2001 def CreateRepresentation(aProxy, view, **extraArgs):
2002     """Creates a representation for the proxy and adds it to the render
2003     module.
2004
2005     This method can also be used to initialize properties by passing
2006     keyword arguments where the key is the name of the property.In addition
2007     registrationGroup and registrationName (optional) can be specified (as
2008     keyword arguments) to automatically register the proxy with the proxy
2009     manager.
2010
2011     This method tries to create the best possible representation for the given
2012     proxy in the given view. Additionally, the user can specify proxyName
2013     (optional) to create a representation of a particular type."""
2014
2015     global rendering
2016     if not aProxy:
2017         raise RuntimeError, "proxy argument cannot be None."
2018     if not view:
2019         raise RuntimeError, "view argument cannot be None."
2020     if "proxyName" in extraArgs:
2021       display = CreateProxy("representations", extraArgs['proxyName'], None)
2022       del extraArgs['proxyName']
2023     else:
2024       display = view.SMProxy.CreateDefaultRepresentation(aProxy.SMProxy, 0)
2025       if display:
2026         display.UnRegister(None)
2027     if not display:
2028         return None
2029     display.SetConnectionID(aProxy.GetConnectionID())
2030     extraArgs['proxy'] = display
2031     proxy = rendering.__dict__[display.GetXMLName()](**extraArgs)
2032     proxy.Input = aProxy
2033     proxy.UpdateVTKObjects()
2034     view.Representations.append(proxy)
2035     return proxy
2036
2037 class _ModuleLoader(object):
2038     def find_module(self, fullname, path=None):
2039         if vtkPVPythonModule.HasModule(fullname):
2040             return self
2041         else:
2042             return None
2043     def load_module(self, fullname):
2044         import imp
2045         moduleInfo = vtkPVPythonModule.GetModule(fullname)
2046         if not moduleInfo:
2047             raise ImportError
2048         module = sys.modules.setdefault(fullname, imp.new_module(fullname))
2049         module.__file__ = "<%s>" % moduleInfo.GetFullName()
2050         module.__loader__ = self
2051         if moduleInfo.GetIsPackage:
2052             module.__path__ = moduleInfo.GetFullName()
2053         code = compile(moduleInfo.GetSource(), module.__file__, 'exec')
2054         exec code in module.__dict__
2055         return module
2056
2057 def LoadXML(xmlstring):
2058     """Given a server manager XML as a string, parse and process it."""
2059     parser = vtkSMXMLParser()
2060     if not parser.Parse(xmlstring):
2061         raise RuntimeError, "Problem parsing XML string."
2062     parser.ProcessConfiguration(vtkSMObject.GetProxyManager())
2063     # Update the modules
2064     updateModules()
2065
2066 def LoadPlugin(filename,  remote=True, connection=None):
2067     """ Given a filename and a connection (optional, otherwise uses
2068     ActiveConnection), loads a plugin. It then updates the sources,
2069     filters and rendering modules."""
2070
2071     if not connection:
2072         connection = ActiveConnection
2073     if not connection:
2074         raise RuntimeError, "Cannot load a plugin without a connection."
2075
2076     pxm=ProxyManager()
2077     plm=pxm.GetApplication().GetPluginManager()
2078     
2079     """ Load the plugin on server. """
2080     if remote:
2081       serverURI = connection.GetURI()
2082     else:
2083       serverURI = "builtin:"
2084     
2085     plinfo = plm.LoadPlugin(filename, connection.ID, serverURI, remote)
2086     
2087     if not plinfo or not plinfo.GetLoaded():
2088         # Assume that it is an xml file
2089         f = open(filename, 'r')
2090         try:
2091             LoadXML(f.read())
2092         except RuntimeError:
2093             raise RuntimeError, "Problem loading plugin %s: %s" % (filename, pld.GetProperty("Error").GetElement(0))
2094     else:
2095         updateModules()
2096
2097
2098 def Fetch(input, arg1=None, arg2=None, idx=0):
2099     """
2100     A convenience method that moves data from the server to the client,
2101     optionally performing some operation on the data as it moves.
2102     The input argument is the name of the (proxy for a) source or filter
2103     whose output is needed on the client.
2104
2105     You can use Fetch to do three things:
2106
2107     If arg1 is None (the default) then all of the data is brought to the client.
2108     In parallel runs an appropriate append Filter merges the
2109     data on each processor into one data object. The filter chosen will be
2110     vtkAppendPolyData for vtkPolyData, vtkAppendRectilinearGrid for
2111     vtkRectilinearGrid, vtkMultiBlockDataGroupFilter for vtkCompositeData,
2112     and vtkAppendFilter for anything else.
2113
2114     If arg1 is an integer then one particular processor's output is brought to
2115     the client. In serial runs the arg is ignored. If you have a filter that
2116     computes results in parallel and brings them to the root node, then set
2117     arg to be 0.
2118
2119     If arg1 and arg2 are a algorithms, for example vtkMinMax, the algorithm
2120     will be applied to the data to obtain some result. Here arg1 will be
2121     applied pre-gather and arg2 will be applied post-gather. In parallel
2122     runs the algorithm will be run on each processor to make intermediate
2123     results and then again on the root processor over all of the
2124     intermediate results to create a global result.
2125
2126     Optional argument idx is used to specify the output port number to fetch the
2127     data from. Default is port 0.
2128     """
2129
2130     import types
2131
2132     #create the pipeline that reduces and transmits the data
2133     gvd = rendering.ClientDeliveryRepresentationBase()
2134     gvd.AddInput(0, input, idx, "DONTCARE")
2135
2136     if arg1 == None:
2137         print "getting appended"
2138
2139         cdinfo = input.GetDataInformation(idx).GetCompositeDataInformation()
2140         if cdinfo.GetDataIsComposite():
2141             print "use composite data append"
2142             gvd.SetReductionType(5)
2143
2144         elif input.GetDataInformation(idx).GetDataClassName() == "vtkPolyData":
2145             print "use append poly data filter"
2146             gvd.SetReductionType(1)
2147
2148         elif input.GetDataInformation(idx).GetDataClassName() == "vtkRectilinearGrid":
2149             print "use append rectilinear grid filter"
2150             gvd.SetReductionType(4)
2151
2152         elif input.GetDataInformation(idx).IsA("vtkDataSet"):
2153             print "use unstructured append filter"
2154             gvd.SetReductionType(2)
2155
2156
2157     elif type(arg1) is types.IntType:
2158         print "getting node %d" % arg1
2159         gvd.SetReductionType(3)
2160         gvd.SetPreGatherHelper(None)
2161         gvd.SetPostGatherHelper(None)
2162         gvd.SetPassThrough(arg1)
2163
2164     else:
2165         print "applying operation"
2166         gvd.SetReductionType(6) # CUSTOM
2167         gvd.SetPreGatherHelper(arg1)
2168         gvd.SetPostGatherHelper(arg2)
2169         gvd.SetPassThrough(-1)
2170
2171     #go!
2172     gvd.UpdateVTKObjects()
2173     gvd.Update()
2174     op = gvd.GetOutput()
2175     opc = gvd.GetOutput().NewInstance()
2176     opc.ShallowCopy(op)
2177     opc.UnRegister(None)
2178     return opc
2179
2180 def AnimateReader(reader, view, filename=None):
2181     """This is a utility function that, given a reader and a view
2182     animates over all time steps of the reader. If the optional
2183     filename is provided, a movie is created (type depends on the
2184     extension of the filename."""
2185     if not reader:
2186         raise RuntimeError, "No reader was specified, cannot animate."
2187     if not view:
2188         raise RuntimeError, "No view was specified, cannot animate."
2189     # Create an animation scene
2190     scene = animation.AnimationScene()
2191
2192     # We need to have the reader and the view registered with
2193     # the time keeper. This is how the scene gets its time values.
2194     try:
2195         tk = ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
2196         scene.TimeKeeper = tk
2197     except IndexError:
2198         tk = misc.TimeKeeper()
2199         scene.TimeKeeper = tk
2200
2201     if not reader in tk.TimeSources:
2202         tk.TimeSources.append(reader)
2203     if not view in tk.Views:
2204         tk.Views.append(view)
2205
2206
2207     # with 1 view
2208     scene.ViewModules = [view]
2209     # Update the reader to get the time information
2210     reader.UpdatePipelineInformation()
2211     # Animate from 1st time step to last
2212     scene.StartTime = reader.TimestepValues.GetData()[0]
2213     scene.EndTime = reader.TimestepValues.GetData()[-1]
2214
2215     # Each frame will correspond to a time step
2216     scene.PlayMode = 2 #Snap To Timesteps
2217
2218     # Create a special animation cue for time.
2219     cue = animation.TimeAnimationCue()
2220     cue.AnimatedProxy = view
2221     cue.AnimatedPropertyName = "ViewTime"
2222     scene.Cues = [cue]
2223
2224     if filename:
2225         writer = vtkSMAnimationSceneImageWriter()
2226         writer.SetFileName(filename)
2227         writer.SetFrameRate(1)
2228         writer.SetAnimationScene(scene.SMProxy)
2229
2230         # Now save the animation.
2231         if not writer.Save():
2232             raise RuntimeError, "Saving of animation failed!"
2233     else:
2234         scene.Play()
2235     return scene
2236
2237 def GetProgressPrintingIsEnabled():
2238     return progressObserverTag is not None
2239
2240 def SetProgressPrintingEnabled(value):
2241     """Is not supported because of not supported observers"""
2242     pass
2243
2244 def ToggleProgressPrinting():
2245     """Turn on/off printing of progress.  See SetProgressPrintingEnabled."""
2246     SetProgressPrintingEnabled(not GetProgressPrintingIsEnabled())
2247
2248 def Finalize():
2249     """Although not required, this can be called at exit to cleanup."""
2250     global progressObserverTag
2251     # Make sure to remove the observer
2252     if progressObserverTag:
2253         ToggleProgressPrinting()
2254     vtkInitializationHelper.Finalize()
2255
2256 # Internal methods
2257
2258 def _getPyProxy(smproxy, outputPort=0):
2259     """Returns a python wrapper for a server manager proxy. This method
2260     first checks if there is already such an object by looking in the
2261     _pyproxies group and returns it if found. Otherwise, it creates a
2262     new one. Proxies register themselves in _pyproxies upon creation."""
2263     if not smproxy:
2264         return None
2265     if (smproxy, outputPort) in _pyproxies:
2266         return _pyproxies[(smproxy, outputPort)]()
2267
2268     xmlName = smproxy.GetXMLName()
2269     if smproxy.GetXMLLabel():
2270         xmlName = smproxy.GetXMLLabel()
2271     classForProxy = _findClassForProxy(_make_name_valid(xmlName), smproxy.GetXMLGroup())
2272     if classForProxy:
2273         retVal = classForProxy(proxy=smproxy, port=outputPort)
2274     else:
2275         retVal = Proxy(proxy=smproxy, port=outputPort)
2276     return retVal
2277
2278 def _makeUpdateCameraMethod(rv):
2279     """ This internal method is used to create observer methods """
2280     if not hasattr(rv(), "BlockUpdateCamera"):
2281         rv().add_attribute("BlockUpdateCamera", False)
2282     def UpdateCamera(obj, string):
2283         if not rv().BlockUpdateCamera:
2284           # used to avoid some nasty recursion that occurs when interacting in
2285           # the GUI.
2286           rv().BlockUpdateCamera = True
2287           rv().SynchronizeCameraProperties()
2288           rv().BlockUpdateCamera = False
2289     return UpdateCamera
2290
2291 def _createInitialize(group, name):
2292     """Internal method to create an Initialize() method for the sub-classes
2293     of Proxy"""
2294     pgroup = group
2295     pname = name
2296     def aInitialize(self, connection=None):
2297         if not connection:
2298             connection = ActiveConnection
2299         if not connection:
2300             raise RuntimeError,\
2301                   'Cannot create a proxy without a connection.'
2302         self.InitializeFromProxy(\
2303             CreateProxy(pgroup, pname, connection))
2304     return aInitialize
2305
2306 def _createGetProperty(pName):
2307     """Internal method to create a GetXXX() method where XXX == pName."""
2308     propName = pName
2309     def getProperty(self):
2310         return self.GetPropertyValue(propName)
2311     return getProperty
2312
2313 def _createSetProperty(pName):
2314     """Internal method to create a SetXXX() method where XXX == pName."""
2315     propName = pName
2316     def setProperty(self, value):
2317         return self.SetPropertyWithName(propName, value)
2318     return setProperty
2319
2320 def _findClassForProxy(xmlName, xmlGroup):
2321     """Given the xmlName for a proxy, returns a Proxy class. Note
2322     that if there are duplicates, the first one is returned."""
2323     global sources, filters, writers, rendering, animation, implicit_functions,\
2324            piecewise_functions, extended_sources, misc
2325     if not xmlName:
2326         return None
2327     if xmlGroup == "sources":
2328         return sources.__dict__[xmlName]
2329     elif xmlGroup == "filters":
2330         return filters.__dict__[xmlName]
2331     elif xmlGroup == "implicit_functions":
2332         return implicit_functions.__dict__[xmlName]
2333     elif xmlGroup == "piecewise_functions":
2334         return piecewise_functions.__dict__[xmlName]
2335     elif xmlGroup == "writers":
2336         return writers.__dict__[xmlName]
2337     elif xmlGroup == "extended_sources":
2338         return extended_sources.__dict__[xmlName]
2339     elif xmlName in rendering.__dict__:
2340         return rendering.__dict__[xmlName]
2341     elif xmlName in animation.__dict__:
2342         return animation.__dict__[xmlName]
2343     elif xmlName in misc.__dict__:
2344         return misc.__dict__[xmlName]
2345     else:
2346         return None
2347
2348 def _printProgress(caller, event):
2349     """The default event handler for progress. Prints algorithm
2350     name and 1 '.' per 10% progress."""
2351     global currentAlgorithm, currentProgress
2352
2353     pm = vtkProcessModule.GetProcessModule()
2354     progress = pm.GetLastProgress() / 10
2355     # If we got a 100% as the first thing, ignore
2356     # This is to get around the fact that some vtk
2357     # algorithms report 100% more than once (which is
2358     # a bug)
2359     if not currentAlgorithm and progress == 10:
2360         return
2361     alg = pm.GetLastProgressName()
2362     if alg != currentAlgorithm and alg:
2363         if currentAlgorithm:
2364             while currentProgress <= 10:
2365                 import sys
2366                 sys.stdout.write(".")
2367                 currentProgress += 1
2368             print "]"
2369             currentProgress = 0
2370         print alg, ": [ ",
2371         currentAlgorithm = alg
2372     while currentProgress <= progress:
2373         import sys
2374         sys.stdout.write(".")
2375         #sys.stdout.write("%d " % pm.GetLastProgress())
2376         currentProgress += 1
2377     if progress == 10:
2378         print "]"
2379         currentAlgorithm = None
2380         currentProgress = 0
2381
2382 def updateModules():
2383     """Called when a plugin is loaded, this method updates
2384     the proxy class object in all known modules."""
2385     global sources, filters, writers, rendering, animation, implicit_functions,\
2386            piecewise_functions, extended_sources, misc
2387
2388     createModule("sources", sources)
2389     createModule("filters", filters)
2390     createModule("writers", writers)
2391     createModule("representations", rendering)
2392     createModule("views", rendering)
2393     createModule("lookup_tables", rendering)
2394     createModule("textures", rendering)
2395     createModule("animation", animation)
2396     createModule("misc", misc)
2397     createModule('animation_keyframes', animation)
2398     createModule('implicit_functions', implicit_functions)
2399     createModule('piecewise_functions', piecewise_functions)
2400     createModule("extended_sources", extended_sources)
2401     createModule("incremental_point_locators", misc)
2402
2403 def _createModules():
2404     """Called when the module is loaded, this creates sub-
2405     modules for all know proxy groups."""
2406     global sources, filters, writers, rendering, animation, implicit_functions,\
2407            piecewise_functions, extended_sources, misc
2408
2409     sources = createModule('sources')
2410     filters = createModule('filters')
2411     writers = createModule('writers')
2412     rendering = createModule('representations')
2413     createModule('views', rendering)
2414     createModule("lookup_tables", rendering)
2415     createModule("textures", rendering)
2416     animation = createModule('animation')
2417     createModule('animation_keyframes', animation)
2418     implicit_functions = createModule('implicit_functions')
2419     piecewise_functions = createModule('piecewise_functions')
2420     extended_sources = createModule("extended_sources")
2421     misc = createModule("misc")
2422     createModule("incremental_point_locators", misc)
2423
2424 class PVModule(object):
2425     pass
2426
2427 def _make_name_valid(name):
2428     """Make a string into a valid Python variable name.  Return None if
2429     the name contains parentheses."""
2430     if not name or '(' in name or ')' in name:
2431         return None
2432     import string
2433     valid_chars = "_%s%s" % (string.ascii_letters, string.digits)
2434     name = str().join([c for c in name if c in valid_chars])
2435     if not name[0].isalpha():
2436         name = 'a' + name
2437     return name
2438
2439 def createModule(groupName, mdl=None):
2440     """Populates a module with proxy classes defined in the given group.
2441     If mdl is not specified, it also creates the module"""
2442
2443     pxm = vtkSMObject.GetProxyManager()
2444     # Use prototypes to find all proxy types.
2445     pxm.InstantiateGroupPrototypes(groupName)
2446
2447     debug = False
2448     if not mdl:
2449         debug = True
2450         mdl = PVModule()
2451     numProxies = pxm.GetNumberOfXMLProxies(groupName)
2452     for i in range(numProxies):
2453         proxyName = pxm.GetXMLProxyName(groupName, i)
2454         proto = pxm.GetPrototypeProxy(groupName, proxyName)
2455         pname = proxyName
2456         if proto.GetXMLLabel():
2457             pname = proto.GetXMLLabel()
2458         pname = _make_name_valid(pname)
2459         if not pname:
2460             continue
2461         if pname in mdl.__dict__:
2462             if debug:
2463                 print "Warning: %s is being overwritten. This may point to an issue in the ParaView configuration files" % pname
2464         cdict = {}
2465         # Create an Initialize() method for this sub-class.
2466         cdict['Initialize'] = _createInitialize(groupName, proxyName)
2467         iter = PropertyIterator(proto)
2468         # Add all properties as python properties.
2469         for prop in iter:
2470             propName = iter.GetKey()
2471             if (prop.GetInformationOnly() and propName != "TimestepValues" ) or prop.GetIsInternal():
2472                 continue
2473             names = [propName]
2474             names = [iter.PropertyLabel]
2475                 
2476             propDoc = None
2477             if prop.GetDocumentation():
2478                 propDoc = prop.GetDocumentation().GetDescription()
2479             for name in names:
2480                 name = _make_name_valid(name)
2481                 if name:
2482                     cdict[name] = property(_createGetProperty(propName),
2483                                            _createSetProperty(propName),
2484                                            None,
2485                                            propDoc)
2486         # Add the documentation as the class __doc__
2487         if proto.GetDocumentation() and \
2488            proto.GetDocumentation().GetDescription():
2489             doc = proto.GetDocumentation().GetDescription()
2490         else:
2491             doc = Proxy.__doc__
2492         cdict['__doc__'] = doc
2493         # Create the new type
2494         if proto.GetXMLName() == "ExodusIIReader":
2495             superclasses = (ExodusIIReaderProxy,)
2496         elif proto.IsA("vtkSMSourceProxy"):
2497             superclasses = (SourceProxy,)
2498         else:
2499             superclasses = (Proxy,)
2500
2501         cobj = type(pname, superclasses, cdict)
2502         # Add it to the modules dictionary
2503         mdl.__dict__[pname] = cobj
2504     return mdl
2505
2506
2507 def __determineGroup(proxy):
2508     """Internal method"""
2509     if not proxy:
2510         return None
2511     xmlgroup = proxy.GetXMLGroup()
2512     xmlname = proxy.GetXMLName()
2513     if xmlgroup == "sources":
2514         return "sources"
2515     elif xmlgroup == "filters":
2516         return "sources"
2517     elif xmlgroup == "views":
2518         return "views"
2519     elif xmlgroup == "representations":
2520         if xmlname == "ScalarBarWidgetRepresentation":
2521             return "scalar_bars"
2522         return "representations"
2523     elif xmlgroup == "lookup_tables":
2524         return "lookup_tables"
2525     elif xmlgroup == "implicit_functions":
2526         return "implicit_functions"
2527     elif xmlgroup == "piecewise_functions":
2528         return "piecewise_functions"
2529     return None
2530
2531 __nameCounter = {}
2532 def __determineName(proxy, group):
2533     global __nameCounter
2534     name = _make_name_valid(proxy.GetXMLLabel())
2535     if not name:
2536         return None
2537     if not __nameCounter.has_key(name):
2538         __nameCounter[name] = 1
2539         val = 1
2540     else:
2541         __nameCounter[name] += 1
2542         val = __nameCounter[name]
2543     return "%s%d" % (name, val)
2544
2545 def __getName(proxy, group):
2546     pxm = ProxyManager()
2547     if isinstance(proxy, Proxy):
2548         proxy = proxy.SMProxy
2549     return pxm.GetProxyName(group, proxy)
2550
2551 class MissingRegistrationInformation(Exception):
2552     """Exception for missing registration information. Raised when a name or group 
2553     is not specified or when a group cannot be deduced."""
2554     pass
2555     
2556 def Register(proxy, **extraArgs):
2557     """Registers a proxy with the proxy manager. If no 'registrationGroup' is
2558     specified, then the group is inferred from the type of the proxy.
2559     'registrationName' may be specified to register with a particular name
2560     otherwise a default name will be created."""
2561     # TODO: handle duplicate registration
2562     if "registrationGroup" in extraArgs:
2563         registrationGroup = extraArgs["registrationGroup"]
2564     else:
2565         registrationGroup = __determineGroup(proxy)
2566
2567     if "registrationName" in extraArgs:
2568         registrationName = extraArgs["registrationName"]
2569     else:
2570         registrationName = __determineName(proxy, registrationGroup)
2571     if registrationGroup and registrationName:
2572         pxm = ProxyManager()
2573         pxm.RegisterProxy(registrationGroup, registrationName, proxy)
2574     else:
2575         raise MissingRegistrationInformation, "Registration error %s %s." % (registrationGroup, registrationName)
2576     return (registrationGroup, registrationName)
2577
2578 def UnRegister(proxy, **extraArgs):
2579     """UnRegisters proxies registered using Register()."""
2580     if "registrationGroup" in extraArgs:
2581         registrationGroup = extraArgs["registrationGroup"]
2582     else:
2583         registrationGroup = __determineGroup(proxy)
2584
2585     if "registrationName" in extraArgs:
2586         registrationName = extraArgs["registrationName"]
2587     else:
2588         registrationName = __getName(proxy, registrationGroup)
2589
2590     if registrationGroup and registrationName:
2591         pxm = ProxyManager()
2592         pxm.UnRegisterProxy(registrationGroup, registrationName, proxy)
2593     else:
2594         raise RuntimeError, "UnRegistration error."
2595     return (registrationGroup, registrationName)
2596
2597 def demo1():
2598     """This simple demonstration creates a sphere, renders it and delivers
2599     it to the client using Fetch. It returns a tuple of (data, render
2600     view)"""
2601     if not ActiveConnection:
2602         Connect()
2603     ss = sources.Sphere(Radius=2, ThetaResolution=32)
2604     shr = filters.Shrink(Input=OutputPort(ss,0))
2605     cs = sources.Cone()
2606     app = filters.AppendDatasets()
2607     app.Input = [shr, cs]
2608     rv = CreateRenderView()
2609     rep = CreateRepresentation(app, rv)
2610     rv.ResetCamera()
2611     rv.StillRender()
2612     data = Fetch(ss)
2613
2614     return (data, rv)
2615
2616 def demo2(fname="/Users/berk/Work/ParaViewData/Data/disk_out_ref.ex2"):
2617     """This method demonstrates the user of a reader, representation and
2618     view. It also demonstrates how meta-data can be obtained using proxies.
2619     Make sure to pass the full path to an exodus file. Also note that certain
2620     parameters are hard-coded for disk_out_ref.ex2 which can be found
2621     in ParaViewData. This method returns the render view."""
2622     if not ActiveConnection:
2623         Connect()
2624     # Create the exodus reader and specify a file name
2625     reader = sources.ExodusIIReader(FileName=fname)
2626     # Get the list of point arrays.
2627     arraySelection = reader.PointVariables
2628     print arraySelection.Available
2629     # Select all arrays
2630     arraySelection.SetData(arraySelection.Available)
2631
2632     # Next create a default render view appropriate for the connection type.
2633     rv = CreateRenderView()
2634     # Create the matching representation
2635     rep = CreateRepresentation(reader, rv)
2636     rep.Representation = 1 # Wireframe
2637     # Black background is not pretty
2638     rv.Background = [0.4, 0.4, 0.6]
2639     rv.StillRender()
2640     # Reset the camera to include the whole thing
2641     rv.ResetCamera()
2642     rv.StillRender()
2643     # Change the elevation of the camera. See VTK documentation of vtkCamera
2644     # for camera parameters.
2645     c = rv.GetActiveCamera()
2646     c.Elevation(45)
2647     rv.StillRender()
2648     # Now that the reader execute, let's get some information about it's
2649     # output.
2650     pdi = reader[0].PointData
2651     # This prints a list of all read point data arrays as well as their
2652     # value ranges.
2653     print 'Number of point arrays:', len(pdi)
2654     for i in range(len(pdi)):
2655         ai = pdi[i]
2656         print "----------------"
2657         print "Array:", i, ai.Name, ":"
2658         numComps = ai.GetNumberOfComponents()
2659         print "Number of components:", numComps
2660         for j in range(numComps):
2661             print "Range:", ai.GetRange(j)
2662     # White is boring. Let's color the geometry using a variable.
2663     # First create a lookup table. This object controls how scalar
2664     # values are mapped to colors. See VTK documentation for
2665     # details.
2666     lt = rendering.PVLookupTable()
2667     # Assign it to the representation
2668     rep.LookupTable = lt
2669     # Color by point array called Pres
2670     rep.ColorAttributeType = 0 # point data
2671     rep.ColorArrayName = "Pres"
2672     # Add to RGB points. These are tuples of 4 values. First one is
2673     # the scalar values, the other 3 the RGB values. This list has
2674     # 2 points: Pres: 0.00678, color: blue, Pres: 0.0288, color: red
2675     lt.RGBPoints = [0.00678, 0, 0, 1, 0.0288, 1, 0, 0]
2676     lt.ColorSpace = 1 # HSV
2677     rv.StillRender()
2678     return rv
2679
2680 def demo3():
2681     """This method demonstrates the use of servermanager with numpy as
2682     well as pylab for plotting. It creates an artificial data sources,
2683     probes it with a line, delivers the result to the client using Fetch
2684     and plots it using pylab. This demo requires numpy and pylab installed.
2685     It returns a tuple of (data, render view)."""
2686     import paraview.numpy_support
2687     import pylab
2688
2689     if not ActiveConnection:
2690         Connect()
2691     # Create a synthetic data source
2692     source = sources.Wavelet()
2693     # Let's get some information about the data. First, for the
2694     # source to execute
2695     source.UpdatePipeline()
2696
2697     di = source.GetDataInformation()
2698     print "Data type:", di.GetPrettyDataTypeString()
2699     print "Extent:", di.GetExtent()
2700     print "Array name:", \
2701           source[0].PointData[0].Name
2702
2703     rv = CreateRenderView()
2704
2705     rep1 = CreateRepresentation(source, rv)
2706     rep1.Representation = 3 # outline
2707
2708     # Let's apply a contour filter
2709     cf = filters.Contour(Input=source, ContourValues=[200])
2710
2711     # Select the array to contour by
2712     #cf.SelectInputScalars = 'RTData'
2713
2714     rep2 = CreateRepresentation(cf, rv)
2715
2716     rv.Background = (0.4, 0.4, 0.6)
2717     # Reset the camera to include the whole thing
2718     rv.StillRender()
2719     rv.ResetCamera()
2720     rv.StillRender()
2721
2722     # Now, let's probe the data
2723     probe = filters.ResampleWithDataset(Input=source)
2724     # with a line
2725     line = sources.Line(Resolution=60)
2726     # that spans the dataset
2727     bounds = di.GetBounds()
2728     print "Bounds: ", bounds
2729     line.Point1 = bounds[0:6:2]
2730     line.Point2 = bounds[1:6:2]
2731
2732     probe.Source = line
2733
2734     # Render with the line
2735     rep3 = CreateRepresentation(line, rv)
2736     rv.StillRender()
2737
2738     # Now deliver it to the client. Remember, this is for small data.
2739     data = Fetch(probe)
2740     # Convert it to a numpy array
2741     data = paraview.numpy_support.vtk_to_numpy(
2742       data.GetPointData().GetArray("RTData"))
2743     # Plot it using matplotlib
2744     pylab.plot(data)
2745     pylab.show()
2746
2747     return (data, rv, probe)
2748
2749 def demo4(fname="/Users/berk/Work/ParaViewData/Data/can.ex2"):
2750     """This method demonstrates the user of AnimateReader for
2751     creating animations."""
2752     if not ActiveConnection:
2753         Connect()
2754     reader = sources.ExodusIIReader(FileName=fname)
2755     view = CreateRenderView()
2756     repr = CreateRepresentation(reader, view)
2757     view.StillRender()
2758     view.ResetCamera()
2759     view.StillRender()
2760     c = view.GetActiveCamera()
2761     c.Elevation(95)
2762     return AnimateReader(reader, view)
2763
2764
2765 def demo5():
2766     """ Simple sphere animation"""
2767     if not ActiveConnection:
2768         Connect()
2769     sphere = sources.Sphere()
2770     view = CreateRenderView()
2771     repr = CreateRepresentation(sphere, view)
2772
2773     view.StillRender()
2774     view.ResetCamera()
2775     view.StillRender()
2776
2777     # Create an animation scene
2778     scene = animation.AnimationScene()
2779     # Add 1 view
2780     scene.ViewModules = [view]
2781
2782     # Create a cue to animate the StartTheta property
2783     cue = animation.KeyFrameAnimationCue()
2784     cue.AnimatedProxy = sphere
2785     cue.AnimatedPropertyName = "StartTheta"
2786     # Add it to the scene's cues
2787     scene.Cues = [cue]
2788
2789     # Create 2 keyframes for the StartTheta track
2790     keyf0 = animation.CompositeKeyFrame()
2791     keyf0.Type = 2 # Set keyframe interpolation type to Ramp.
2792     # At time = 0, value = 0
2793     keyf0.KeyTime = 0
2794     keyf0.KeyValues= [0]
2795
2796     keyf1 = animation.CompositeKeyFrame()
2797     # At time = 1.0, value = 200
2798     keyf1.KeyTime = 1.0
2799     keyf1.KeyValues= [200]
2800
2801     # Add keyframes.
2802     cue.KeyFrames = [keyf0, keyf1]
2803
2804     scene.Play()
2805     return scene
2806
2807 ASSOCIATIONS = { 'POINTS' : 0, 'CELLS' : 1, 'VERTICES' : 4, 'EDGES' : 5, 'ROWS' : 6}
2808
2809 # Users can set the active connection which will be used by API
2810 # to create proxies etc when no connection argument is passed.
2811 # Connect() automatically sets this if it is not already set.
2812 ActiveConnection = None
2813
2814 # Needs to be called when paraview module is loaded from python instead
2815 # of pvpython, pvbatch or GUI.
2816 if not vtkSMObject.GetProxyManager():
2817     vtkInitializationHelper.Initialize(sys.executable)
2818
2819 # Initialize progress printing. Can be turned off by calling
2820 # ToggleProgressPrinting() again.
2821 progressObserverTag = None
2822 currentAlgorithm = False
2823 currentProgress = 0
2824 fromGUI = False
2825 ToggleProgressPrinting()
2826
2827 _pyproxies = {}
2828
2829 # Create needed sub-modules
2830 _createModules()
2831
2832 # Set up our custom importer (if possible)
2833 loader = _ModuleLoader()
2834 sys.meta_path.append(loader)
2835
2836 # Definitions for working in SALOME GUI mode
2837 aParams = myParavis.GetConnectionParameters()
2838 ActiveConnection = Connection(aParams[0])
2839 ActiveConnection.SetHost(aParams[1], aParams[2], aParams[3], aParams[4], aParams[5])
2840 ToggleProgressPrinting()
2841 fromGUI = True
2842
2843 print vtkSMProxyManager.GetParaViewSourceVersion();
2844