3
$\begingroup$

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, )

    print(weight_values)
    return

    
    
print_weight(bpy.context.object,"Group")
$\endgroup$
4
  • 2
    $\begingroup$ Incorrect assumption. groups[0].weight Is not one attribute. weight is an attribute of groups[0]. $\endgroup$
    – batFINGER
    Nov 10, 2020 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$
    – Fox
    Nov 10, 2020 at 12:44
  • 1
    $\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, 2020 at 12:47
  • 1
    $\begingroup$ May also be of interest blender.stackexchange.com/questions/75223/… $\endgroup$
    – batFINGER
    Nov 10, 2020 at 13:24

1 Answer 1

5
$\begingroup$

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

$\endgroup$
4
  • 2
    $\begingroup$ Oh man, i really hope this will merged one day, I have addons that could heavily benefit from this patch. $\endgroup$
    – Fox
    Nov 29, 2020 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, 2020 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, 2020 at 0:07
  • $\begingroup$ Sorry for noobish question, but the patch will help "only" for custom addons & scripts, but it won't increase the speed of general Weight Painting (which is super-slow in case of larger meshes), right? $\endgroup$ May 24, 2021 at 16:38

You must log in to answer this question.

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