User Tools

Site Tools


Render To RIB

Summary

Below is an example demonstrating how you can cause a scene to be “rendered” to a RIB file, via script.

API Areas of Interest

Example

Render_To_RIB.dsa
// DAZ Studio version 4.5 filetype DAZ Script
/*****************************
   Script Globals
*****************************/
var g_sToolName = "Render To RIB";
var g_sToolKey = "3Delight";
 
var g_oFileInfo = new DzFileInfo( getScriptFileName() );
var g_sScriptName = String( "%1.%2" ).arg( g_oFileInfo.baseName() ).arg( g_oFileInfo.extension() );
 
var s_bShiftPressed = App.modifierKeyState() & 0x02000000;
var s_bControlPressed = App.modifierKeyState() & 0x04000000;
var s_bAltPressed = App.modifierKeyState() & 0x08000000;
var s_bMetaPressed = App.modifierKeyState() & 0x10000000;
 
var g_sRenderRibKey = "RenderToRIB";
var g_sRibPathKey = "RIBFilename";
var g_sKeepShadowsKey = "RIBKeepShadows";
var g_sCollectAssetsKey = "RIBCollectAll";
var g_sProgressiveRenderKey = "ProgressiveRender";
 
var g_oTmpFile = new DzFileInfo( Scene.getFilename() );
 
var g_sDefaultRibPath = String( "%1/%2" ).arg( App.getTempPath() )
	.arg( g_oTmpFile.exists() ? String( "%1.rib" ).arg( g_oTmpFile.baseName() ) : "dzTest.rib" );
 
delete g_oTmpFile;
g_oTmpFile = undefined;
 
var g_oGui = new DsInterface;
var g_oActions = new DsActions;
var g_oSettingsHelper = new DzSettingsHelper;
var g_oArrayHelper = new DzArrayHelper;
var g_oStringHelper = new DzStringHelper;
 
var g_bDEBUG = false;
 
/***********************************************************************
***** DsInterface Prototype *****
***********************************************************************/
 
/*********************************************************************/
function DsInterface()
{
	// Get the current style
	this.oStyle = App.getStyle();
 
	// List of Boolean options
	this.aBoolNames = [];
	this.aBoolObjs = [];
 
	// List of String options
	this.aStrNames = [];
	this.aStrObjs = [];
 
	// List of ComboBox options
	this.aCmbNames = [];
	this.aCmbObjs = [];
	//
	this.wDlg, this.wTabStack;
	this.wOptionPage, this.wPrefsPage;
	this.wPathGB, this.wPrefsGB;
	this.wRibPath;
	this.wKeepShadows, this.wCollect;
	this.wHelpBtn, this.wAcceptBtn, this.wCancelBtn;
	this.wRcrdOnExec, this.wRcrdBtn, this.wRdBtn, this.wDfltBtn;
 
	// Functions for "Static" members
	this.__defineGetter__( "s_nWidgetWidth", function(){ return DsInterface.s_nWidgetWidth; } )
	this.__defineSetter__( "s_nWidgetWidth", function( v ){ DsInterface.s_nWidgetWidth = v; } )
	this.__defineGetter__( "s_nBtnHeight", function(){ return DsInterface.s_nBtnHeight; } )
	this.__defineSetter__( "s_nBtnHeight", function( v ){ DsInterface.s_nBtnHeight = v; } )
	this.__defineGetter__( "s_nBtnMinWidth", function(){ return DsInterface.s_nBtnMinWidth; } )
	this.__defineSetter__( "s_nBtnMinWidth", function( v ){ DsInterface.s_nBtnMinWidth = v; } )
	this.__defineGetter__( "s_nSpacing", function(){ return DsInterface.s_nSpacing; } )
	this.__defineSetter__( "s_nSpacing", function( v ){ DsInterface.s_nSpacing = v; } )
	this.__defineGetter__( "s_nMargin", function(){ return DsInterface.s_nMargin; } )
	this.__defineSetter__( "s_nMargin", function( v ){ DsInterface.s_nMargin = v; } )
};
 
/***********************************************************************/
DsInterface.superclass = Object;
 
// Initialize "Static" members
DsInterface.s_nMargin = g_oGui.oStyle.pixelMetric( "DZ_GeneralMargin" );
DsInterface.s_nSpacing = g_oGui.s_nMargin;
DsInterface.s_nBtnMinWidth = g_oGui.oStyle.pixelMetric( "DZ_ButtonMinWidth" );;
DsInterface.s_nBtnHeight = g_oGui.oStyle.pixelMetric( "DZ_ButtonHeight" );
DsInterface.s_nWidgetWidth = 290;
 
