Here’s my own attempt at procedurally creating cubes (without collision geometry or whatever attached to them), parenting them together and then copying them:
from panda3d.core import *
from direct.showbase.ShowBase import ShowBase
class Cube(object):
def __init__(self, parent_np, name):
vertex_format = GeomVertexFormat.getV3n3cpt2()
vertex_data = GeomVertexData("cube_data", vertex_format, Geom.UHStatic)
tris_prim = GeomTriangles(Geom.UHStatic)
pos_writer = GeomVertexWriter(vertex_data, "vertex")
normal_writer = GeomVertexWriter(vertex_data, "normal")
vertex_count = 0
for direction in (-1, 1):
for i in range(3):
normal = VBase3()
normal[i] = direction
for a, b in ( (-1., -1.), (-1., 1.), (1., 1.), (1., -1.) ):
pos = VBase3()
pos[i] = direction
pos[(i - direction) % 3] = a
pos[(i - direction * 2) % 3] = b
pos_writer.addData3f(pos)
normal_writer.addData3f(normal)
vertex_count += 4
tris_prim.addVertices(vertex_count - 4, vertex_count - 3, vertex_count - 2)
tris_prim.addVertices(vertex_count - 2, vertex_count - 1, vertex_count - 4)
cube_geom = Geom(vertex_data)
cube_geom.addPrimitive(tris_prim)
cube_node = GeomNode(name)
cube_node.addGeom(cube_geom)
self._origin = parent_np.attachNewNode(cube_node)
def getOrigin(self):
return self._origin
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# set up a light source
p_light = PointLight("point_light")
p_light.setColor(VBase4(1., 1., 1., 1.))
self.light = self.camera.attachNewNode(p_light)
self.light.setPos(5., -10., 7.)
self.render.setLight(self.light)
# create cube1
cube1 = Cube(self.render, "cube1").getOrigin()
cube1.setColor(VBase4(1., 0., 0., 1.))
# create cube2
cube2 = Cube(self.render, "cube2").getOrigin()
cube2.setColor(VBase4(0., 1., 0., 1.))
render_2 = NodePath("render_2")
#reparent:
cube1.reparentTo(render_2)#<- reparent to a different scene graph
cube2.reparentTo(cube1)
#positioning etc:
cube2.setPos(10,10,10)
cube2.setH(60)
#ask for the original parent's children:
print "Original KIDS: ", cube1.getChildren()
################################ output ##################################
# KIDS: render_2/cube1/cube2
##########################################################################
# list the original cube hierarchy:
print "Original cube hierarchy:\n"
cube1.ls()
################################ output ##################################
# GeomNode cube1 (1 geoms) S:(ColorAttrib)
# GeomNode cube2 (1 geoms) T:(pos 10 10 10 hpr 60 0 0) S:(ColorAttrib)
##########################################################################
#now the duplicating of the geometry:
cube1_copy=cube1.copyTo(render)
#ask for it's children:
print "\nCopied KIDS: ", cube1_copy.getChildren()
################################ output ##################################
# KIDS: render/cube1/cube2
##########################################################################
# list the copied cube hierarchy:
print "Copied cube hierarchy:\n"
cube1_copy.ls()
################################ output ##################################
# GeomNode cube1 (1 geoms) S:(ColorAttrib)
# GeomNode cube2 (1 geoms) T:(pos 10 10 10 hpr 60 0 0) S:(ColorAttrib)
##########################################################################
self.run()
MyApp()
The output clearly lists cube2 as a child of cube1, so I have no idea why it isn’t working for you.
Right, it doesn’t. What is the output when you call ls() on the parent NodePath? It might shed some light on how everything is parented together.
Also, if you call getChildren() and ls() on the original parent (see my code), do you get the same results? If so (which is what I suspect, since copyTo() is supposed to preserve the hierarchy), then it has nothing to do with copyTo().