Examples of Scripted Operators

 
 
 

This section provides a couple of simple examples that cover the basics of scripted operators.

Example 1: A Scripted Operator on Parameters

The following example shows how to apply and edit a simple scripted operator on an object's position parameters. It covers making connections, setting output values, and using custom variables. The final operator places a sphere between two cones according to a weight variable.

Start simple

The first part is to move the sphere between the cones in X.

  1. In a new scene, get two cones and a sphere. Move them around a bit.

  2. When the sphere is selected and the Translate tool is active, the sphere's position parameters are already marked. Choose Animation Set Scripted Operator.

    The scripted operator editor opens with a default operator on the first marked parameter, sphere.kine.local.posx.

  3. Create two new input connections on the cones' X positions. Rename the connections if you want.

  4. In the main editing pane, delete the default code and enter the following line:

    	Out.Value = (InX0.Value + InX1.Value)/2
  5. Click Apply, then move the cones about in X. The sphere stays midway between the cones in X.

Building it up

Now that the operator is working in X, extend it to the other axes.

  1. Add output connections for the Y and Z positions of the sphere, and input parameters for the cones. Follow the same naming convention.

  2. Add the corresponding code to the main update routine. Remember that, since there are multiple output connections, you must test Out.Name.

    If Out.Name = "OutX" Then
    		Out.Value = (InX0.Value + InX1.Value)/2
    	Elseif Out.Name = "OutY" ThenOut.Value = (InY0.Value + InY1.Value)/2ElseOut.Value = (InZ0.Value + InZ1.Value)/2End If
  3. Click Apply and move the cones around again. The sphere stays midway between the cones in all axes.

Adding the finishing touch

The last part is to add a variable for the weight and modify the code accordingly.

  1. Select Variables from the list in the upper left of the scripted operator editor.

  2. Click New. Call the new variable Weight and set its default value to 0.5. Leave the other options as they are.

  3. Modify the main update routine for the weight:

    dim wt
    	wt = In_UpdateContext.Parameters("Weight").Value
    	If Out.Name = "OutX" Then
    		Out.Value = InX0.Value*wt + InX1.Value*(1-wt)
    	Elseif Out.Name = "OutY" Then
    		Out.Value = InY0.Value*wt + InY1.Value*(1-wt)
    	Else
    		Out.Value = InZ0.Value*wt + InZ1.Value*(1-wt)
    	End If
  4. Click Apply and move the cones around again.

  5. Click Inspect. A property editor opens showing the custom Weight parameter.

  6. Adjust the Weight. The sphere moves closer to one cone or the other.

Example 2: A Scripted Operator on an Object's Geometry

This example is of a very basic deformation. It shows how to connect to an object's geometry, and how to get and set the positions of its points. The operator simply truncates the position of points that are below Y = 0.

Connecting to an object's geometry

  1. In a new scene, get a primitive surface sphere.

  2. Click the Selection button on the Select panel, then select the sphere's NURBS Surface Mesh node.

  3. Choose Animation Set Scripted Operator from the Animation panel. The scripted operator editor opens with an output connection on sphere.srfmesh.

  4. Rename the output connection OutGeom, then add a new input connection called InGeom and connect it to sphere.srfmesh as well.

  5. Enter the following lines for the main update routine:

    ' Get an array listing the positions of the points
    Dim pts
    pts = InGeom.Value.Geometry.Points.PositionArray
    
    ' Loop through the array
    ' If a point is below Y = 0, set it to Y = 0
    For i = 0 to UBound(pts,2)
    	If pts(1,i) < 0 Then
    		pts(1,i) = 0
    	End If
    Next
    
    ' Write the new positions to the output
    Out.Value.Geometry.Points.PositionArray = pts
  6. Click Apply. The sphere's points are moved to its local Y = 0 position based on their position relative to its center.

    If you move the object, the points are not affected. That's because the operator is affecting their local positions. For a persistent effect, you would need to convert the input positions to global coordinates, modify them, and convert them back to local coordinates before outputting.

    Also, if you move the center after applying the operator, nothing changes because the Center operator is above the scripted operator in the stack. You need to reapply the operator to see the effect of moving the center.