User Tools

Site Tools


Render To Asset Icon

Summary

Below is an example demonstrating how to render to the icon for the asset(s) selected in the Content Library pane, via script.

API Areas of Interest

Example

Render_To_Asset_Icon.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;
	};
 
	/*********************************************************************/
	// Get the pane manager
	var oPaneMgr = MainWindow.getPaneMgr();
	// Find the content library pane
	var oPane = oPaneMgr.findPane( "DzContentLibraryPane" );
	// If the pane was not found
	if( !oPane ){
		// We are done...
		return;
	}
 
	// Get the render manager
	var oRenderMgr = App.getRenderMgr();
 
	// Get the active software renderer;
	// this will not work for OpenGL [hardware] based renders
	var oRenderer = oRenderMgr.getActiveRenderer();
 
	// If we did not find a renderer
	if( !oRenderer ){
		// Inform the user
		MessageBox.critical( text( "An active renderer could not be found." ),
			text( "Resource Error" ), text( "&OK" ) );
 
		// We are done...
		return;
	}
 
	// Collect the render options; so we can restore them
	var oRenderOptions = oRenderMgr.getRenderOptions();
	var bCurrentFrame = oRenderOptions.isCurrentFrameRender;
	var nRenderImgId = oRenderOptions.renderImgToId;
	var sRenderFile = oRenderOptions.renderImgFilename;
	var bConstrain = oRenderOptions.isAspectConstrained;
	var sizeImage = oRenderOptions.imageSize;
	var nAspect = oRenderOptions.aspect;
	var nAspectWidth = 1;
	var nAspectHeight = 1;
	// If the app version is greater than 4.6.4.4
	if( App.version64 > 0x0004000600040004 ){
		// Get the aspect width and height
		nAspectWidth = oRenderOptions.aspectWidth;
		nAspectHeight = oRenderOptions.aspectHeight;
	// If the app version is 4.6.4.4 or earlier
	} else {
		// Get the aspect width and height based on image size
		if( sizeImage.width > sizeImage.height ){
			nAspectWidth = nAspect;		
		} else if( sizeImage.height > sizeImage.width ){
			nAspectHeight = nAspect;
		}
	}
 
	// Set the render options for the icon render
	oRenderOptions.isCurrentFrameRender = true;
	oRenderOptions.renderImgToId = DzRenderOptions.DirectToFile;
	oRenderOptions.isAspectConstrained = true;
	oRenderOptions.setAspectRatio( 1, 1 );
	oRenderOptions.imageSize = new Size( 91, 91 );
 
	// Declare working variables
	var oAsset, oFileInfo;
	var sFilePath, sIconPath;
 
	// Get the selected assets
	var aAssets = oPane.getSelectedAssets();
	// Iterate over the selected assets
	for( var i = 0; i < aAssets.length; i += 1 ){
		// Get the 'current' asset
		oAsset = aAssets[ i ];
		// Get the path of the asset
		sFilePath = oAsset.getAsLocalFile();
		// Create a file info object for easy file related operations
		oFileInfo = new DzFileInfo( sFilePath );
		// Contruct the path to the icon for the asset
		sIconPath = String("%1/%2.png").arg( oFileInfo.path() ).arg( oFileInfo.baseName() );
		// Set the path for the render
		oRenderOptions.renderImgFilename = sIconPath;
		// Render, using our modified options
		oRenderMgr.doRender( oRenderOptions );
		// Force the icon to update
		oAsset.forceImageReload();
	}
 
	// Restore the render options
	oRenderOptions.isCurrentFrameRender = bCurrentFrame;
	oRenderOptions.renderImgToId = nRenderImgId;
	oRenderOptions.renderImgFilename = sRenderFile;
	oRenderOptions.isAspectConstrained = bConstrain;
	oRenderOptions.imageSize = sizeImage;
	oRenderOptions.setAspectRatio( nAspectWidth, nAspectHeight );
 
// Finalize the function and invoke
})();