I'm searching how to batch set/get vertex group data inside blender. using foreach_get() foreach_set() function from bpy.data.objects[0].data.vertices & numpy for a fast calculation.

but it seem that those two function only accept really simple properties? and batch actions on vertex_groups/vcol are not possible in blender ?

this code below is throwing me errors

vertices[...]' elements have no attribute 'groups[0].weight'

but this error message is False the elements does have this attribute !!

enter image description here

note that this error message also occur with getattr() and setattr()

import bpy
import numpy as np

def print_weight(o, vg_name, use_modifiers=True):
    #eval object modifiers to mesh?
    if use_modifiers:
          depsgraph = bpy.context.evaluated_depsgraph_get()
          eo = o.evaluated_get(depsgraph)
          ob = eo.to_mesh(preserve_all_data_layers=True, depsgraph=depsgraph)
    else: ob = o.data
    #slow method
    weight_values = []
    for v in ob.vertices:
        weight_values.append( v.groups[o.vertex_groups[vg_name].index].weight )
    #trying fast batch_get method, unsuccessful..
    weight_values = np.zeros((len(ob.vertices) * 3), dtype=np.float, )
    i = o.vertex_groups[vg_name].index
    ob.vertices.foreach_get(f"groups[{i}].weight", weight_values, )


  • 2
    $\begingroup$ Incorrect assumption. groups[0].weight Is not one attribute. weight is an attribute of groups[0]. $\endgroup$ – batFINGER Nov 10 '20 at 12:41
  • $\begingroup$ oh, of course ! then i assume there's no way to quickly batch get those properties, sad Thanks for the answer $\endgroup$ – DB3D Nov 10 '20 at 12:44
  • $\begingroup$ Don't think so since it will also be a task to size the flat array to populate. Related blender.stackexchange.com/questions/135152/… $\endgroup$ – batFINGER Nov 10 '20 at 12:47
  • $\begingroup$ May also be of interest blender.stackexchange.com/questions/75223/… $\endgroup$ – batFINGER Nov 10 '20 at 13:24

I'm not entirely sure if this already is possible in some way but I made a patch last year that hasn't been reviewed yet to read/write vertex weights in batches. It speeds it up by around a factor of 8x. If you really really need it (and only on your computer) you can download and compile yourself. https://developer.blender.org/D6227

  • 1
    $\begingroup$ Oh man, i really hope this will merged one day, I have addons that could heavily benefit from this patch. $\endgroup$ – DB3D Nov 29 '20 at 13:28
  • $\begingroup$ I really need this patch lol. I see that the foreach_set/get patches got approved fast, could this be rewritten the same way to get it approved faster? $\endgroup$ – VSB Dec 6 '20 at 23:33
  • $\begingroup$ I'll see what I can do to speed it up. Would like to get it reviewed atleast so that I will know what to change / how to design it and hopefully have something ready for 2.92 if it's accepted. $\endgroup$ – ecke101 Dec 8 '20 at 0:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.