User Tools

Site Tools


Post-Load Material 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 proxy properties on a node and corresponding properties on a surface.

See Also: Post-Load Material Proxy Create

API Areas of Interest

Example

Post_Load_Material_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 material by name
	function getMaterialByName( oNode, sName )
	{
		// If nothing is selected
		if( !oNode ){
			// We're done...
			return;
		}
 
		// Get the object for the node
		var oObject = oNode.getObject();
		// If we don't have an object
		if( !oObject ){
			// We're done...
			return;
		}
 
		// Get the current shape for the object
		var oShape = oObject.getCurrentShape();
		// If we don't have a shape
		if( !oShape ){
			// We're done...
			return;
		}
 
		return oShape.findMaterial( sName );
	};
 
	/*********************************************************************/
	// 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 oMaterial = getMaterialByName( oElement, sTargetName );
 
		// If we don't have a material
		if( !oMaterial ){
			// Construct the message
			sMessage = text( "Could not find the \"%1\" surface." ).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 oMaterialProperty, oProxyProperty;
		var sPropertyName, sProxyName;
 
		// 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 material
			oMaterialProperty = oMaterial.findProperty( sPropertyName );
 
			// If we didn't find the property
			if( !oMaterialProperty ){
				// Next!!
				continue;
			}
 
			// Construct the name of the proxy property
			sProxyName = String("%1 %2 Proxy").arg( oMaterial.name ).arg( sPropertyName );
 
			// Attempt to find the property on the proxy
			oProxyProperty = oElement.findProperty( sProxyName );
			// If we didn't find the property
			if( !oProxyProperty ){
				// Next!!
				continue;
			}
 
			// If the property is numeric
			if( oMaterialProperty.inherits( "DzNumericProperty" ) ){
				// Link the material property to the proxy property
				linkProperties( oMaterialProperty, oProxyProperty );
			}
		}
	}
 
// Finalize the function and invoke
})();