/*********************************************************************/
// void : Build the 'common' portion of the interface (used in hidden and unhidden modes)
DsInterface.prototype.doCommon = function()
{
	// --------------------- 
	// --- Main dialog
	// --------------------- 
	this.wDlg = new DzBasicDialog();
	this.wTabStack = new DzTabWidget( this.wDlg );
 
	// --------------------- 
	// --- Options Page
	// --------------------- 
	this.wOptionPage = new DzVGroupBox( this.wTabStack );
 
	// --- Path
	this.wPathGB = new DzVGroupBox( this.wOptionPage );
	this.wRibPath = new DzLineEdit( this.wPathGB );
	this.aStrObjs.push( this.wRibPath );
	this.aStrNames.push( g_sRibPathKey );
 
	// --- Shadow Maps
	this.wKeepShadows = new DzCheckBox( this.wOptionPage );
	this.aBoolObjs.push( this.wKeepShadows );
	this.aBoolNames.push( g_sKeepShadowsKey );
 
	// --- Collect
	this.wCollect = new DzCheckBox( this.wOptionPage );
	this.aBoolObjs.push( this.wCollect );
	this.aBoolNames.push( g_sCollectAssetsKey );
 
	// --------------------- 
	// --- Preferences Page
	// --------------------- 
	this.wPrefsPage = new DzVGroupBox( this.wTabStack );
	this.wPrefsGB = new DzVGroupBox( this.wPrefsPage );
 
	// --- Record on Execution
	this.wRcrdOnExec = new DzCheckBox( this.wPrefsGB );
	this.aBoolObjs.push( this.wRcrdOnExec );
	this.aBoolNames.push( "rcrdOnExec" );
 
	// Set the initial options
	this.setDefaults();
};
 
