Jython Console

The "Jython Console" application module enables one to write small programs in the Python programming language to control Modelzilla and Modelzilla applications. Jython is an implementation of Python that runs within the Java environment. (Jython generates Java bytecode based on a Python source file.) Here is a link to the Python manual.

Rather than include Jython support in the Modelzilla framework, it is packaged as an application module, so that there is no Jython dependency in the framework. Over the past few years, we have experimented with Jython, resulting in some Jython dependencies being sprinkled around the Modelzilla code. This has all been removed as of the release of the "Jython Console" ap-mod. The Java-based framework now has nothing to do with Python.

A scripting system is useful for:

  • Automating repetitive assemblies. This especially relevant to CAD work. Often many similar objects need to be created, for example 50 circles with computed radii, that are a pain to enter with the interactive GUI.
  • Building high level templates. In CAD work it is great to be able to create a template object such like a bracket and parameterize it to control such things as the screw hole diameter and overall width and height. This allows you to quickly make many similar versions of the bracket.
  • Doing animations. It is possible to write a loop that modifies parameters in the Modelzilla database, causes a redraw after each set of modifications and does an image dump.
  • Debugging. It is handy to use Jython to print out the state of various objects under debugging scrutiny.

How Macros work

Currently the macro system is limited to creating and specifying database nodes that are creatable through the GUI anyway. Macro development is distinguished from application module development by the classes a macro imports and uses. A macro does not define new database node types, or work with the lower level geometry classes in meshGeom.geom. It is to assemble database nodes. These then do the computation. Thus a macro usually runs quite quickly to simply assemble database nodes, which then to the complex computations after the macro is finished. Because the output of a macro is kept in the database, the result of a macro is saved to the MVG files.

Macros do not remain resident in the database. We are planning on making database resident macros happen in the future. A database resident macro would be like regular database node that accepts input from other nodes and does its job every time the input changes. Its job would typically be to create more database nodes which it would store as children of itself. Issues related to select ability and lifespan of the derived nodes have to be solved before this ability can be offered.


Jython comes with the MZ Jython Console distribution

We are redistributing Jython as allowed by their license agreement. This makes it easier for everyone. You can get the latest versoin of Jython at http://www.jython.org. It is important to set the PYTHON_PATH variable in the file ModelzillaProject/JythonGUI/lib/regestry. This is the file Jython will look at to find its settings. The PYTHON_PATH variable controls where Jython looks for python packages loaded with the import statement. If you have your own Jython already, you may remove the one that comes with our Jython Console distribution, and make sure to point you your copy in the MZ starter script.

Note that as of this writing, Jython crashes the Java virtual machine under Windows.


Using Jython Interactivly

Jython can be used interactively, entering one line at a time. To do this, after loading the Jython ap-mod, hit the "New Console" button. But before you do this, fill in the field "startup" in the little Jython control panel. A Python script given here will be run in the context of the Jython input panel created with the "New Console" button. The examples directory has a small script that creates the X_HighLevels objects. Each console window has an independent Jython context with independent symbol definitions.

Note that we are using a Java GUI based console rather than the operating system console because there is no way to start Jython after Modelzilla otherwise. It is also possible to start MZ from Jython in the OS console with:

>>> from harmonic.mz.manager import *
>>> MZ()
This will enable you to use Jython from the system console if you prefer, although as the Java GUI console improves there will be no reason to do this.

To test the your Jython is working, at the prompt enter something like:

>>> a = 2
>>> b = 3
>>> a+b
This should echo 5 to the Jython console.

Some interactive sample code

The Jython Console lets you use the macro class libraries of each application module and the framework. For example, the Modelzilla framework has a class called MZ_HighLevels, which has a set of functions for doing general management of the model. CAD-Zilla has CAD_HighLevels for doing convenient geometrical modeling. VincentVanMol has VVM_HighLevels for doing convenient molecular graphics. The start of a script that uses these handy classes should do something like:
>>> from harmonic.mz.manager import *
>>> from harmonic.cad.manager import *
>>> from scripps.vvm.manager import *
>>> mz = MZ_HighLevels()
>>> cad = CAD_HighLevels()
>>> vvm = VVM_HighLevels()
You can now use the high level method sets like this:
>>> mol = vvm.loadMolecule("2gep.pdb")
>>> ribbons = vvm.newRibbons(mol)
>>> residues = vvm.interpretRediues("56-72", mol)
>>> ribbons.setResiduesColors("56-72", colors.red)
>>> surface = vvm.newMolecularSurface(mol)
>>> from harmonic.meshGeom.math import *
>>> sphere = cad.sphere(Pnt3f(20, 0, 0), 20)
>>> sub = cad.subract(surface, sphere)
>>> mz.addShape(sub)
You can also use the regular developer API from Jython:
>>> from harmonic.cad.shapes import *	
>>> sphere = SphereFromCenterRadiusShape()
>>> sphere.center.setPoint(5, 0, 0)
>>> sphere.radius.setScalar(3)
>>> cyl = CylinderFromCenterRadiusHeightShape()
>>> cyl.center.setPoint(6, 0, 0)
>>> cyl.radius.setScalar(2)
>>> cyl.height.setScalar(2)
>>> sub = SubractionShape()
>>> sub.positive.addNode(sphere)
>>> sub.negative.addNode(cyl)
>>> sub.addToCurrent()

Running prepared scripts

The most useful way to run Jython scripts is write them in an external editor and simply hit the "Run Script" button from the Jython controls. Once the filename is entered you can just hit this button each time you change the script with your external editor. Another way to do it is to put the script in your PYTHON_PATH directory (set in the file ModelzillaProject/JythonGUI/lib/regestry), and import it from the interactive console, ex:
import FibonocciRectangles
If you make changes to a script, it can be reimported using the Python built-in reload() function. Ex:
reload(FibonocciRectangles)
You will need to use import package_name, then reload(package_name). Doing a from package_name import X will not work for Python reload() function.

Java Macros

Modelzilla has a macro facility that is independent of Jython. It is possible to write a script, aka a macro, in Java. You simply extend the Java class harmonic.mz.functions.Module, override the function execute(), and put the compiled class file in the classpath defined in the mz startup script and run it from the "Applications" control area. A marco in Python has the advantage of not requiring that the Java process be restarted if changes are made to the macro.

Links to the High Level API's

Modelzilla Functions

CAD-Zilla Functions

VincentVanMol Functions


Example macros

FibonocciRectangles.py
FibonocciRectangles.java

MakeHelix.py
MakeHelix.java

MakeSpring.py
MakeSpring.java

Ploter.py

VVM_Macros.py

WarpDNA.py