Using memoryview to assign colors to vertex data

Hi, welcome to the forums!

get_v3c4() is an interleaved format containing two different data types. This is tricky to manipulate in this way.

You have several options:

  1. Create a memoryview in B format so you can store the colours as bytes instead, either doing it separately from the float data or encoding the float data as bytes before you put it into the memoryview.
  2. Create a custom GeomVertexFormat storing the colours as 4 floats instead. If your source data is in floats, this may be easiest, although it is less memory-efficient than 4 bytes, of course.
  3. Create a custom GeomVertexFormat containing two arrays, one with the float data, and one with the byte data. You can then call modify_array(1) to get a handle to this second array and create a second memoryview with the B format. You would create the vertex format like so:
vertex_format = GeomVertexFormat()
vertex_format.add_array(GeomVertexFormat.get_v3().arrays[0])
vertex_format.add_array(GeomVertexArrayFormat('color', 4, Geom.NT_uint8, Geom.C_color))
vertex_format = GeomVertexFormat.register_format(vertex_format)
2 Likes