Erain 3D
--> FDT Pure coding comfort


Printing sandy scene

We were asked about this recently, so here goes: how do you print the result of three lines tutorial? By adding another three lines (but who's counting):

import flash.printing.PrintJob;
import sandy.core.Scene3D;
import sandy.core.scenegraph.*;
import sandy.primitive.*;
 
// code from 3 lines tutorial
var scene:Scene3D = new Scene3D ("scene", this, new Camera3D (550, 400), new Group ("root"));
scene.root.addChild (new Sphere ("sphere")); scene.render ();
 
// now let's print this out
var job:PrintJob = new PrintJob;
job.start (); try { job.addPage (scene.container); job.send (); } catch (all:*) {}

What happens here is that you create printer job, take scene container clip and ask user to confirm printing it out. Just in case the user changes his mind or there is a printer problem, we swallow all errors with try-catch construct.

Ok, it's simple, but if you print it out, the result is not nice, our sphere just sits in top left corner of the page. We will center it by retrieving page dimensions and specifying appropriate rectangle to print:

import flash.geom.Rectangle;
import flash.printing.PrintJob;
import sandy.core.Scene3D;
import sandy.core.scenegraph.*;
import sandy.primitive.*;
 
// code from 3 lines tutorial
var scene:Scene3D = new Scene3D ("scene", this, new Camera3D (550, 400), new Group ("root"));
scene.root.addChild (new Sphere ("sphere")); scene.render ();
 
// launch printer job
var job:PrintJob = new PrintJob; job.start ();
 
try
{
	// get our page dimensions
	var h:Number = job.pageHeight;
	var w:Number = job.pageWidth;
 
	// center container in this w x h area
	var dx:Number = (w - scene.camera.viewport.width) / 2;
	var dy:Number = (h - scene.camera.viewport.height) / 2;
	job.addPage (scene.container, new Rectangle (-dx, -dy, w -dx, h -dy));
 
	job.send ();
}
 
catch (all:*) {}

As you see it gets a bit more verbose now, but the sphere sits right in the middle of the page, as we wanted it. Note this works best in landscape format. Now when you printed it out, frame it and send us your picture with it. First one to do so shall get our e-hugs :)