Export As: [ PDF | ODT ]

Render To Viewport

This example is dependent on functionality provided by DAZ Studio 4.5.x.

Summary

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

API Areas of Interest

Example

Click the name of the example below to save it as a file.

Render_To_Viewport.dsa
// DAZ Studio version 4.5.0.53 filetype DAZ Script
 
// Get the render manager
var oRenderMgr = App.getRenderMgr();
 
// Define the class name of the renderer we want to use
var sClassName = "DzDelightRenderer";
 
// Find the renderer we want to use
var oRenderer = oRenderMgr.findRenderer( sClassName );
 
// If we've found the renderer with the class name we wanted
if( oRenderer ){
	// Define whether we are rendering to the viewport
	var bRenderToViewport = true;
 
	// Define whether we are cropping the render
	var bCropRender = true;
 
	// Get the render options
	var oRenderOptions = oRenderMgr.getRenderOptions();
 
	// Define the path of the file to render to
	var sFilename = "";
	var oFileInfo = new DzFileInfo( oRenderOptions.renderImgFilename );
 
	if( oRenderOptions.renderImgToId == oRenderOptions.DirectToFile ){
		if( !oFileInfo.baseName().isEmpty() ){
			sFilename = oRenderOptions.renderImgFilename;
		}
	}
 
	if( sFilename.isEmpty() ){
		sFilename = String("%1.%2")
			.arg( App.getTempRenderFilename() )
			.arg( oFileInfo.extension() );
	}
 
	print( "\nFile:" );
	print( 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, relative to the viewport,
	// of the current aspect frame
	var rectFrame = o3DViewport.getAspectFrameRect();
 
	// Get the width, height and aspect of the frame
	var nFrameWidth = rectFrame.width;
	var nFrameHeight = rectFrame.height;
	var nFrameAspect = nFrameWidth / nFrameHeight;
 
	// Get the size of the 3d viewport
	var rectViewport = new Rect( 0, 0, o3DViewport.width, o3DViewport.height );
 
	// Get the width, height and aspect of the viewport
	var nViewportWidth = rectViewport.width;
	var nViewportHeight = rectViewport.height;
	var nViewportAspect = nViewportWidth / nViewportHeight;
 
	// Initialize the position of the render
	var nPosX = 0;
	var nPosY = 0;
 
	// If we are rendering to the viewport and cropping
	if( bRenderToViewport && bCropRender ){
		// Define the area of the viewport to render...
		if( nViewportAspect > nFrameAspect ){
			nFrameHeight = nFrameWidth / nFrameAspect;
		} else {
			nFrameWidth = nFrameHeight * nFrameAspect;
		}
 
		// Set the position
		nPosX = rectFrame.x;
		nPosY = rectFrame.y;
	// If we are rendering to the viewport, but not cropping
	} else if ( bRenderToViewport ){
		// Set the position
		nPosX = -rectFrame.x;
		nPosY = -rectFrame.y;
 
		// Use the viewport size
		nFrameWidth = nViewportWidth;
		nFrameHeight = nViewportHeight;
	}
 
	// Set the render rect
	var rectRender = new Rect( nPosX, nPosY, nFrameWidth, nFrameHeight );
 
	oRenderOptions.isAspectConstrained = true;
 
	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 );
 
	print( "\nViewport:" );
	print( String("X: %1").arg( rectViewport.x ) );
	print( String("Y: %1").arg( rectViewport.y ) );
	print( String("W: %1").arg( rectViewport.width ) );
	print( String("H: %1").arg( rectViewport.height ) );
 
	print( "\nRender:" );
	print( String("X: %1").arg( rectRender.x ) );
	print( String("Y: %1").arg( rectRender.y ) );
	print( String("W: %1").arg( rectRender.width ) );
	print( String("H: %1").arg( rectRender.height ) );
 
	// Modify the render options...
	oRenderOptions.isCurrentFrameRender = true;
 
	// Get the time range of the render
	var oTimeRange = new DzTimeRange( oRenderOptions.startTime, oRenderOptions.endTime );
 
	// If we are rendering to the viewport
	if( bRenderToViewport ){
		// Create a 3d view render handler
		oHandler = new Dz3DViewRenderHandler( oViewport, oTimeRange.start, sFilename );
	// If we are NOT rendering to the viewport
	} else {
		// Create a view render handler
		oHandler = new DzViewRenderHandler( oRenderOptions.imageSize, oTimeRange.start, sFilename, true );
	}
 
	// 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 );//new Color(255, 0, 0)
 
		// Set the area of the image to render
		oHandler.setCropWindow( rectRender );
 
		// Set the handler to use the crop window
		oHandler.setUseCropWindow( bCropRender );
 
		// Get the active camera
		var oCamera = o3DViewport.getCamera();
 
		// Set the option for rendering to the viewport...
		// This causes the aspect frame rect to be updated to
		// the size of the 3d viewport itself
		oRenderOptions.renderViewport = bRenderToViewport;
 
		// 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();
 
		// If we are rendering to the viewport and cropping
		if( bRenderToViewport && bCropRender ){
			// Load the rendered image
			var oImage = new Image( sFilename );
			// Create a new image from the cropped area
			oImage = oImage.copy( rectRender );
			// Save over the rendered image
			oImage.save( sFilename );
		}
	// We didn't create a valid render handler
	} else {
		// Inform the user
		MessageBox.critical( "A valid render handler was not created.", "No Render Handler", "&OK" );
	}
// We didn't find a renderer with the class name we wanted
} else {
	// Inform the user
	MessageBox.critical( String("A renderer with the class name \"%1\" " +
		"could not be found.").arg( sClassName ), "No Renderer", "&OK" );
}