Importing Cinema4D model into Papervision3D 2.0

I would like to show you have you can use a custom polygon shape, made with Cinema 4D R11, using the build-in Collada export functionality in Papervision 3D 2.0 Great White.

Cinema 4D R11

You will learn how to prepare your shape for Flash, re-order sub shapes into a hierarchy. Convert the whole thing in to triangular polygons and export the object as a .dae file.

Flash ActionScript 3.0

I have set up a basic ActionScript Only application called Cinema4D (in the file Cinema4D.as). I have the latest Papervision 2.0 Great White framework checkout from SVN, here.

I am not going in to detail on how to setup a basic Papervision scene in AS3 since that is not the scope of this tutorial. See my source code on how I set up my project, yours could differ but that not a problem. After i set up my viewport, camera, scene and basic render engine I do the following:

var materials:MaterialsList = new MaterialsList();
materials.addMaterial( new ColorMaterial( 16711935, 0.8, true ), "all" );

dae = new DAE;
dae.addEventListener( FileLoadEvent.LOAD_COMPLETE, myOnLoadCompleteHandler );
dae.load( "figure.dae", materials );

universe = new DisplayObject3D();
universe.addChild( dae );
scene.addChild( universe );

As you can see, I first make a material list and add one ColorMaterial with the name "all" so the material is applied to every object. Notice the third parameter I pass to the new ColorMaterial() which is set to 'true', meaning the material is interactive so it responds to mouse clicks et cetera.

Remember that you also pass the fourth argument as 'true' while initiating a new ViewPort3D. This will make the viewport interactive.

line 78 initiates a new DAE class instance which holds the functionality for loading an external .dae file. Notice line 79. This one is import. When the .dae file finishes loading the myOnLoadCompleteHandler will be triggered. Line 80 to 84 aren't rocked science so I'll jump over to the event handler function I just mentioned.

private function myOnLoadCompleteHandler( event:FileLoadEvent ) : void
{
    var dae:DAE = event.target as DAE;
    var figure:DisplayObject3D = dae.getChildByName( "Figure", true );

    figure.scale = 2;
    figure.rotationY = 180;

    figure.addEventListener( InteractiveScene3DEvent.OBJECT_CLICK, onClick );

    leftArm = figure.getChildByName( "LeftArm", true );
    rightArm = figure.getChildByName( "RightArm", true );

    leftArm.rotationZ = 20;
    rightArm.rotationZ = -20;
}

Since loading DAE files is an asynchronous process we have to wait till the FileLoadEvent.LOAD_COMPLETE event is triggered. Only then, and not a milli second sooner, are we able to 'browse' the hierarchy of DisplayObject3D objects inside the .dae. As you can see in line 132 I access my DisplayObject3D by name, the name corresponds to the name we used in Cinema4D for the object. Now we are able to do all sorts of things with the individual objects. No matter if they would be children of children of children, or the whole object itself. We can rotate them, move/rotate/scale them when your mouse cursor moves. Make the figure dance when we click a button, you name it.

ps; you may have noticed the getChildByName() in line 139 and 140 are using a different name as in the tutorial. This is because I changed the names of the left and right arm in Cinema4D and totally forgot the tell you about it. So you will use "Left Upper Arm" instead of "LeftArm" and "Right Upper Arm" instead of "RightArm".

Hope this tutorial helped.