User Tools

Site Tools


Render To Viewport

Summary

Below is an example demonstrating how to render to the viewport, via script.

API Areas of Interest

Example

Render_To_Viewport.dsa
// DAZ Studio version 4.5.0.53 filetype DAZ Script
 
// Set whether we want see debugging info
var bDebug = false;
 
// 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've got a renderer
if( oRenderer ){
	// Get the render options
	var oRenderOptions = oRenderMgr.getRenderOptions();
	var bCurrentFrame = oRenderOptions.isCurrentFrameRender;
	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.version > 0x04060404 ){
		// 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;
		}
	}
 
	// Initialize a variable for the file name
	var sFilename = "";
 
	// If the render options indicate we are rendering to file
	if( oRenderOptions.renderImgToId == oRenderOptions.DirectToFile ){
		// Create a file info object for easy file related operations
		var oFileInfo = new DzFileInfo( oRenderOptions.renderImgFilename );
 
		// 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 have a file name
	if( sFilename.isEmpty() ){
		// Set the file name to a temporary file
		sFilename = String("%1.png").arg( App.getTempRenderFilename() );
	}
 
	// If we're debugging
	if( bDebug ){
		print( "File:", sFilename );
	}
 
	// 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 size and position of the current aspect frame
	var rectFrame = o3DViewport.getAspectFrameRect();
	// The aspect rect is relative to the viewport,
	// but the render is relative to the aspect rect,
	// so we need to move the rect by the inverse offset
	rectFrame.moveBy( -rectFrame.x, -rectFrame.y );
 
	// Get the width, height and aspect of the frame
	var nFrameWidth = rectFrame.width;
	var nFrameHeight = rectFrame.height;
	var nFrameAspect = nFrameWidth / nFrameHeight;
 
	// If we're debugging
	if( bDebug ){
		print( "Frame:" );
		print( "X: ", rectFrame.x );
		print( "Y: ", rectFrame.y );
		print( "W: ", nFrameWidth );
		print( "H: ", nFrameHeight );	
		print( "A: ", nFrameAspect, ": 1" );
	}
 
	// Constrain the aspect
	oRenderOptions.isAspectConstrained = true;
 
	// Set the aspect
	if( nFrameWidth == nFrameHeight ){
		oRenderOptions.setAspectRatio( 1, 1 );
	} else if( nFrameWidth > nFrameHeight ){
		oRenderOptions.setAspectRatio( nFrameAspect, 1 );
	} else {
		oRenderOptions.setAspectRatio( 1, nFrameAspect );
	}
 
	// Update the image size for the render settings...
	// This causes the Render Settings > Dimensions to be updated
	oRenderOptions.imageSize = new Size( nFrameWidth, nFrameHeight );
 
	// We should only render the current frame to the viewport
	oRenderOptions.isCurrentFrameRender = true;
 
	// Get the time range of the render
	var oTimeRange = new DzTimeRange( oRenderOptions.startTime, oRenderOptions.endTime );
 
	// Create a 3d view render handler
	var oHandler = new Dz3DViewRenderHandler( oViewport, oTimeRange.start, sFilename );
 
	// If we created a valid render handler
	if( oHandler ){
		// Set the background color of the render to the
		// background color of the viewport
		oHandler.setBackColor( o3DViewport.background );
 
		// Set the area of the image to render
		oHandler.setCropWindow( rectFrame );
 
		// Set the handler to use the crop window
		oHandler.setUseCropWindow( true );
 
		// Get the active camera
		var oCamera = o3DViewport.getCamera();
 
		// Render, using our handler and options
		oRenderer.render( oHandler, oCamera, oRenderOptions );
 
		// Schedule the render handler for deletion, so we don't create a memory leak
		oHandler.deleteLater();
	// We didn't create a valid render handler
	} else {
		// Inform the user
		MessageBox.critical( qsTr("A valid render handler was not created."), qsTr("No Render Handler"), qsTr("&OK") );
	}
 
	// Restore the render options
	oRenderOptions.isCurrentFrameRender = bCurrentFrame;
	oRenderOptions.isAspectConstrained = bConstrain;
	oRenderOptions.imageSize = sizeImage;
	oRenderOptions.setAspectRatio( nAspectWidth, nAspectHeight );
 
// We didn't find a renderer with the class name we wanted
} else {
	// Inform the user
	MessageBox.critical( qsTr("An active renderer could not be found."), qsTr("No Renderer"), qsTr("&OK") );
}