/*********************************************************************/
// void : Method for running with the dialog unhidden
DsInterface.prototype.doDialog = function()
{
	// Build the common portion of the dialog
	this.doCommon();
	// Get the Help Manager for "What's This?" and tool tips
	var oHelpMgr = App.getHelpMgr();
 
	// --------------------- 
	// --- Main dialog
	// --------------------- 
	this.wDlg.caption = g_sToolName;
	this.wDlg.whatsThis =
		qsTr( "<b>File : </b> %1<br>" ).arg( g_sScriptName ) +
		qsTr( "<b>Type : </b> %2<br>" ).arg( getScriptType() ) +
		qsTr( "<b>Size : </b> %3<br>" ).arg( g_oFileInfo.sizeStr() ) +
		qsTr( "<b>Version : </b> %4<br>" ).arg( getScriptVersionString() ) +
		qsTr( "<b>Created : </b> %5<br>" ).arg( g_oFileInfo.created()
			.toString( "dddd, MMMM d yyyy h:mm ap" ) ) +
		qsTr( "<b>Modified : </b> %6" ).arg( g_oFileInfo.lastModified()
			.toString( "dddd, MMMM d yyyy h:mm ap" ) );
 
	// --- Accept button
	this.wDlg.setAcceptButtonText( qsTr("&Render") );
 
	// --------------------- 
	// --- Options Page
	// --------------------- 
	this.wOptionPage.whatsThis = oHelpMgr.getHelpString( "PresetOptionTab" );
	this.wOptionPage.flat = true;
	this.wOptionPage.insideMargin = g_oGui.s_nMargin;
	this.wOptionPage.insideSpacing = g_oGui.s_nSpacing;
 
	// --- Path
	this.wPathGB.title = qsTr("Path:");
	this.wPathGB.margin = g_oGui.s_nMargin;
	this.wPathGB.spacing = g_oGui.s_nSpacing;
	this.wPathGB.whatsThis = "<b>" + this.wPathGB.title + "</b><br/>" +
		qsTr("This field allows you to specify the absolute path for the RIB " +
			"file you would like to render to.");
 
	// --- Keep Shadows
	this.wKeepShadows.text = qsTr("Keep Shadows");
	this.wKeepShadows.whatsThis = "<b>" + this.wKeepShadows.text + ":</b><br/>" +
		qsTr("This option allows you to choose if you would like to keep the " +
			"generated shadow map files.");
 
	// --- Collect
	this.wCollect.text = qsTr("Collect and Localize");
	this.wCollect.whatsThis = "<b>" + this.wCollect.text + ":</b><br/>" +
		qsTr("This option allows you to choose if you would like to collect all " +
			"of the associated files and localize their paths in the RIB.");
 
	// Add the 'Options Page' widget to the tab stack
	this.wTabStack.addTab( this.wOptionPage, qsTr("Options") );
 
	// --------------------- 
	// --- Preferences Page
	// --------------------- 
	this.wPrefsPage.whatsThis = oHelpMgr.getHelpString( "PresetPrefTab" );
	this.wPrefsPage.flat = true;
	this.wPrefsPage.insideMargin = g_oGui.s_nMargin;
	this.wPrefsPage.insideSpacing = g_oGui.s_nSpacing;
	this.wPrefsGB.margin = g_oGui.s_nMargin;
	this.wPrefsGB.spacing = g_oGui.s_nSpacing;
 
	// --- Record on Execution CheckBox
	this.wRcrdOnExec.text = qsTr("Set Preferred Options on Accept");
	this.wRcrdOnExec.whatsThis = oHelpMgr.getHelpString( "PresetPrefOnAccept" );
 
	// --- Set Preferred Options Button
	this.wRcrdBtn = new DzPushButton( this.wPrefsGB );
	this.wRcrdBtn.text = qsTr("&Set Preferred Options");
	this.wRcrdBtn.whatsThis = oHelpMgr.getHelpString( "PresetSetPref" );
	connect( this.wRcrdBtn, "pressed()", this, "setOptions" );
 
	// --- Read Preferred Options Button
	this.wRdBtn = new DzPushButton( this.wPrefsGB );
	this.wRdBtn.text = qsTr("&Read Preferred Options");
	this.wRdBtn.whatsThis = oHelpMgr.getHelpString( "PresetReadPref" );
	connect( this.wRdBtn, "pressed()", this, "getOptions" );
 
	// --- Restore Default Options Button
	this.wDfltBtn = new DzPushButton( this.wPrefsGB );
	this.wDfltBtn.text = qsTr("Restore &Default Options");
	this.wDfltBtn.whatsThis = oHelpMgr.getHelpString( "PresetRestoreDef" );
	connect( this.wDfltBtn, "pressed()", this, "setDefaults" );
 
	// --- Notes
	var wNotesGB = new DzVGroupBox( this.wPrefsPage );
	wNotesGB.title = qsTr("Notes :");
	wNotesGB.margin = g_oGui.s_nMargin;
	wNotesGB.spacing = g_oGui.s_nSpacing;
	wNotesGB.minWidth = g_oGui.s_nWidgetWidth;
 
	var wKeyLbl = new DzLabel( wNotesGB );
	wKeyLbl.text = oHelpMgr.getHelpString( "PresetNotes" );
 
	// Add the 'Preferences Page' widget to the tab stack
	this.wTabStack.addTab( this.wPrefsPage, qsTr("Preferences") );
 
	//
	this.wDlg.addWidget( this.wTabStack );
 
	// --------------------- 
	// --- Polish
	// --------------------- 
	this.wDlg.maxWidth = this.wDlg.minWidth;
	this.wDlg.maxHeight = this.wDlg.minHeight;
	// Get the users prefered options
	this.getOptions();
	// If the dialog is not canceled
	if( this.wDlg.exec() ){
		// If the 'Record on Execute' checkbox is checked, record the current options
		if( this.wRcrdOnExec.checked ){
			this.setOptions();
		}
		// Do... whatever it is that we do
		g_oActions.begin();
	}
};
 
/*********************************************************************/
// void : Method for running with the dialog hidden
DsInterface.prototype.doNoDialog = function()
{
	// Build the common portion of the dialog
	this.doCommon();
	// If the user was holding the shift modifier,
	// update the options from the ones recorded
	if( s_bShiftPressed ){
		this.getOptions();
	}
	// Do... whatever it is that we do
	g_oActions.begin();
};
 
/*********************************************************************/
// void : Method for setting initial option values
DsInterface.prototype.setDefaults = function()
{
	// --- String Options
	this.wRibPath.text = g_sDefaultRibPath;
	// --- Boolean options
	this.wKeepShadows.checked = false;
	this.wCollect.checked = false;
	this.wRcrdOnExec.checked = false;
};
 
