I’m trying to figure out a way to recolor models (via their vertex colors) at runtime. I just can’t figure out why Panda keeps complaining about the vertex data I am trying to read and write to. Here is my model-coloring code:
def colorModel(model, pcolor, scolor):
geomNodeCollection = model.findAllMatches('**/+GeomNode')
for nodePath in geomNodeCollection.asList():
geomNode = nodePath.node()
for i in range(geomNode.getNumGeoms()):
geom = geomNode.modifyGeom(i)
vdata = geom.getVertexData()
print type(vdata)
print vdata
# color using GeomVertexRewriter
vcolor = GeomVertexRewriter(vdata, 'color')
if(vcolor is None):
print "Error: model has no color data available"
return
while not vcolor.isAtEnd():
v = vcolor.getData3f()
print "colorvector : ", type(v)
# we will mark vertex colors in the modeller based on how we
# want them to be colored
# magenta is primary
if(v == [1.0, 0.0, 1.0, 1.0]):
newv = pcolor
#cyan is secondary
elif(v == [0.0, 1.0, 1.0, 1.0]):
newv = scolor
# leave other colors as they are
if(newv is not None):
vcolor.setData3f(newv[0], newv[1], newv[2], 1.0)
I constructed most of this function (everything but the stuff at the end) by pasting together the functions in this page of the Panda manual http://www.panda3d.org/manual/index.php/Reading_existing_geometry_data into one big function.
The problem is, when I try to declare my GeomVertexRewriter, I get a TypeError. I am following the manual to the line, and it TypeError complaint lists this as a possible constructor:
GeomVertexRewriter(non-const GeomVertexData vertex_data, const InternalName name)
Which looks a whole hell of a lot like what I’m giving it. As you can see, I’m printing vdata’s type just to make sure it’s a GeomVertexData. I am also printing the contents of vdata, and I can see that there is a ‘color’ column included:
<type 'libpanda.GeomVertexData'>
color_runner
28 rows.
Array 0 (00FF09B0, [ vertex(3f) normal(3f) color(4b) ]):
row 0:
vertex -0.33 -0.396584 0
normal 0 -0.295494 0.955345
color 0 1 1 1
more vertices.....
Could someone please point out where I’m messing up?
If you are curious, I am trying to combine this with flat shading and low-poly models without textures to create a retro-looking aesthetic, from the days where we were beyond wireframes but didn’t have gourad shading yet. The runtime coloring is intended to, as you might expect, provide team coloring for the models.