I do not see all my objects after I import them

Hello.
I am writing a Blender Import/Export for a new file format, alone with a series of blender scripts to inject physics and graphics properties to a Graphic scene.
The idea is as follow
-Import Graphics scene
-Edit scene, adding properties
-Export modified Scene.
However I am having a problem that does not seem to go away.
When I import me graphics Scene only few object show on eh screen, I have to move the mouse of select the object in the scene so that they become visible.
I can no determine what I am doing wrong since the scene seems to load fine. And also it seems to happen with scene with more than one root model.
This is the function than loads the scene.


 
#
# implement main scene loader function
#
def LoadAlchemediaScene(filename):
 ''' Load a newton Alchemedia file '''
 scene = pyScene.pyScene()
 scene.Load (filename)
 root = scene.GetRoot() 
 
 # get the active blender scene
 blenderScene = bpy.data.scenes.active
 
 # load all unique textures
 (path, name) = os.path.split (filename)
 
 childLink = scene.GetFirstChildLink (root)
 while childLink != None: 
  textureNode = scene.GetNodeFromLink(childLink)
  if scene.IsTextureNode(textureNode) == True:
   texture = CreateBlenderTextureFromNode (scene, textureNode, blenderScene, path)
   
   # add a map key for asigning faces
   sourceTexture = pyScene.pyTexture(scene, textureNode)
   g_textureMap[sourceTexture.GetId()] = texture
  childLink = scene.GetNextChildLink (root, childLink)
 
  
 # import all objects into a blender scene 
 myChidren = []
 childLink = scene.GetFirstChildLink (root)
 while childLink != None: 
  node = scene.GetNodeFromLink(childLink)
  if scene.IsSceneNode(node) == True:
     LoadNodesScene(scene, node, blenderScene, myChidren)
  childLink = scene.GetNextChildLink (root, childLink)
 # make sure everthing is updated before exiting
 # I see this in some demos, but nthing seeme to makes the scene render completly
 blenderScene.update(1)
 Blender.Redraw()

The Plug-in can be downloaded here
http://www.newtondynamics.com/downloads/AlchemdiaBlender.rar

It is possible that Blender do no support more than on root object in the scene.
I noticed that this happen when I have multiple root objects.
I also have few other questions but If someone can tell me what I need to do to fix this problem, I can continue and I can probably figure out myself.

what version of blender do you use?

in which line do you add the object to the blender scene?

the version of Blender is 2.49b

and the part that add obledner object to teh scene is thsi function

 
#
# Load all scene objects
#
def LoadNodesScene(scene, rootNode, blenderScene, chidrenList):
 ''' recusivally load convert a scene to a blender scene'''
 blenderObject = None
 meshNode = GetMeshNode(scene, rootNode)
 if meshNode != None:
  blenderObject = CreateBlenderMeshObjectFromNode (scene, meshNode, blenderScene)
 else:
  blenderObject = CreateBlenderEmptyOject (scene, rootNode, blenderScene)
 
 # add the object to the childer list
 chidrenList.append(blenderObject) 
 
 # see if this node has more children and add then to the scene as children of this object
 myChidren = []
 childLink = scene.GetFirstChildLink (rootNode)
 while childLink != None: 
  childNode = scene.GetNodeFromLink(childLink)
  if scene.IsSceneNode(childNode) == True:
     LoadNodesScene(scene, childNode, blenderScene, myChidren)
  childLink = scene.GetNextChildLink (rootNode, childLink)
 
 blenderObject.makeParent(myChidren) 
 # set the object name
 blenderObject.setName (scene.GetNodeName(rootNode))
 
 # set position and oriention
 object = pyScene.pyObject (scene, rootNode)
 posit = object.GetLocalPosition()
 eulers = object.GetLocalEulers()
 scale = object.GetLocalScale()
 euler = [eulers.x, eulers.y, eulers.z]
 
 blenderObject.setSize(scale.x, scale.y, scale.z) 
 blenderObject.setLocation(posit.x, posit.y, posit.z) 
 blenderObject.setEuler(euler) 
 

