Difference between revisions of "TrakEM2 Scripting"

Line 1: Line 1:
Examples in [[Jython Scripting|Jython]]
+
Examples in [[Jython Scripting|Jython]].
 +
 
 +
Open the "Plugins - Scripting - Jython Interpreter" (see [[Scripting Help]]) and make sure there is a TrakEM2 project open, with a display open. Then type or paste:
 +
 
 +
 
  
 
=== Get the instance of a selected image ===
 
=== Get the instance of a selected image ===
Line 40: Line 44:
 
   d.setLocked(True)
 
   d.setLocked(True)
 
</source>
 
</source>
 +
 +
=== Obtain a collection of selected images ===
 +
 +
The [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Selection.html Selection] object of a [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Display.html Display] can return a number of collections with any selected objects in it, for example of type [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Patch.html Patch] (those that wrap an image). All you need to do is to call <i>[http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Selection.html#getSelected(java.lang.Class) getSelected]</i> with the name of the class to filter for:
 +
 +
<source lang="python">
 +
for d in Display.getFront().getSelected(Patch):
 +
  print d.title
 +
</source>
 +
 +
 +
=== Setting and getting member objects in jython ===
 +
 +
In Jython as in Python, member objects have automatically <i>get</> and <i>set</i> functions.
 +
 +
For example, altough a [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Displayable.html Displayable] has a private <i>String title</i> member, this is valid python code for getting and setting the title of a Displayable like a Patch:
 +
 +
<source lang="python">
 +
>>> p = Display.getFront().getActive()
 +
>>> print p.title
 +
090504_0314_ex0768.mrc
 +
</source>
 +
 +
Above, a Patch takes as title the name of the file containing the ImagePlus, by default. Let's change the title to something else:
 +
 +
<source lang="python">
 +
>>> p = Display.getFront().getActive()
 +
>>> p.title = "A new name for this Patch"
 +
>>> print p.title
 +
A new name for this Patch
 +
</source>
 +
 +
 +
=== The properties of a Displayable: color, visibil

Revision as of 21:51, 28 October 2009

Examples in Jython.

Open the "Plugins - Scripting - Jython Interpreter" (see Scripting Help) and make sure there is a TrakEM2 project open, with a display open. Then type or paste:


Get the instance of a selected image

>>> p = Display.getFront().getActive()
>>> print p
090504_0314_ex0768.mrc z=0.0 #67398


Obtain the ImagePlus of a selected image

>>> p = Display.getFront().getActive()
>>> imp = p.getImagePlus()
>>> print imp.width, imp.height
2048 2048


Access the Layer and Selection of a Display

>>> front = Display.getFront()
>>> layer = front.getLayer()
>>> layer_set = front.getLayerSet()
>>> sel = front.getSelection()
>>> print sel.getSelected().size()
10
>>> print sel.isEmpty()
0

In Jython, 1 is True and 0 is False

Lock all selected objects

for d in Display.getFront().getSelected():
  d.setLocked(True)

Obtain a collection of selected images

The Selection object of a Display can return a number of collections with any selected objects in it, for example of type Patch (those that wrap an image). All you need to do is to call getSelected with the name of the class to filter for:

for d in Display.getFront().getSelected(Patch):
  print d.title


Setting and getting member objects in jython

In Jython as in Python, member objects have automatically get</> and <i>set functions.

For example, altough a Displayable has a private String title member, this is valid python code for getting and setting the title of a Displayable like a Patch:

>>> p = Display.getFront().getActive()
>>> print p.title
090504_0314_ex0768.mrc

Above, a Patch takes as title the name of the file containing the ImagePlus, by default. Let's change the title to something else:

>>> p = Display.getFront().getActive()
>>> p.title = "A new name for this Patch"
>>> print p.title
A new name for this Patch


=== The properties of a Displayable: color, visibil