User Tools

Site Tools


Post-Load View Proxy Link Properties

Summary

Below is an example demonstrating how you can use a post-load script and settings embedded in the post-load data item to re-establish links between properties on a proxy node and corresponding properties on a view camera.

See Also: Post-Load View Proxy Create

API Areas of Interest

Example

Post_Load_View_Proxy_Link_Properties.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;
	};
 
	/*********************************************************************/
	// DzBasicCamera : A function for getting a view camera by name
	function getViewCameraByName( sName )
	{
		// Declare working variable
		var oViewCamera;
 
		// Get the viewport manager
		var oViewportMgr = MainWindow.getViewportMgr();
 
		// Iterate over the properties
		for( var i = 0, nViews = oViewportMgr.getNumViewCameras(); i < nViews; i += 1 ){
			// Get the 'current' view camera
			oViewCamera = oViewportMgr.getViewCamera( i );
			// If the name is the same as the one we want
			if( oViewCamera.name == sName ){
				// We're done...
				return oViewCamera;
			}
		}
 
		return undefined;
	};
 
	/*********************************************************************/
	// void : A function for creating a one to one link between properties
	function linkProperties( oSlaveProperty, oMasterProperty )
	{
		// Link the slave property to the master property
		oSlaveProperty.linkTo( oMasterProperty );
	};
 
	/*********************************************************************/
	// Declare working variables
	var sMessage;
 
	// Define common strings
	var sButton = text( "&OK" );
	var sTitle = text( "Data Item Info" );
 
	// 'DataItem' is a global transient variable, available when
	// this script is executed as a 'post load data item'
 
	// If we didn't find the 'DataItem' global transient;
	// this script was executed outside the context of a 'post load data item'	
	if( typeof( DataItem ) == "undefined" ){
		// Construct the message
		sMessage = text( "Oops... 'DataItem' doesn't exist." );
		// Display the message
		MessageBox.warning( sMessage, sTitle, sButton, "" );
		// We're done...
		return;
	// If we have the 'DataItem' global transient
	} else {
		// Get the owner of the data item
		var oElement = DataItem.getOwner();
 
		// If the data item is not of the type we expect
		if( !DataItem.inherits("DzSimpleElementScriptData") ){
			// Construct the message
			sMessage = text( "Oops... 'DataItem' is an incompatible type." );
			// Display the message
			MessageBox.warning( sMessage, sTitle, sButton, "" );
			// We're done...
			return;
		}
 
		// Get the settings for the data item
		var oSettings = DataItem.getSettings();
 
		// Get the name of the target
		var sTargetName = oSettings.getStringValue( "target", "" );
		// If the target name is empty
		if( sTargetName.isEmpty() ){
			// Construct the message
			sMessage = text( "Oops... missing the 'target' value." );
			// Display the message
			MessageBox.warning( sMessage, sTitle, sButton, "" );
			// We're done...
			return;
		}
 
		// Declare working variable
		var oCamera = getViewCameraByName( sTargetName );
 
		// If we don't have a view camera
		if( !oCamera ){
			// Construct the message
			sMessage = text( "Could not find the \"%1\" view camera." ).arg( sTargetName );
			// Display the message
			MessageBox.warning( sMessage, sTitle, sButton, "" );
			// We're done...
			return;
		}
 
		// Get the properties settings
		var oPropertySettings = oSettings.getSettingsValue( "properties" );
		// If the properties settings does not exist
		if( !oPropertySettings ){
			// Construct the message
			sMessage = text( "Oops... missing the 'properties' value." );
			// Display the message
			MessageBox.warning( sMessage, sTitle, sButton, "" );
			// We're done...
			return;
		}
 
		// Declare working variables
		var oCameraProperty, oProxyProperty;
		var sPropertyName;
 
		// Iterate over the properties
		for( var i = 0, nProps = oPropertySettings.getNumValues(); i < nProps; i += 1 ){
			// Get the name of the property
			sPropertyName = oPropertySettings.getKey( i );
 
			// Attempt to find the property on the camera
			oCameraProperty = oCamera.findProperty( sPropertyName );
			// If we didn't find the property
			if( !oCameraProperty ){
				// Next!!
				continue;
			}
 
			// Attempt to find the property on the proxy
			oProxyProperty = oElement.findProperty( sPropertyName );
			// If we didn't find the property
			if( !oProxyProperty ){
				// Next!!
				continue;
			}
 
			// If the property is numeric
			if( oCameraProperty.inherits( "DzNumericProperty" ) ){
				// Link the camera property to the proxy property
				linkProperties( oCameraProperty, oProxyProperty );
 
				// If the property is a transform
				if( oCameraProperty.getXYZInterest() != DzNumericProperty.NO_INTEREST ){
					// Linking transform properties causes issues when attempting to
					// animate; when keyframes exist on the proxy node's transforms,
					// viewport navigation appears to stop responding to user input
					// if the playhead is positioned beyond the last keyed frame as
					// a result of the proxy node's property being the 'master' of
					// the link (which forces the view camera's property to mimic
					// its value)
					oProxyProperty.setCanAnimate( false );
				}
			}
		}
	}
 
// Finalize the function and invoke
})();