3
$\begingroup$

I need to read the vertex groups in objects for an exporter I'm working on. I'm using evaluated_get to get the objects with modifiers applied in a non-destructive way. However doing this makes the object have no vertex groups on vertices. Basically any mesh I read is exported as if it was rigid instead of rigged. Is there a way around this?

https://github.com/Project-Cartographer/H2V-Blender-JMSv2-Exporter/blob/master/io_scene_jms/export_jms.py#L289

                    modifier_list = []
                    if triangulate_faces:
                        for modifier in obj.modifiers:
                            modifier.show_render = True
                            modifier.show_viewport = True
                            modifier.show_in_editmode = True
                            modifier_list.append(modifier.type)

                        if not 'TRIANGULATE' in modifier_list:
                            obj.modifiers.new("Triangulate", type='TRIANGULATE')

                        depsgraph = bpy.context.evaluated_depsgraph_get()
                        object_eval = obj.evaluated_get(depsgraph)
                        geometry_list.append(object_eval)
$\endgroup$
0

1 Answer 1

2
$\begingroup$

Preserve all layers

By default it preserve all layers is turned off when using to_mesh on the evaluated object, set it to True

Test in console, Default cube, default subsurf modifier, 3 vertex groups. (All verts in vgroup 0, a single in groups 1 and 2)

>>> dg = C.evaluated_depsgraph_get()
>>> me = C.object.data
>>> len(me.vertices)
8

>>> for v in me.vertices:
...     [g.group for g in v.groups]
...     
[0]
[0]
[0]
[1, 0]
[0]
[0]
[0]
[2, 0]

Using Object.evaluated_get

>>> o = C.object.evaluated_get(dg)
>>> eme = o.to_mesh(preserve_all_data_layers=True, depsgraph=dg)
>>> for v in eme.vertices:
...     [g.group for g in v.groups]
...     
[0]
[0]
[0]
[1, 0]
[0]
[0]
[0]
[2, 0]
[0]
[0]
[1, 0]
[0, 1]
[0]
[2, 0, 1]
[0, 2]
[0]
[0, 2]
[0]
[0]
[0]
[1, 0]
[2, 1, 0]
[2, 0]
[0]
[0]
[1, 0, 2]

Bmesh from object

The method outlined here also preserves the vertex groups.

get the evaluated bmesh, write to a mesh, count the verts and check for vert group assignments.. (prob should have put vert index and weight test in too, but you get the gist)

>>> bm = bmesh.new()
>>> bm.from_object(C.object, dg)
>>> bm.to_mesh(D.meshes.new("Test"))
>>> len(D.meshes["Test"].vertices)
26

>>> for v in D.meshes["Test"].vertices:
...     [g.group for g in v.groups]
...     
[0]
[0]
[0]
[1, 0]
[0]
[0]
[0]
[2, 0]
[0]
[0]
[1, 0]
[0, 1]
[0]
[2, 0, 1]
[0, 2]
[0]
[0, 2]
[0]
[0]
[0]
[1, 0]
[2, 1, 0]
[2, 0]
[0]
[0]
[1, 0, 2]
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .