5 Replies Latest reply on Jan 14, 2009 5:01 PM by strayhand

    XML + Papervision = Frustration

    strayhand Level 1
      What do I need to do to get this "ERROR 1009" to go away?

      [quote]
      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at org.papervision3d.render::
      [/quote]

      I've read various Web sites and this problem creeps up for several using papervision people but I haven't really found a solid solution. I think that it's more fundamental than papervision and that's why I'm posting here. I think it has to do with the order in which things are firing off. Perhaps the onEnterFrame method is firing off before the XML has been read. BUT I tried to organize my script in a way that would avoid such a situation and that's what's frustrating. Any insight that you might have into what I'm doing wrong would be appreciated.

      Basically I'm trying to write a simple script that will allow me read an XML file containing nodes that describe the models and primitive shapes that I want to render into the flash movie.

      Here's a snippet of the XML file that I'm loading:

      [code]
      <?xml version="1.0" encoding="utf-8"?>
      <scenes>
      <scene>
      <viewport width="820" height="440" scaleToStage="false" interactive="true" />
      <models>
      <model id="base" castShadows="true" type="plane">
      <dimensions w="600" h="600" />
      <material type="BitmapFileMaterial">
      <path>materials/base.png</path>
      </material>
      <position x="0" y="0" z="0" />
      <rotation x="0" y="0" z="0" />
      <scale>1</scale>
      <segments w="3" h="3" />
      </model>
      </models>
      </scene>
      </scenes>
      [/code]

      So in this XML snippet I only have one scene, and only one model but I plan on adding more once I get this blasted script running.

      My entire project can be downloaded here. I've also attached the code. I should also note that I'm using Flash to compile and Flex to code this project.

      Thanks.
        • 1. Re: XML + Papervision = Frustration
          Level 7

          "strayhand" <webforumsuser@macromedia.com> wrote in message
          news:gkjbub$oc2$1@forums.macromedia.com...
          > What do I need to do to get this "ERROR 1009" to go away?
          >
          > [quote]
          > TypeError: Error #1009: Cannot access a property or method of a null
          > object
          > reference.
          > at org.papervision3d.render::
          > [/quote]
          >
          > I've read various Web sites and this problem creeps up for several using
          > papervision people but I haven't really found a solid solution. I think
          > that
          > it's more fundamental than papervision and that's why I'm posting here. I
          > think
          > it has to do with the order in which things are firing off. Perhaps the
          > onEnterFrame method is firing off before the XML has been read. BUT I
          > tried to
          > organize my script in a way that would avoid such a situation and that's
          > what's
          > frustrating. Any insight that you might have into what I'm doing wrong
          > would be
          > appreciated.
          >
          > Basically I'm trying to write a simple script that will allow me read an
          > XML
          > file containing nodes that describe the models and primitive shapes that I
          > want
          > to render into the flash movie.

          In your init3D() function, set a flag. In your enterFrame handler, check
          for the flag before calling for the render.

          There's an article on the Flash Developer Center about creating a Flash
          component that talks about how to make use of the existing Flash
          invalidation model (which seems quite different from the Flex one) and add
          your own stuff to it. I'd suggest you see if you can find that article and
          read that part of it.

          HTH;

          Amy


          • 2. Re: XML + Papervision = Frustration
            Peter Lorent Level 2
            Did you check if the program enters modelBuildPlane()?
            • 3. Re: XML + Papervision = Frustration
              Peter Lorent Level 2
              -> for each (var property:XML in xml.scenes.scene)
              You probably want:
              for each (var property:XML in xml.scene)
              • 4. XML + Papervision = Frustration
                strayhand Level 1
                Amy,

                Thanks for the reply. I did just as you suggested. I created a few boolean flags to check if init3d and initmodel had been called before calling the render object inside of the enter frame method.

                Code:

                if(flagInit3d == true && flagInitModels == true)
                {
                renderer.renderScene(scene, camera, viewport);
                }

                That seems to have pacified the errors but things are still not working correctly.

                LuigiL,

                I think you're correct in your second comment. I want xml.scenes (not xml.scenes.scene). Ironically this is where I'm having the most trouble right now. I put a trace statement inside of my for each loop and I discovered that it's not properly parsing the XML. I ran a check on my XML file and everything seems to be in order. So I think that I may have typecast my variable wrong and that I should be using XMLList or XMLNode instead of making everything XML.

                I'm trying to read the xml object and copy a certain level of node to a new variable property. I'm then using AS3's built in XML tools to access the nodes within the property variable and assign them to my 3D objects. But somethings not working correctly. Below you'll find a snippet of my code.

                Code:

                private function init3d():void
                {

                //trace ("made it inside init3d");

                for each (var property:XML in xml.scenes)
                {

                trace (property);

                // Setup Viewport
                viewport = new Viewport3D(property.viewport.attribute("width")
                ,property.viewport.attribute("height")
                ,property.viewport.attribute("scaleToStage")
                ,property.viewport.attribute("interactive"));
                // Add to movie
                addChild(viewport);

                // Setup Models
                initModels(property);
                }

                // Flag - Verify that init3d was accessed
                flagInit3d = true;

                addEventListener(Event.ENTER_FRAME, onEnterFrame);
                }

                Why is the property variable empty when I compile the swf?

                Here's the XML again if anyone's interested.

                XML:

                <?xml version="1.0" encoding="utf-8"?>
                <scenes>
                <scene>
                <viewport width="820" height="440" scaleToStage="false" interactive="true" />
                <models>
                <model id="base" castShadows="false" type="plane">
                <dimensions w="600" h="600" />
                <material type="BitmapFileMaterial">
                <path>materials/base.png</path>
                </material>
                <position x="0" y="0" z="0" />
                <rotation x="0" y="0" z="0" />
                <scale>1</scale>
                <segments w="3" h="3" />
                </model>
                <model id="menu" castShadows="true" type="plane">
                <dimensions w="600" h="600" />
                <material type="ColorMaterial">
                <color>0xFFCC00</color>
                </material>
                <position x="0" y="0" z="0" />
                <rotation x="0" y="0" z="0" />
                <scale>1</scale>
                <segments w="3" h="3" />
                </model>
                </models>
                </scene>
                </scenes>

                I would appreciate anyone's insight. Thanks.
                • 5. Re: XML + Papervision = Frustration
                  strayhand Level 1
                  Well...

                  I believe that I've figured out my issue(s).

                  1. Amy's tip helped me to suppress and check for the existence of my 3d objects before calling the render method using some simple boolean flags. (Thanks Amy)

                  2. LuigiL's comments lead me in the right direction concerning the XML. I went back into another project that I had created and noticed that I had a root XML node wrapping everything. And so I decided to wrap my new XML document in another layer and for whatever reason I've got things talking at the correct level now.

                  So I'm able to generate a simple 3D plane using AS3 + Papervision + XML. I have a long ways to go but I'll post my code here for anyone who wants to learn from it. You can also download it here. Thank you.

                  XML:

                  <?xml version="1.0" encoding="utf-8"?>
                  <papervision>
                  <scenes>
                  <scene>
                  <viewport width="820" height="440" scaleToStage="false" interactive="true" />
                  <models>
                  <model id="base" castShadows="false" type="plane">
                  <dimensions w="600" h="600" />
                  <material type="BitmapFileMaterial">
                  <path>materials/base.png</path>
                  </material>
                  <position x="0" y="0" z="0" />
                  <rotation x="0" y="0" z="0" />
                  <scale x="1" y="1" z="1" />
                  <segments w="3" h="3" />
                  </model>
                  </models>
                  </scene>
                  </scenes>
                  </papervision>

                  Code:

                  package {
                  //- LIBRARIES ----------------------------------------------------------------------------------------- ----
                  import flash.display.Sprite;
                  import flash.events.Event;
                  import flash.net.URLLoader;
                  import flash.net.URLRequest;

                  import org.papervision3d.cameras.Camera3D;
                  import org.papervision3d.materials.BitmapFileMaterial;
                  import org.papervision3d.materials.ColorMaterial;
                  import org.papervision3d.objects.primitives.Plane;
                  import org.papervision3d.render.BasicRenderEngine;
                  import org.papervision3d.scenes.Scene3D;
                  import org.papervision3d.view.Viewport3D;

                  public class pluhome extends Sprite
                  {
                  //- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------

                  // XML
                  private var xml:XML;

                  // Papervision
                  private var viewport:Viewport3D;
                  private var scene:Scene3D = new Scene3D();
                  private var camera:Camera3D = new Camera3D();
                  private var renderer:BasicRenderEngine = new BasicRenderEngine();

                  // Placeholder variables (So that import scripts get included)
                  private var placeholderBitmapFileMaterial:BitmapFileMaterial = new BitmapFileMaterial("materials/placeholder.png");
                  private var placeholderColorMaterial:ColorMaterial = new ColorMaterial(0xFFCC00);
                  //private var placeholderMovieAssetMaterial:MovieAssetMaterial = new MovieAssetMaterial("materials/placeholder.swf", false);

                  // Flags
                  private var flagInit3d:Boolean = false;
                  private var flagInitModels:Boolean = false;

                  //- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------

                  //- CONSTRUCTOR ----------------------------------------------------------------------------------------- --

                  public function pluhome()
                  {
                  initXML();
                  }
                  //- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------

                  private function initXML():void
                  {
                  // Setup XML path
                  var xmlDefault:String = "papervision.xml";
                  var xmlPath:String = this.loaderInfo.parameters.xmlpath;

                  if(xmlPath != null)
                  {
                  xmlDefault = xmlPath;
                  }

                  // Load XML, call event
                  var loader:URLLoader = new URLLoader(new URLRequest(xmlDefault));
                  loader.addEventListener(Event.COMPLETE, onXMLLoaded);
                  }

                  private function init3d():void
                  {
                  trace("Made it inside init3d");

                  for each (var property:XML in xml.scenes.scene)
                  {

                  // Setup Viewport
                  viewport = new Viewport3D(property.viewport.attribute("width")
                  ,property.viewport.attribute("height")
                  ,property.viewport.attribute("scaleToStage")
                  ,property.viewport.attribute("interactive"));
                  // Add to movie
                  addChild(viewport);

                  // Setup Models
                  initModels(property);
                  }

                  // Flag - Verify that init3d was accessed
                  flagInit3d = true;

                  addEventListener(Event.ENTER_FRAME, onEnterFrame);
                  }

                  private function initModels(property:XML):void
                  {
                  trace("Made it inside initModels");

                  for each (var model:XML in property.models.model)
                  {
                  var type:String = model.attribute("type");

                  switch(type)
                  {
                  case "plane":
                  modelBuildPlane(model);
                  break;
                  default:
                  // Do nothing
                  ;
                  }
                  }

                  // Flag - verify that initModels was accessed
                  flagInitModels = true;
                  }

                  private function modelBuildPlane(model:XML):void
                  {
                  trace("Made it inside modelBuildPlane");

                  // Material Type
                  var type:String = model.material.attribute("type");

                  // Material
                  var material;

                  switch(type)
                  {
                  case "BitmapFileMaterial":
                  var bitmapMaterial:BitmapFileMaterial = new BitmapFileMaterial(model.material.path);
                  bitmapMaterial.doubleSided = true;
                  material = bitmapMaterial;
                  break;
                  case "ColorMaterial":
                  var colorMaterial:ColorMaterial = new ColorMaterial(model.material.color);
                  colorMaterial.doubleSided = true;
                  material = colorMaterial;
                  break;
                  case "MovieAssetMaterial":
                  break;
                  case "WireframeMaterial":
                  break;
                  default:
                  // Color Material
                  break;
                  }

                  trace(material);

                  // Create plane
                  var plane:Plane = new Plane(material
                  ,model.dimensions.attribute("width")
                  ,model.dimensions.attribute("height")
                  ,model.segments.attribute("w")
                  ,model.segments.attribute("h"));

                  // Position plane
                  plane.x = model.position.attribute("x");
                  plane.y = model.position.attribute("y");
                  plane.z = model.position.attribute("x");

                  // Rotate plane
                  plane.rotationX = model.rotation.attribute("x");
                  plane.rotationY = model.rotation.attribute("y");
                  plane.rotationZ = model.rotation.attribute("z");

                  // Scale plane
                  plane.scaleX = model.scale.attribute("x");
                  plane.scaleY = model.scale.attribute("y");
                  plane.scaleZ = model.scale.attribute("z");

                  if(type == "BitmapFileMaterial")
                  {
                  // Add plane to scene
                  scene.addChild(plane);
                  }
                  else
                  {
                  // Add plane to scene
                  scene.addChild(plane);
                  }

                  }

                  //- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------

                  //- EVENT HANDLERS ----------------------------------------------------------------------------------------

                  public function onXMLLoaded (e:Event):void
                  {
                  // Assign the XML data to a variable
                  xml = new XML(e.target.data);

                  // Initialize 3d
                  if(xml != null)
                  {
                  init3d();
                  }
                  }

                  public function onEnterFrame (e:Event):void
                  {
                  if(flagInit3d == true && flagInitModels == true)
                  {
                  renderer.renderScene(scene, camera, viewport);
                  }
                  }

                  //- GETTERS & SETTERS VARIABLES ---------------------------------------------------------------------------

                  //- HELPERS ----------------------------------------------------------------------------------------- ------

                  //- END CLASS ----------------------------------------------------------------------------------------- ----
                  }
                  }