and the function that creates a single Blender mesh is this

 
#
# Create blender mesh object from node
#
def CreateBlenderMeshObjectFromNode (scene, meshNode, blenderScene): 
 # Get a alchemedia mesh for the node and buidl a blender mesh
 mesh = pyScene.pyMesh (scene, meshNode) 
 
 # create a blender mesh
 newMesh = bpy.data.meshes.new(scene.GetNodeName(meshNode))   
 
 # Create all verte and face data
 vertexList = []  
 vertexCount = mesh.GetVertexCount()
 for i in range(0, vertexCount):
  vertex = mesh.GetVertex (i)
  vertexList.append([vertex.x, vertex.y, vertex.z]) 
 
 faceList = []
 faceNode = mesh.GetFirstTriangle()
 while faceNode != None:
  face = mesh.GetTriangle (faceNode);
  faceList.append([face.p0.vertex, face.p1.vertex, face.p2.vertex]) 
  faceNode = mesh.GetNextTriangle(faceNode)
 newMesh.verts.extend(vertexList)         # add vertices to mesh
 newMesh.faces.extend(faceList)           # add faces to the mesh (also adds edges)
 # create all materials from this mesh
 materialIndex = 0
 materialMap = {}
 meshMaterials = newMesh.materials
 childLink = scene.GetFirstChildLink (meshNode)
 while childLink != None: 
  childNode = scene.GetNodeFromLink(childLink)
  if scene.IsMaterialNode(childNode) == True:
   # make a blender material and a alchemdia material
   material = CreateBlenderMaterialFromNode (scene, childNode, blenderScene)  
   meshMaterials.append(material)
 
   # add a map key for asigning faces
   sourceMaterial = pyScene.pyMaterial(scene, childNode)
   materialMap[sourceMaterial.GetId()] = materialIndex
 
   materialIndex = materialIndex + 1
  childLink = scene.GetNextChildLink (meshNode, childLink)
 newMesh.materials = meshMaterials
 
 # In a secund pass asign material and uv mapping to faces
 faceIndex = 0
 newMesh.faceUV= True
 faceNode = mesh.GetFirstTriangle()
 while faceNode != None:
  face = mesh.GetTriangle (faceNode);
  newMesh.faces[faceIndex].mat = materialMap[face.materialIndex]
  uv0 = mesh.GetUV0(face.p0.uv0)
  uv1 = mesh.GetUV0(face.p1.uv0)
  uv2 = mesh.GetUV0(face.p2.uv0)
  newMesh.faces[faceIndex].uv = [Vector(uv0.x, uv0.y), Vector(uv1.x, uv1.y), Vector(uv2.x, uv2.y)]
 
  faceIndex = faceIndex + 1
  faceNode = mesh.GetNextTriangle(faceNode)
 
 # link mesh to blend objects 
 object = blenderScene.objects.new(newMesh, scene.GetNodeName(meshNode)) 
 
 # calculate normal after mesh is parented 
 #newMesh.mode |= Blender.Mesh.Modes.AUTOSMOOTH
 #newMesh.degr = 30
 #for face in newMesh.faces:
 # face.smooth = 1
 #newMesh.calcNormals()
 return object 

It is more complex but nothing really special, basically it read from one mesh format and make a Blender mesh.
This works for any model made of root and chidren, but fail to show any model made of more than one root.
for example two Boxes at the root level will show only one Box.
But if one Box was a child of another box, then it will display fine.

i made a simple scene with a cube (named “Cube”) that has 2 children (also cubes).

your exporter outputs this:


        <dSceneNodeInfo name="Cube" color="0.750000 0.750000 0.000000 0.000000">
            <transform position="0.000000 0.000000 0.000000 1.000000" eulerAngles="-0.000000 -0.000000 0.000000 0.000000" localScale="1.000000 1.000000 1.000000 1.000000" stretchAxis="1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000" />
            <parentNodes count="1" indices="0" />
            <childrenNodes count="3" indices="24 26 29" />
        </dSceneNodeInfo>

why does cube have 3 children?

btw. i think it’s cool that you work on a blender exporter/importer. i like your physics engine. :slight_smile:

edit:
i have examined an imported scene with several root objects now. all objects seem to be there (they are listed in the outliner) but they all are at the same location. that’s why only one is visible. probably you are doing something wrong with the transformations?

