gerwindehaan / osgswig

Python bindings for OpenSceneGraph (auto-export from code.google.com/p/osgswig)
Other
1 stars 1 forks source link

Node cannot be drawn without saving it to disk first. #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When I create nodes with osgswig and want to show them in the viewer (with
wxPython) they just do not show up.
The weird thing is that if I use the exact same code to create the node but
save it to disk using osgDB.writeNodeFile(node, 'node.osg') and read it
right back using node = osgDB.readNodeFile('node.osg') it works.

First I thought it is some mistake in my code but the same thing happens
when I use the example code (pyramid).

To reconstruct this bug, just open the example file osgviewerWX.py and
replace the OnOpen(self, evt) medthod with this code from the pyramid.py:

    def OnOpen(self, evt):
        # create a root node
        node = osg.Group()

        # Line Geometry
        lineGeode = osg.Geode()
        lineGeometry = osg.Geometry()
        lineGeode.addDrawable(lineGeometry)
        lineStateSet = lineGeode.getOrCreateStateSet()

        lineVertices = osg.Vec3Array()
        lineVertices.push_back(osg.Vec3(-10,10,0))
        lineVertices.push_back(osg.Vec3(-10,-10,0))
        lineGeometry.setVertexArray(lineVertices)

        lineBase = osg.DrawElementsUInt(osg.PrimitiveSet.LINES,0)
        lineBase.push_back(0)
        lineBase.push_back(1)
        lineGeometry.addPrimitiveSet(lineBase)

        node.addChild(lineGeode)

        # Pyramid geometry, following tutorial 
        pyramidGeode = osg.Geode()
        pyramidGeometry = osg.Geometry()
        pyramidGeode.addDrawable(pyramidGeometry)
        pyramidStateSet = pyramidGeode.getOrCreateStateSet()

        pyramidVertices = osg.Vec3Array()
        pyramidVertices.push_back(osg.Vec3(0,0,0))
        pyramidVertices.push_back(osg.Vec3(10,0,0))
        pyramidVertices.push_back(osg.Vec3(10,10,0))
        pyramidVertices.push_back(osg.Vec3(0,10,0))
        pyramidVertices.push_back(osg.Vec3(5,5,10))
        pyramidGeometry.setVertexArray(pyramidVertices)

        pyramidBase = osg.DrawElementsUInt(osg.PrimitiveSet.QUADS,0)
        pyramidBase.push_back(3)
        pyramidBase.push_back(2)
        pyramidBase.push_back(1)
        pyramidBase.push_back(0)
        pyramidGeometry.addPrimitiveSet(pyramidBase)

        pyramidFace1 = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES,0)
        pyramidFace1.push_back(0)
        pyramidFace1.push_back(1)
        pyramidFace1.push_back(4)
        pyramidGeometry.addPrimitiveSet(pyramidFace1)

        pyramidFace2 = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES,0)
        pyramidFace2.push_back(1)
        pyramidFace2.push_back(2)
        pyramidFace2.push_back(4)
        pyramidGeometry.addPrimitiveSet(pyramidFace2)

        pyramidFace3 = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES,0)
        pyramidFace3.push_back(2)
        pyramidFace3.push_back(3)
        pyramidFace3.push_back(4)
        pyramidGeometry.addPrimitiveSet(pyramidFace3)

        pyramidFace4 = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES,0)
        pyramidFace4.push_back(3)
        pyramidFace4.push_back(0)
        pyramidFace4.push_back(4)
        pyramidGeometry.addPrimitiveSet(pyramidFace4)

        colors= osg.Vec4Array()
        colors.push_back(osg.Vec4(1,0,0,1)) #index 0 red
        colors.push_back(osg.Vec4(0,1,0,1)) #index 1 green
        colors.push_back(osg.Vec4(0,0,1,1)) #index 2 blue
        colors.push_back(osg.Vec4(1,1,1,1)) #index 3 white

        colorIndexArray = osg.UIntArray()
        colorIndexArray.push_back(0)
        colorIndexArray.push_back(1)
        colorIndexArray.push_back(2)
        colorIndexArray.push_back(3)
        colorIndexArray.push_back(0)

        pyramidGeometry.setColorArray(colors)
        pyramidGeometry.setColorIndices(colorIndexArray)
        pyramidGeometry.setColorBinding(osg.Geometry.BIND_PER_VERTEX)

        pyramidGeometry.setSupportsDisplayList(False)

        node.addChild(pyramidGeode)

#        osgDB.writeNodeFile(node, 'pyramid.osg')
#        node = osgDB.readNodeFile('pyramid.osg')

        self.canvas.viewer.setSceneData(node)

What is the expected output? What do you see instead?
I would expect to see the pyramid, but I see nothing.
If I uncomment the two lines at the end it works.

What version of the product are you using? On what operating system?
I'm using osgswig 0.9.0

Original issue reported on code.google.com by birnt...@in.tum.de on 7 Sep 2008 at 10:31

GoogleCodeExporter commented 9 years ago
Oh, and I'm using Windows XP SP2

Original comment by birnt...@in.tum.de on 7 Sep 2008 at 11:33

GoogleCodeExporter commented 9 years ago
Hi, which osg version are you using ?
Could you perhaps post a complete file as an attachment so I can quickly have a 
look
at it?
cheers,
Gerwin

Original comment by gerwinde...@gmail.com on 14 Oct 2008 at 12:11

GoogleCodeExporter commented 9 years ago
I haven't tried this myself, but often I encounter problems with reference 
counting
and python garbage collection.

One should be aware that the reference counting of python objects and that of 
the OSG
Referenced counterparts (e.g. Node, Array etc.) is linked. If a python object 
goes
out of scope (e.g. the end of a function is reached), the osg reference counter 
of
this object goes down. If this reference counter reaches zero, the node is 
deleted.
If this node is attached somewhere in the scene, reference counting should not 
reach
zero and all should be well. 

In this example, I'm not sure if the self.canvas.viewer.setSceneData(node) 
correctly
updates the reference count. To be sure, you might want to create a top-level 
node
and set this to the scene, and addChild the node in your function under this 
node.
Alternatively, you could keep a reference to the node in your python class (e.g.
self._node = node)

Original comment by gerwinde...@gmail.com on 15 Oct 2008 at 12:44