/*
This example demonstrates how to connect a runtime custom operator to an object.
*/
NewScene( null, false );
var null1 = GetPrim( "null" );
var null2 = GetPrim( "null" );
// Create a runtime scripted operator and turn on debugging (logs extra information)
var sop = XSIFactory.CreateScriptedOp( "myexpr", myexpr_Update.toString(), "JScript" );
sop.Debug = 1;
// Add a portgroup to read from/write to
var group1 = sop.AddPortGroup( "MainGroup" )
// We use an IO port because we want to blend the existing
// transformation rather than completely replacing it
sop.AddIOPort( null1.Kinematics.Local, "", group1.Index );
// Add a second group with an optional port
var group2 = sop.AddPortGroup( "SecondGroup", 0, 1 )
sop.AddInputPort( null2.Kinematics.Local, "inputs", -1, group2.Index );
// Connect first group. Operator would start functioning immediately
sop.ConnectToGroup( group1.Index, null1.Kinematics.Local );
// Connect to the optional port
sop.ConnectToGroup( 1, null2.Kinematics.Local);
// This has no effect on null1
Translate( null2, 1, -1, 1 );
// This changes null1 rotation as well
Rotate(null2, 90, -45, 0 ) ;
// This operator constrains the rotation of the connected (target) object to the
// rotation of the object connected through the second portgroup.
function myexpr_Update( ctx, out, inlocal1, inlocal2 )
{
Application.LogMessage( "myexpr_Update: " + out.Name );
Application.LogMessage( ""+(inlocal2) );
var transfo = inlocal1.Value.Transform;
// Is there an object connected to the 2nd group?
if ( ctx.Operator.GetNumInstancesInGroup( 1 ) )
{
var inlocal2 = ctx.Operator.PortAt( 0, 1, 0 );
// Is the port connected?
if ( inlocal2.IsConnected )
{
var rot = inlocal2.Value.Transform.Rotation;
transfo.SetRotation ( rot );
}
}
out.Value.Transform = transfo;
}
|