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;
	};
 
	/*********************************************************************/
	// Boolean : A function for testing whether or not a QObject instance
	// inherits one of a list of types
	function inheritsType( oObject, aTypeNames )
	{
		// If the object does not define the 'inherits' function
		if( !oObject || typeof( oObject.inherits ) != "function" ){
			// We are done... it is not a QObject
			return false;
		}
 
		// Iterate over the list of type names
		for( var i = 0, nTypes = aTypeNames.length; i < nTypes; i += 1 ){
			// If the object does not inherit the 'current' type
			if( !oObject.inherits( aTypeNames[i] ) ){
				// Next!!
				continue;
			}
 
			// Return the result
			return true;
		}
 
		// Return the result
		return false;
	};
 
	/*********************************************************************/
	// String : A function for triggering an action
	function triggerAction( sClassName, oSettings )
	{
		// Get the action manager
		var oActionMgr = MainWindow.getActionMgr();
		// If we do not have an action manager
		if( !oActionMgr ){
			// We are 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 are 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 are done...
			return;
		}
 
		// If we have the necessary function (4.16.1.18) and we have settings
		if( typeof( oAction.triggerWithSettings ) == "function" && oSettings ){
			// Trigger execution of the action with the settings
			oAction.triggerWithSettings( oSettings );
		} else {
			// Trigger execution of the action
			oAction.trigger();
		}
	};
 
	/*********************************************************************/
	// Get the primary selection
	var oNode = Scene.getPrimarySelection();
	// If we do not 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 are 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( inheritsType( oNode, ["DzSkeleton", "DzBone"] ) ){
		// Update the classname accordingly
		sAction = "DzRestoreFigureShapeAction";
	}
 
	// Trigger the action
	triggerAction( sAction );
 
// Finalize the function and invoke
})();

Example (With Settings)

Here is the variant of the sample above that triggers an action with settings to control the behavior of the action.

Action_Zero_Pose_Trigger_With_Settings.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;
	};
 
	/*********************************************************************/
	// Boolean : A function for testing whether or not a QObject instance
	// inherits one of a list of types
	function inheritsType( oObject, aTypeNames )
	{
		// If the object does not define the 'inherits' function
		if( !oObject || typeof( oObject.inherits ) != "function" ){
			// We are done... it is not a QObject
			return false;
		}
 
		// Iterate over the list of type names
		for( var i = 0, nTypes = aTypeNames.length; i < nTypes; i += 1 ){
			// If the object does not inherit the 'current' type
			if( !oObject.inherits( aTypeNames[i] ) ){
				// Next!!
				continue;
			}
 
			// Return the result
			return true;
		}
 
		// Return the result
		return false;
	};
 
	/*********************************************************************/
	// String : A function for triggering an action
	function triggerAction( sClassName, oSettings )
	{
		// Get the action manager
		var oActionMgr = MainWindow.getActionMgr();
		// If we do not have an action manager
		if( !oActionMgr ){
			// We are 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 are 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 are done...
			return;
		}
 
		// If we have the necessary function (4.16.1.18) and we have settings
		if( typeof( oAction.triggerWithSettings ) == "function" && oSettings ){
			// Trigger execution of the action with the settings
			oAction.triggerWithSettings( oSettings );
		} else {
			// Trigger execution of the action
			oAction.trigger();
		}
	};
 
	/*********************************************************************/
	// Get the primary selection
	var oNode = Scene.getPrimarySelection();
	// If we do not 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 are done...
		return;
	}
 
	// Define the classname of the action we want to invoke the execution of
	var sAction = "DzZeroPoseAction"; //DzZeroShapeAction, DzZeroSelectedItemsAction
	// If the selected node is a skeleton or a bone
	if( inheritsType( oNode, ["DzSkeleton", "DzBone"] ) ){
		// Update the classname accordingly
		sAction = "DzZeroFigurePoseAction"; //DzZeroFigureShapeAction
	}
 
	// Trigger the action
	// RunSilent settings only supported for DzZero*Action since 4.21.1.14
	triggerAction( sAction, { "RunSilent" : true } );
 
// Finalize the function and invoke
})();