[vtkusers] python vtkSet utility function
Michael Halle
mhalle at bwh.harvard.edu
Thu Apr 22 15:00:44 EDT 2004
For those vtk Python hackers amongst you, here's
a pleasant convenience function called vtkSet that
can greatly reduce your vtk typing effort. It allows
you to use Python keyword arguments to call several
object methods. This technique is particularly
useful for the "Set*" methods.
For example, instead of:
lut = vtk.vtkLookupTable()
lut.SetNumberOfColors(201)
lut.SetHueRange(0.15, 1.0)
lut.SetSaturationRange(1.0, 1.0)
lut.SetValueRange(1.0, 1.0)
lut.SetAlphaRange(1.0, 1.0)
lut.SetRange(0.0, 1.0)
you can do:
lut = vtkSet(vtk.vtkLookupTable(),
numberOfColors=201,
hueRange=(0.15, 1.0),
saturationRange=(1.0, 1.0),
valueRange=(1.0, 1.0),
alphaRange=(1.0, 1.0),
range=(0.0, 1.0))
The keywords for method names can vary depending on your
level of API purity. In the above example, you can use:
SetNumberOfColors=201 or
NumberOfColors=201 or
numberOfColors=201
Caveats:
Methods are not guaranteed to be called in the order in which
they are listed. Keywords are stored in a Python dictionary --
there's nothing I can do about it.
If you pass a single tuple as a keyword argument, it will
be expanded into individual method arguments. You can always
do a list(arg) or (arg,) to get things to work. If this
explanation means nothing to you, don't worry about it.
Using this function means that vtkpython code won't look
so closely like vtk Tcl or C++ code. Oh well, I'd say.
Enjoy. If it's useful, it could get added to the vtk python
utility code.
Michael Halle
mhalle @ bwh.harvard.edu
-----------------
File vtkset.py:
------------------------------------------------------------------
# vtkset.py
# version 1.0
# April 22, 2004
# Michael Halle
# mhalle @ bwh.harvard.edu
import types
def vtkSet(obj, **kwargs):
"""Call several of the Set* methods for the vtk
object obj at once. The methods to call are
indicated using methodname=arguments keyword-value
argument syntax. The "Set" prefix for the method
name can be omitted, and the first letter of the ivar
name can be lowercase. The object is returned.
Note that the methods are not guaranteed to be called
in the order in which they are listed as keywords. If
ordering is important, use multiple called to vtkSet
or call the Set* methods of the object directly."""
for k in kwargs.keys():
methodName = k
# look for the method name using the following guesses
# 1. keyword (exact matches first),
# 2. Set+keyword,
# 3. Set+keyword with the first letter of keyword cap'ed
nameTry = (k, "Set%s" % k, "Set%s%s" % (k[0].upper(), k[1:]))
for n in nameTry:
if hasattr(obj, n):
methodName = n
break
margs = kwargs[k]
if type(margs) != types.TupleType:
margs = (margs,)
apply(getattr(obj, methodName), margs)
return obj
if __name__ == '__main__':
import vtk
a = vtkSet(vtk.vtkFloatArray(),
numberOfComponents=3,
name="param")
# number of tuples must be set after number of components :(
a.SetNumberOfTuples(100)
print a
More information about the vtkusers
mailing list