-I think the transformations are correct. because after I select them they shwo up in the righ place. can I see the completer sampel mesh you exported?
It si eassy to see if teh tranform are right by ispecting the file…

-The number of children and parents is not an indication of the number chidren in the scene. The format is a Graph that can take any other object as a child or Parent.
In your test mesh I am guessing you are getting two children as child objects, plus one Mesh which is a node as well.

-Thanks for the commnet on the engine, If this goes well I will write a couple of scripts to model complex scenes in blender, maybe a runtime module to test the Scene engine, although I do not know how to do tha for a Script yet, I hope it is possible.

http://www.pastexall.org/15668

(remove the x from the link! somehow the forum doesn’t seem to like links to this site.)

this is my example. it looks like only one object because they all are at the same location. but in the original blender scene they weren’t at the same location but the parent cubes were 4 units apart.

http://www.blender.org/documentation/242PythonDoc/API_related-module.html
maybe script links can be used to run a simulation directly in blender. but in the blender 2.5x series (which still is in beta) this probably will work differently. so maybe better wait for a final blender 2.5x.

you are right, i looked into the xml and the transformations seem to be there correctly. it looks like some weird bug with blender’s python api.

blenderObject.setSize(scale.x, scale.y, scale.z)
blenderObject.setLocation(posit.x, posit.y, posit.z)
blenderObject.setEuler(euler)

maybe you can try to use blenderObject.setMatrix() instead of those?

Ha Ok I will try to use setMatrix tonight, maybe that is the problem.

i tried it.

#blenderObject.setSize(scale.x, scale.y, scale.z)
#blenderObject.setLocation(posit.x, posit.y, posit.z) 
#blenderObject.setEuler(euler) 
    
blenderObject.setMatrix(TranslationMatrix(Vector(posit.x, posit.y, posit.z)))

(you have to import the matrix stuff from mathutils.)

it works! :slight_smile: but i only tested it with the position. you also have to add scale and eulers to the matrix.

Excellent, you where right the function setPosition, setScale, and set rotatiion are wrong in blender

I was reluctant to believe what you said but I tried anyway, and yes indeed using SetMatrix solves the problem.
What is funny is that in the documnation in the Blender website specifically say that the method SetMatrix is Bad and that I might be replaced in teh future, therefore I did not use in the first place.
This is what it says.

Note: This method is “bad”: when called it changes the location, rotation and size attributes of the object (since Blender uses these values to calculate the object’s transformation matrix). Ton is not happy having a method which “pretends” to do a matrix operation. In the future, this method may be replaced with other methods which make it easier for the user to determine the correct loc/rot/size values for necessary for the object

 
 #posit = object.GetLocalPosition()
 #eulers = object.GetLocalEulers()
 #scale = object.GetLocalScale()
 #euler = [eulers.x, eulers.y, eulers.z]
 #blenderObject.setSize(scale.x, scale.y, scale.z) 
 #blenderObject.setLocation(posit.x, posit.y, posit.z) 
 #blenderObject.setEuler(euler) 
 
 objectMatrix = object.GetMatrix4x4()
 blenderMatrix = Blender.Mathutils.Matrix([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1])
 
 blenderMatrix[0][0] = objectMatrix.e00
 blenderMatrix[0][1] = objectMatrix.e01
 blenderMatrix[0][2] = objectMatrix.e02
 
 blenderMatrix[1][0] = objectMatrix.e10
 blenderMatrix[1][1] = objectMatrix.e11
 blenderMatrix[1][2] = objectMatrix.e12
 blenderMatrix[2][0] = objectMatrix.e20
 blenderMatrix[2][1] = objectMatrix.e21
 blenderMatrix[2][2] = objectMatrix.e22
 
 blenderMatrix[3][0] = objectMatrix.e30
 blenderMatrix[3][1] = objectMatrix.e31
 blenderMatrix[3][2] = objectMatrix.e32
 
 blenderObject.setMatrix(blenderMatrix)

I can continues now,
Thankyou very much.
Later when I have more finish version I will post again, maybe you can test it.

the blender python api has some bugs but in my own projects so far i always was able to find workarounds. in general i found it quite nice to work with.

yes, i can test future versions if you like.