Bouncing Ball Script (Transform Channels)

From The Foundry MODO SDK wiki
Revision as of 13:10, 31 March 2012 by Jangell (Talk | contribs)

Jump to: navigation, search

modo takes a somewhat unique approach to item transforms. Rather than include the transforms as part of the item itself, the item is linked to secondary transform-specific items. This article explains how transform channels work and demonstrates them with script that creates a bouncing ball.

Transform Channels

Every item in modo has channels, and these are the primary manner in which item state is exposed for editing. Some channels are hidden from the user, or represent [Common Datatypes#Complex Datatypes|complex datatypes]] such as mesh stacks and item links. In general, scripts deal only with simple datatypes, notably numeric and string channels.

As described previously, most item channels can be walked through the Sceneservice ScriptQuery interface, and can be read or set using the command. Transform channels, however, are a little different.

Transform Items

Locator item types (which includes Mesh, Camera, Light and so on) do not directly have their own position, rotation and scale channels. Instead, they have links to transform items that handle these properties. This allows multiple items to share the same transform items, and to save memory by not creating the transform items and associated channels when they aren’t required.

This unique system complicates how the position, rotation and scale of an item are read. You can’t directly ask the item for its channels, since they aren't on that item. Instead, you must ask the item for its transform item, and then query that item's channels. The sceneservice ScriptQuery interface provides a series of item.xfrm??? attributes to simplify getting the IDs of the transform items.

Attribute Transform
item.xfrmItems (all)
item.xfrmPos Position
item.xfrmRot Rotation
item.xfrmScl Scale
item.xfrmPiv Pivot
item.xfrmPivC Pivot Compensation

These each require an item ID as a selector, which can be obtained via the attribute of sceneservice. The item.xfrmItems attribute returns a list of all transform items currently used by the selector item. For example, a mesh item might return the following:


You can also directly request a transform item by using one of the other item.xfrm??? attributes. These simply return the item ID string, or an empty string if there is no linked transform item.

As you can see from the table above, there are currently five types of transform items. The position, rotation and scale transforms should be obvious. Pivot defines a "pivot point", which defines the position at which the rotation transform is centered. Pivot compensation is the inverse of the pivot transform; when you move the pivot of an item that has been scaled or rotated, a compensation transform is calculated to ensure that the item remains in the same wold position.

Creating Transform Items

If the transform you want to manipulate does not yet have a matching item, you need to create it first. This example shows how to check for the pivot transform item, and if it is not found, create it through the transform.add command.


Once you have the item ID, you can query it as normal through the command or through the sceneservice interface. Since works on the current item selection, you need to select the transform item first with select.item, or you can pass it as the item argument. This example sets the X position of the pivot item to 1.3 meters.


Bouncing Ball Example

This example perl script by Arnie Cachelin creates a simple bouncing ball style animation by creating keyframes on the position transform item of the currently selected item.


More Information