User Tools

Site Tools


Scripted Renderer Save Preset

Summary

Below is an example demonstrating how you can generate a script based “Preset” that activates DzScriptedRenderer, sets the definition file and sets the values of properties associated with that definition, via script.

API Areas of Interest

Example

Scripted_Renderer_Save_Preset.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Initialize 'static' variables that hold modifier key state
	var s_bShiftPressed = false;
	var s_bControlPressed = false;
	var s_bAltPressed = false;
	var s_bMetaPressed = false;
 
	// If the "Action" global transient is defined, and its the correct type
	if( typeof( Action ) != "undefined" && Action.inherits( "DzScriptAction" ) ){
		// If the current key sequence for the action is not pressed
		if( !App.isKeySequenceDown( Action.shortcut ) ){
			updateModifierKeyState();
		}
	// If the "Action" global transient is not defined
	} else if( typeof( Action ) == "undefined" ) {
		updateModifierKeyState();
	}
 
	/*********************************************************************/
	// String : A function for updating the keyboard modifier state
	function updateModifierKeyState()
	{
		// Get the current modifier key state
		var nModifierState = App.modifierKeyState();
		// Update variables that hold modifier key state
		s_bShiftPressed = (nModifierState & 0x02000000) != 0;
		s_bControlPressed = (nModifierState & 0x04000000) != 0;
		s_bAltPressed = (nModifierState & 0x08000000) != 0;
		s_bMetaPressed = (nModifierState & 0x10000000) != 0;
	};
 
	/*********************************************************************/
	// String : A function for printing only if debugging
	function debug()
	{
		// If we are not debugging
		if( !s_bAltPressed ){
			// We are done...
			return;
		}
 
		// Convert the arguments object into an array
		var aArguments = [].slice.call( arguments );
 
		// Print the array
		print( aArguments.join(" ") );
	};
 
	/*********************************************************************/
	// 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;
	};
 
	/*********************************************************************/
	// Define message components
	var sTitle = text( "Configuration Error" );
	var sButton = text( "&OK" );
	var sMessage;
 
	// Get the render manager
	var oRenderMgr = App.getRenderMgr();
	// Find the scripted renderer
	var oRenderer = oRenderMgr.findRenderer( "DzScriptedRenderer" );
	// If the renderer could not be found
	if( !oRenderer ){
		// Construct a message
		sMessage = text( "The \"%1\" renderer could not be found." ).arg( oRenderer.getName() );
		// Notify the user
		MessageBox.information( sMessage, sTitle, sButton );
 
		// We are done...
		return;
	}
 
	// Get the active renderer
	var oActiveRenderer = oRenderMgr.getActiveRenderer();
	// If the renderer we want is inactive
	if( oActiveRenderer != oRenderer ){
		// Construct a message
		sMessage = text( "\"%1\" must be the active renderer to continue." )
				.arg( oRenderer.getName() );
		// Notify the user
		MessageBox.information( sMessage, sTitle, sButton );
 
		// We are done...
		return;
	}
 
	// Get the element that holds all of the properties for the render pass
	var oElement = oRenderer.getPropertyHolder();
 
	// Create a new script object
	var oScript = new DzScript();
 
	// Get the base path for scripted renderer definition files
	var sDefinitionBasePath = String("%1/Scripted Renderer/").arg( App.getResourcesPath() );
	// Construct the full path for the current definition file
	var sDefinitionPath = String("%1%2").arg( sDefinitionBasePath ).arg( oRenderer.getDefinitionFile() );
	// Create a file info object for path operations
	var oFileInfo = new DzFileInfo( sDefinitionPath );
	// Get the full directory path
	var sRelDefinitionPath = oFileInfo.path();
	// Get the relative directory path; by removing the base path from the full path
	sRelDefinitionPath = sRelDefinitionPath.slice( sDefinitionBasePath.length );
 
	// Begin adding lines to the "preset" script; the logic of the script is similar to this one, the biggest
	// difference being that we are not collecting data rather we are applying data we've already collected
	oScript.addLine( "(function(){" );
 
	oScript.addLine( "function text( sText )", 1 );
	oScript.addLine( "{", 1 );
	oScript.addLine( "if( typeof( qsTr ) != \"undefined\" ){", 2 );
	oScript.addLine( "return qsTr( sText );", 3 );
	oScript.addLine( "}", 2 );
	oScript.addLine( "return sText;", 2 );
	oScript.addLine( "};", 1 );
 
	oScript.addLine( "var sDefinitionBasePath = String(\"%1/Scripted Renderer/\").arg( App.getResourcesPath() );", 1 );
	oScript.addLine( String("var sRelDefinitionPath = \"%1/%2\";").arg( sRelDefinitionPath ).arg( oFileInfo.baseName() ), 1 );
	oScript.addLine( "var sDefinitionPath = String(\"%1%2\").arg( sDefinitionBasePath ).arg( sRelDefinitionPath );", 1 );
	oScript.addLine( "var sMessage = sDefinitionPath;", 1 );
	oScript.addLine( "var oScript = new DzScript();", 1 );
	oScript.addLine( "sDefinitionPath = oScript.getScriptFile( sDefinitionPath );", 1 );
	oScript.addLine( "var oFileInfo = new DzFileInfo( sDefinitionPath );", 1 );
	oScript.addLine( "if( !oFileInfo.exists() ){", 1 );
	oScript.addLine( "MessageBox.warning( text(\"%1.ds(a|b|e)\").arg( sMessage ), text(\"File Not Found\"), text(\"&OK\"), \"\");", 2 );
	oScript.addLine( "return;", 2 );
	oScript.addLine( "}", 1 );
 
	oScript.addLine( "var oRenderMgr = App.getRenderMgr();", 1 );
	oScript.addLine( "var sRenderer = \"DzScriptedRenderer\";", 1 );
	oScript.addLine( "var oRenderer = oRenderMgr.findRenderer( sRenderer );", 1 );
	oScript.addLine( "if( !oRenderer ){", 1 );
	oScript.addLine( "MessageBox.warning( text(\"\\\"%1\\\" could not be found.\").arg( sRenderer ), text(\"Resource Error\"), text(\"&OK\"), \"\");", 2 );
	oScript.addLine( "return;", 2 );
	oScript.addLine( "}", 1 );
 
	oScript.addLine( "oRenderMgr.setActiveRenderer( oRenderer );", 1 );
	oScript.addLine( "sRelDefinitionPath = sDefinitionPath.slice( sDefinitionBasePath.length );", 1 );
	oScript.addLine( "oRenderer.setDefinitionFile( sRelDefinitionPath );", 1 );
	oScript.addLine( "oRenderer.defintionFileChanged();", 1 );
	oScript.addLine( "var oElement = oRenderer.getPropertyHolder();", 1 );
 
	// Declare working variables
	var oProperty;
	var vValue;
	var bAdd;
 
	// Get the properties
	var aProperties = oElement.getPropertyList();
 
	// Iterate over the properties
	for( var i = 0; i < aProperties.length; i += 1 ){
		// Initialize
		bAdd = false;
		// Get the 'current' property
		oProperty = aProperties[i];
 
		// If the property is DzFloatProperty or DzIntProperty derived
		if( inheritsType( oProperty, ["DzFloatProperty", "DzIntProperty"] ) ){
			// Get the raw value of the property
			vValue = oProperty.getRawValue();
			// Indicate that we should add this property
			bAdd = true;
		}
		// If the property is DzImageProperty or DzStringProperty derived
		else if( inheritsType( oProperty, ["DzImageProperty", "DzStringProperty"] ) ){
			// Get the value of the property
			vValue = oProperty.getValue();
			// Indicate that we should add this property
			bAdd = true;
		}
 
		// If we are adding the property
		if( bAdd ){
			// Add the lines for finding and setting the property
			oScript.addLine( String("oProperty = oElement.findProperty( \"%1\" )").arg( oProperty.name ), 1 );
			oScript.addLine( "if( oProperty ){", 1 );
			switch( typeof vValue ){
				case "number":
					oScript.addLine( String("oProperty.setValue( %1 );").arg( vValue ), 2 );
					break;
				case "string":
					oScript.addLine( String("oProperty.setValue( \"%1\" );").arg( vValue ), 2 );
					break;
			}
			oScript.addLine( "}", 1 );
		}
	}
 
	// Finish adding lines to the script
	oScript.addLine( "})();" );
 
	// Get whether or not to debug
	var bDebug = s_bAltPressed;
	// If we are debugging
	if( bDebug ){
		// Inspect the generated code
		debug( oScript.getCode() );
		// We are done...
		return;
	}
 
	// Build the extensions filter forthe dialog
	var sExtensions = "DAZ Script 2 Pain Text (*.dsa);DAZ Script 2 Binary (*.dsb)";
	// Prompt the user for a file name to save to
	var sFilePath = FileDialog.doFileDialog( false, text( "Save As" ), App.getLoadSavePath(), sExtensions );
	// If the user canceled
	if( sFilePath.isEmpty() ){
		// We are done...
		return;
	}
 
	// Create a file info object for path operations
	oFileInfo = new DzFileInfo( sFilePath );
	// Update the last load/save path with the path the user chose
	App.setLoadSavePath( oFileInfo.path() );
 
	// Save the script to file
	oScript.saveToFile( sFilePath, DzScript.UseExtension, "Scripted Render Settings Preset" );
 
	// Get the content manager
	var oContentMgr = App.getContentMgr();
	// Notify the content manager that we created the file
	oContentMgr.fileCreated( sFilePath );
 
	// Get the asset manager
	var oAssetMgr = App.getAssetMgr();
 
	// Define the content type
	var sContentType = "Preset/Render-Settings";
	// Define the category
	var sCategory = "/Default/Saved Files/Render Settings";
 
	// If the application version is 4.9.0.17 or newer
	if( App.version64 >= 0x0004000900000011 ){
		// Set metadata for the file
		oAssetMgr.setFileMetadata( sFilePath, sContentType, "", sCategory, undefined, "" );
	// If the application version is older
	} else {
		// Create a database object so that we have access to its functions
		var oContentInstance = new DzDBContentInstanceTable();
		// Create the instance record for this file; creates the content record if needed
		oContentInstance = oContentInstance.createFromPath( sFilePath );
		// If we have a database object; the CMS is running
		if( oContentInstance ){
			// Get the content type id for the type of file we've saved
			var nTypeId = oAssetMgr.findTypeID( sContentType );
 
			// Get the content record that this is an instance of
			var oContent = oContentInstance.getContent();
			// Set the type on the file
			oContent.contentType = nTypeId;
			// Add the file to a category
			oContent.addToCategory( sCategory );
			// Notify everything that is listening for updates
			oContent.update();
		}
	}
 
// Finalize the function and invoke
})();