Change vertex data of existing geom?

Hi, I’m working on procedural generation of 3d structures, and wanted to add a method for flipping every normal of a given primitive.

Short explanation of my variables:
face is a list with all sorts of information and index[5] is an alias for core.geomTriangles.
So the first line just returns a tuple with the vertex indices I want to change.
I then created a data object, that supposedly gives a pointer to the vertex data that is modifiyable? That’s the doc for that.
But my intuition was to just use a vertex writer on the vertex data (self.v_dat) itself. I’ve tried the vertex writer on both data and self.v_dat, but neither of those changed a thing. The setData(2,3,5) is just a random vector to see if it changes anything.

 def flip_normals(self, face):
         """
         Flips the normals of a given face
         """
         t_tri = face[5].getVertexList()
         data = self.b_geom.modifyVertexData()
         n_flipper = cor.GeomVertexWriter(data, "normal")
         n_reader = cor.GeomVertexReader(self.v_dat, "normal")

         for vertex in t_tri:
             n_reader.setRow(vertex)
             print("vertex {}: {}".format(vertex, n_reader.getData3f()))
             n_flipper.setRow(vertex)
             n_flipper.setData3f(2, 3, 5)
             print("vertex {}: {}".format(vertex, n_reader.getData3f()))

How have you determined that it’s not doing anything? Do you have lighting active to be able to see the effects of the normals?

Note that the way the triangles are facing is actually not a property of the normal vectors, but of the winding order of the indices in the GeomTriangles.

Wow, you have got to be one of the most involved maintainers out there… Thank you for the quick reply.
I overread that at first, but you’re saying that the normal component of the vector doesn’t determine the way a triangle is facing? They just affect lighting? In that case, my question is answered and I have to figure something else out.
I still don’t get why the writer didn’t change the data, I checked for that using the print statements, and also printing the vertex data afterwards, but at least I know now that the effort was futile. Maybe just set them two-sided.

It’s not immediately obvious to me why the change didn’t show up. It might be that using a GeomVertexWriter and GeomVertexReader at the same time is simply not supported and that you’re supposed to use a GeomVertexRewriter instead (or destroy the writer before creating the reader).

The normal indeed doesn’t affect which way a face is facing; the normal is only used to determine lighting information. You can however simply call geom.reverseInPlace() to reverse the winding order of each face.

Tested it out without the writers, still no change. But it’s not that bad, I can figure something else out. reverseInPlace() is cool, however I want to apply it to a single primitive thats facing the wrong way, not the entire geom. But again, it doesn’t matter right now, I can circumvent this issue.

GeomPrimitive has a reverse() method that returns a reversed version of the GeomPrimitive object :slight_smile:

Hmmm, This should work, but for some reason it didn’t. I re-assigned it like so:
t_tri = t_tri.reverse(), and when printing, it shows that the indices have been reversed, in this case from 0, 1, 2 to 2, 1, 0.

This time I didn’t change anything with the normals, but I assume that would just fix the lighting, which I will look at later. I should be able to see the face flipped, right? even if it’s just a black surface.

I just tested this by generating a new one with reversed vertices, and it’s flipped. Maybe the fault is somewhere in my program, which is a whole mess of inheritance which I don’t want to bother you with😁…

It returns a new primitive, so you have to replace the existing one using geom.setPrimitive(i, t_tri).

Oh, always something to learn. That finally worked, thank you for your commitment in helping me out!