Updating the Attributes of a Master Controller Widget


The attributes of a Master Controller widget can be initialized are their creation. However, it may also be useful to update their attributes in other circumstances.

For example, if a SliderWidget is set to control the horizontal position of a drawing that is already animated to go from left to right. If the user moves the Timeline cursor to a different frame, the drawing will be in a different horizontal position, but the slider's value won't be automatically updated to reflect the drawing's current horizontal position unless we program it to do so.

This can be done by using the global Controller object's onFrameChanged method. This method is called every time the current frame is changed, and it can be overriden with a custom function that will update the attributes of the widget based on the attributes of the element(s) it controls.

A Master Controller widget's attributes can be accessed by their data() method, which returns an object of the Attribute type. The value of a widget can be updated by calling its data().setValue() method. For more information on the Attribute class, refer to the Harmony scripting reference–see Scripting Reference.

// Called when the current frame of the application changes Controller.onFrameChanged = function() { // Get the peg current position in x. var xPegPosition = node.getAttr("Top/My_Peg_A", frame.current(), "POSITION.3DPATH.X").doubleValue(); // If the peg x position is computed as “slider value” / 100.0 * 2.0 // The script widget slider value would be computed as: var sliderValue = xPegPosition * 100.0 / 2.0; Controller.controls[0].data().setValue(sliderValue); }

If more than one attribute was assigned to a widget's data property at its creation, those attributes can be accessed by passing their zero-based index as a parameter to the widget's data() method. Take for example this script where a widget composed of two Point2dWidget linked by a Line2dDisplayWidget are created:

// Create 2 2d point widgets and 1 line 2d widget reusing // the 2d point widget attributes. var pointWidgetA = new Point2dWidget(); var pointWidgetB = new Point2dWidget(); var line2dWidget = new Line2dDisplayWidget( { data : [pointWidgetA.data(), pointWidgetB.data()] });

The two following methods of setting the values of the two Point2DWidget are equivalent, but the second one uses the data() method of the Line2dDisplayWidget and passes the index of each Point2DWidget to access them:

// Set the attribute values of the two 2D point widgets by referring to their // variable names. pointWidgetA.data().setValue(new Point2d(3,3)); pointWidgetB.data().setValue(new Point2d(-3,-3));// Set the attribute values of the two 2D point widgets by referring to the // data array of the 2D line widget. line2dWidget.data(0).setValue(new Point2d(3,3)); line2dWidget.data(1).setValue(new Point2d(-3,-3));