vmtk
Recent Changes - Search:

vmtk

projects

links

edit SideBar

Tutorials / UsingPypesProgrammatically

Advanced PypeS Tutorial

Once you've gained confidence with the execution of vmtk from the command line you might want to push it a little further and start writing your own Python modules based on PypeS.

In this tutorial we are going to guide you through the creation of a Python script implemented as a submodule of PypeS.

Using PypeS as parent class for your own scripts offers you the advantage of achieving the same performances of the vmtkscripts that you are already using and makes your own scripts able to easily interact with the rest of the vmtk framework.

The module we are about to create has no relevant utility and serves purely as an example. Once completed, the script will display a series of isosurfaces extracted from a same 3D image. Each of the surfaces will represent an increasing value of grey level. The grey levels represented will be limited to a range defined by the user.

Let's start by opening a text editor and creating a new file. We can name it as customscript.py and and we can save it anywhere we like instart writing the first few lines:

 #!/usr/bin/env python

 import sys

 from vmtk import pypes
 from vmtk import vmtkscripts

The #!/usr/bin/env python instruction specifies that the system has to handle this file by using the Python interpreter. The import sys instruction tells Python to load the sys module which is a standard Python library that handles command line arguments. With the last two instructions the interpreter is asked to load the modules PypeS and vmtkScripts described as submodules of vmtk.

The next step is the definition of our module. The module will be named as customScript . We begin by writing its core structure:

 customscript = 'customScript'

 class customScript(pypes.pypeScript):

     def __init__(self):

         pypes.pypeScript.__init__(self)



     def Execute(self): 

         pass

 if __name__==_main_:
     main = pypes.pypeMain()
     main.Arguments = sys.argv
     main.Execute()

customscript = 'customScript' : By this expression we are establishing a relation between the name of our class (customScript) and the name of the file in which this class is defined (customscript). There is no necessary need for the two names to be alike, we could have chosen mymodule.py as filename and kept customScript as module as long as we changed our expression into mymodule = 'customScript' . Further detail on this instantiation will be discussed ahead.

class customScript(pypes.pypeScript): : This line is key for the whole script. We define customScript as a new class derived from pypes.pypeScript. The Vmtk scripts (e.g. vmtkmarchingcubes ) are defined in the very same way therefore our customScript is now on the same level and with the same general properties as any other pypeScript in Vmtk.

def __init__(self): : Defines __init__ as the customized class instantiator. Is invoked every time a new instance of customScript is created.

pypes.pypeScript.__init__(self) : It's the first instruction of our instantiator. It invokes the parent class instantiator.

def Execute(self):

Edit - History - Print - Recent Changes - Search
Page last modified on April 12, 2010, at 10:57 AM