2
$\begingroup$

I'm writing a python script that is about to reduce the vertex count of some meshes in accordance with some custom rules (for technical uses, for example, parallel edges must remain parallel and so on).

For this, I need to store data considering Meshes, Vertices and Edges (for example, connecting edges' length for a vertex). It's important that I need to access the containing Mesh from a Vertex or Edge.

My first approach was to inherit a VertexData object from the Vertex where I can add all data needed, like this:

class CustomVertex(bpy.types.MeshVertex):
    def __init__(self, inMesh, inVertex):
        self.data = blabla

But later I realized that this doesn't work too well, for example, I couldn't give more than one parameter to __init__() . (When inherited from a Mesh, __init__() could absorb more parameters).

When discussed here, I was advised to use Properties:

class CustomVertex(bpy.types.PropertyGroup):
    pass
bpy.utils.register_class(CustomVertex)
bpy.types.Mesh.my_vertdata = bpy.props.CollectionProperty(type=CustomVertex)
objdata = bpy.context.object.data
new_item = objdata.my_vertdata.add()

etc.

But when using this, I realized that

  1. I cannot add data to Vertices and Edges this way (only to Meshes, but Meshes allowed inheriting from them, so the first approach was better for this).
  2. I have much more constraints (for example, I cannot sort CollectionProperty like I can sort a list).
  3. My code became much more complicated, I myself cannot read it easily.
  4. I suspect that *Property stuff is useful only when it must be set via UI (most of my internal data doesn't have to be like this).

So, finally my question is: are these my only options for storing/manipulatin geometry/object-related data in Blender? Which is the best approach for this?

$\endgroup$
  • $\begingroup$ A point I don't understand in your question: does the script need to be dynamic? By that I mean updating constantly the mesh when it is modified via the UI for instance? Or it is static: reducing the vertex count after the mesh is stable/statically defined? $\endgroup$ – lemon Mar 22 '17 at 5:52
  • $\begingroup$ Suggest looking at the bmesh module $\endgroup$ – batFINGER Mar 22 '17 at 6:39
  • $\begingroup$ In an ideal case this could be dynamic. Ideally the user should draw a slider from 4 (minimal count of edges, these stuff are rectangular) to the actual count of the edges. But it's quite a challenge (after removing edges, new intersecting vertices must be calculated and when undoing that remove the new vertices), so as a first version this can be static driven by a parameter (and if result isn't good it can be undone). $\endgroup$ – Gyula Sámuel Karli Mar 22 '17 at 21:14
  • $\begingroup$ In the dynamical approach, will you add or remove vertices to/from the original model? $\endgroup$ – lemon Mar 23 '17 at 8:38
  • $\begingroup$ Anything should work, for example, the script would add a new Mesh tho the model or change the Mesh into the newly created one. Obviously user should see what he/she does, so the original Mesh in Edit mode should be seen. (An even simpler solution would be to only select vertices or edges to delete and remove them manually.) $\endgroup$ – Gyula Sámuel Karli Mar 24 '17 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.