User Tools

Site Tools


Render DrawStyle

Summary

Below is an example demonstrating how to perform a hardware render using a named DrawStyle; i.e., “Hidden Line.”, via script.

API Areas of Interest

Example

Render_DrawStyle.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();
	}
 
	/*********************************************************************/
	// void : 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;
	};
 
	/*********************************************************************/
	// void : 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(" ") );
	};
 
	/*********************************************************************/
	// Get the viewport manager
	var oViewportMgr = MainWindow.getViewportMgr()
 
	// Get the active viewport
	var oViewport = oViewportMgr.getActiveViewport();
 
	// Get the 3D viewport
	var o3DViewport = oViewport.get3DViewport();
 
	// Get the current DrawStyle
	var sDrawStyle = o3DViewport.getUserDrawStyle();
 
	// Set the DrawStyle to the one we want
	o3DViewport.setUserDrawStyle( "Hidden Line" );
 
	// Get the current background color
	var clrBackground = o3DViewport.background;
 
	// Get the render manager
	var oRenderMgr = App.getRenderMgr();
 
	// Get the render options
	var oRenderOptions = oRenderMgr.getRenderOptions();
	var nRenderImgToId = oRenderOptions.renderImgToId;
	var sRenderImgFilename = oRenderOptions.renderImgFilename;
 
	// Initialize a variable for the file name
	var sFilename = "";
 
	// Create a file info object for easy file related operations
	var oFileInfo = new DzFileInfo( sRenderImgFilename );
 
	// If we have a base name
	if( !oFileInfo.baseName().isEmpty() ){
		// Set the file name to the one specified by render options
		sFilename = oRenderOptions.renderImgFilename;
	}
 
	// If we do not have a file name
	if( sFilename.isEmpty() ){
		// Set the file name to a temporary file
		sFilename = String("%1.png").arg( App.getTempRenderFilename() );
	}
 
	// Provide feedback
	debug( "File:", sFilename );
 
	// Get the current render type
	var nRenderType = oRenderOptions.renderType;
 
	// Set the options for rendering to file
	oRenderOptions.renderImgToId = DzRenderOptions.DirectToFile;
	oRenderOptions.renderImgFilename = sFilename;
 
	// Set the render type to 'Basic OpenGL'; aka 'ScreenShot'
	oRenderOptions.renderType = DzRenderOptions.ScreenShot;
 
	// Create a mask color
	var clrMask = new Color( 0, 255, 0 );
 
	// Set the background color to the mask color
	o3DViewport.background = clrMask;
 
	// Perform the render using our options
	oRenderMgr.doRender( oRenderOptions );
 
	// If the DrawStyle was a valid UserDrawStyle
	if( !sDrawStyle.isEmpty() ){
		// Restore the DrawStyle
		o3DViewport.setUserDrawStyle( sDrawStyle );
	}
 
	// Restore the background color
	o3DViewport.background = clrBackground;
 
	// Restore render options
	oRenderOptions.renderImgToId = nRenderImgToId;
	oRenderOptions.renderImgFilename = sRenderImgFilename;
	oRenderOptions.renderType = nRenderType;
 
	// If the application version is 4.10.0.119 or newer
	if( App.version64 >= 0x0004000a00000077 ){
		// Create an image with the filename we want
		var oImage = new Image( sFilename );
		// Set the alpha for the image to a color mask
		oImage.setAlphaFromImage( oImage.getColorMask( clrMask, true, 10 ) );
		// Save the image
		oImage.save( sFilename );
	}
 
// Finalize the function and invoke
})();