User Tools

Site Tools


Action Trigger

Summary

Below is an example demonstrating how you can find an action using its class name and then trigger its execution, via script.

API Areas of Interest

Example

Action_Restore_Pose_Trigger.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// String : A function for triggering an action
	function triggerAction( sClassName )
	{
		// Get the action manager
		var oActionMgr = MainWindow.getActionMgr();
		// If we don't have an action manager
		if( !oActionMgr ){
			// We're done...
			return;
		}
 
		// Find the action we want
		var oAction = oActionMgr.findAction( sClassName );
		// If the action was not found
		if( !oAction ){
			// Inform the user...
			MessageBox.warning( 
				text("The \"%1\" action could not be found.").arg( sClassName ),
				text("Resource Error"), text("&Ok"), "" );
			// We're done...
			return;
		}
 
		// If the action is disabled
		if( !oAction.enabled ){
			// Inform the user...
			MessageBox.warning( 
				text("The \"%1\" action is currently disabled.").arg( oAction.text ),
				text("Resource Error"), text("&Ok"), "" );
			// We're done...
			return;
		}
 
		// Trigger execution of the action
		oAction.trigger();
	};
 
	/*********************************************************************/
	// Get the primary selection
	var oNode = Scene.getPrimarySelection();
	// If we don't have a node selected
	if( !oNode ){
		// Inform the user...
		MessageBox.warning(
			text("This script requires an object in the scene to be selected."),
			text("Selection Error"), text("&Ok"), "" );
		// We're done...
		return;
	}
 
	// Define the classname of the action we want to invoke the execution of
	var sAction = "DzRestoreShapeAction";
	// If the selected node is a skeleton or a bone
	if( oNode.inherits( "DzSkeleton" ) || oNode.inherits( "DzBone" ) ){
		// Update the classname accordingly
		sAction = "DzRestoreFigureShapeAction";
	}
 
	// Trigger the action
	triggerAction( sAction );
 
// Finalize the function and invoke
})();