/*********************************************************************/
// void : Method for retrieving options
DsInterface.prototype.getOptions = function()
{
	var i = 0;
 
	// Iterate over all boolean option objects
	for( i = 0; i < this.aBoolObjs.length; i += 1 ){
		// Set the current boolean option to the recorded value
		this.aBoolObjs[ i ].checked = g_oSettingsHelper.get( g_oStringHelper.stripSpaces( g_sToolName ), this.aBoolNames[ i ], this.aBoolObjs[ i ].checked );
	}
 
	// Iterate over all string option objects
	for( i = 0; i < this.aStrObjs.length; i += 1 ){
		// Set the current string option to the recorded value
		this.aStrObjs[ i ].text = g_oSettingsHelper.get( g_oStringHelper.stripSpaces( g_sToolName ), this.aStrNames[ i ], this.aStrObjs[ i ].text );
	}
 
	// Iterate over all combobox option objects
	for( i = 0; i < this.aCmbObjs.length; i += 1 ){
		// Get the recorded currentText value
		var tStr = g_oSettingsHelper.get( g_oStringHelper.stripSpaces( g_sToolName ), this.aCmbNames[ i ], this.aCmbObjs[ i ].currentText );
		// Set the currentItem string option to the recorded value
		for( var j = 0; j < this.aCmbObjs[ i ].count; j += 1 ){
			if( this.aCmbObjs[ i ].text( j ) == tStr ){
				this.aCmbObjs[ i ].currentItem = j;
				break;
			}
		}
	}
};
 
/*********************************************************************/
// void : Method for recording options
DsInterface.prototype.setOptions = function()
{
	var i = 0;
 
	// Iterate over all boolean option objects
	for( i = 0; i < this.aBoolObjs.length; i += 1 ){
		// Record the current boolean option
		g_oSettingsHelper.set( g_oStringHelper.stripSpaces( g_sToolName ), this.aBoolNames[ i ], this.aBoolObjs[ i ].checked );
	}
 
	// Iterate over all string option objects
	for( i = 0; i < this.aStrObjs.length; i += 1 ){
		// Record the current string option
		g_oSettingsHelper.set( g_oStringHelper.stripSpaces( g_sToolName ), this.aStrNames[ i ], this.aStrObjs[ i ].text );
	}
 
	// Iterate over all combobox option objects
	for( i = 0; i < this.aCmbObjs.length; i += 1 ){
		// Record the currentText string option
		g_oSettingsHelper.set( g_oStringHelper.stripSpaces( g_sToolName ), this.aCmbNames[ i ], this.aCmbObjs[ i ].currentText );
	}
};
 
/*********************************************************************/
// String : Method for retrieving the path
DsInterface.prototype.getPath = function()
{
	return this.wRibPath.text;
};
 
/*********************************************************************/
// Boolean : Method for retrieving the shadows option
DsInterface.prototype.getKeepShadows = function()
{
	return this.wKeepShadows.checked;
};
 
/*********************************************************************/
// Boolean : Method for retrieving the collect option
DsInterface.prototype.getCollect = function()
{
	return this.wCollect.checked;
};
 
/*********************************************************************/
// Boolean : Method for retrieving the record option
DsInterface.prototype.getRecord = function()
{
	return this.wRcrdOnExec.checked;
};
 
/***********************************************************************
***** DsActions Prototype *****
***********************************************************************/
 
/*********************************************************************/
function DsActions()
{
	// Instance Members	
	this.sTmpRibPath;
	this.bTmpShadows, this.bTmpCollect;
	this.oProcess;
};
 
/***********************************************************************/
DsActions.superclass = Object;
 
 
/*********************************************************************/
// void : Method to process standard output
DsActions.prototype.readStdout = function()
{
	print( this.oProcess.readStdout() );
};
 
/*********************************************************************/
// void : Method to process standard error
DsActions.prototype.readStderr = function()
{
	print( this.oProcess.readStderr() );
};
 
/*********************************************************************/
// void : Method to do whatever it is we do
DsActions.prototype.begin = function()
{
	// Get the render manager
	var oRenderMgr = App.getRenderMgr();
	// Find the 3Delight renderer
	var oRenderer = oRenderMgr.findRenderer( "DzDelightRenderer" );
	// If we did not find the render we want
	if( !oRenderer ){
		// There is no point in continuing... exit early.
		return;
	}
 
	// Set the active renderer to 3Delight
	oRenderMgr.setActiveRenderer( oRenderer );
 
	// Get the user options
	var sRibPath = g_oGui.getPath();
	var bKeepShadows = g_oGui.getKeepShadows();
	var bCollect = g_oGui.getCollect();
	var bRecord = g_oGui.getRecord();
 
	// Create new file info for easy path operations
	var oFile = new DzFileInfo( sRibPath );
 
	// If the user did not record preferred settings
	if( !bRecord ){
		// Record any previous values
		this.sTmpRibPath = g_oSettingsHelper.get( g_sToolKey, g_sRibPathKey, g_sDefaultRibPath );
		this.bTmpShadows = g_oSettingsHelper.get( g_sToolKey, g_sKeepShadowsKey, false );
		this.bTmpCollect = g_oSettingsHelper.get( g_sToolKey, g_sCollectAssetsKey, false );
 
		// Temporarily set the flags
		g_oSettingsHelper.set( g_sToolKey, g_sRibPathKey, sRibPath );
		g_oSettingsHelper.set( g_sToolKey, g_sKeepShadowsKey, bKeepShadows );
		g_oSettingsHelper.set( g_sToolKey, g_sCollectAssetsKey, bCollect );
	}
 
	// Record whether we are rendering to rib
	var bRenderRib = g_oSettingsHelper.get( g_sToolKey, g_sRenderRibKey, false );
 
	// Set the flag used by DzDelightRenderer
	g_oSettingsHelper.set( g_sToolKey, g_sRenderRibKey, true );
 
	// Get the current render options
	var oRenderOptions = oRenderMgr.getRenderOptions();
 
	// Store the original values of the options we'll be changing
	var nRenderType = oRenderOptions.renderType;
	var nRenderImgToId = oRenderOptions.renderImgToId;
	var sRenderImgFilename = oRenderOptions.renderImgFilename;
	var bShowPreview = oRenderOptions.showPreview;
 
	// Adjust the render options
	oRenderOptions.renderType = oRenderOptions.Software;
	oRenderOptions.renderImgToId = oRenderOptions.DirectToFile;
	oRenderOptions.renderImgFilename = sRibPath + ".png";
	oRenderOptions.showPreview = false;
 
	// Do the "render" using the adjusted options
	oRenderMgr.doRender( oRenderOptions );
 
	// Unset the flag used by DzDelightRenderer
	g_oSettingsHelper.set( g_sToolKey, g_sRenderRibKey, bRenderRib );
 
	// If the user did not record preferred settings
	if( !bRecord ){
		// Restore to the previous values
		g_oSettingsHelper.set( g_sToolKey, g_sRibPathKey, this.sTmpRibPath );
		g_oSettingsHelper.set( g_sToolKey, g_sKeepShadowsKey, this.bTmpShadows );
		g_oSettingsHelper.set( g_sToolKey, g_sCollectAssetsKey, this.bTmpCollect );
	}
 
	// Reset the options from the last recorded options
	oRenderOptions.renderType = nRenderType;
	oRenderOptions.renderImgToId = nRenderImgToId;
	oRenderOptions.renderImgFilename = sRenderImgFilename;
	oRenderOptions.showPreview = bShowPreview;
 
	// Cause the options to be recorded and reloaded
	oRenderOptions.applyChanges();
	oRenderOptions.resetOptions();
 
	// Let the user know we are busy
	setBusyCursor();
 
	// If we are collecting assets and the path is valid
	if( bCollect && oFile.exists() ){
		// Create a new dir
		var oDir = new DzDir( oFile.path() );
 
		// Construct the name of a sub-directory to collect into
		var sCollectDir = String( "%1_collected" ).arg( oFile.baseName() );
 
		// If the sub-directory is successfully created
		if( oDir.mkdir( sCollectDir ) ){
			// Change to the created directory
			oDir.cd( String( "%1/%2" ).arg( oDir.absPath() ).arg( sCollectDir ) );
		}
 
		// Create a new process
		this.oProcess = new DzProcess();
 
		// Set the communication flags for the process
		this.oProcess.communication = DzProcess.Stdin | DzProcess.Stdout | DzProcess.Stderr;
 
		// Set the working directory
		this.oProcess.workingDirectory = App.getUtilitiesPath();
 
		// Connect to recieve standard output
		connect( this.oProcess, "readyReadStdout()", this, "readStdout" );
 
		// Connect to recieve standard error
		connect( this.oProcess, "readyReadStderr()", this, "readStderr" );
 
		// Create an array of arguments
		var aProcessArgs = [
			String( "%1/%2" ).arg( this.oProcess.workingDirectory ).arg( "ribdepends" ),
			"-noinit",
			"-package",
			oDir.absPath(),
			oFile.absFileName()
		];
 
		// Assign the arguments
		this.oProcess.arguments = aProcessArgs;
 
		// If starting the process fails
		if( !this.oProcess.start() ){
			// Inform the user
			MessageBox.critical( qsTr("Could not start the process."), qsTr("Fatal Error"), qsTr("&OK") );
		}
 
		// Wait for the process to exit;
		// Otherwise the script will complete and in cleaning up, the process object
		// will be destroyed before said process can actually do anything
		while( this.oProcess.running ){
			// Allow the application to update
			processEvents();
		}
	}
 
	// Let the user know we are done
	clearBusyCursor();
};
 
/*********************************************************************/
// If the user was holding the CTRL modifier key launch the dialog, otherwise run without the dialog
s_bControlPressed ? g_oGui.doDialog() : g_oGui.doNoDialog();