diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-01-07 12:50:23 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-01-07 12:50:23 +0300 |
commit | 57a3cff3b890d93089a1b45c40bdc14d8101fb5e (patch) | |
tree | 32212f25dfe1dc064ee2d3d2dac747f415795f8d /source | |
parent | 41c00063dd43f1a653747129bdb29dcdb13818dd (diff) |
patch [#25440] Object.vertex_group fixin'
from Dan Eicher (dna)
From the tracker (with minor edits)
========================
cube = bpy.data.objects['Cube']
foo = cube.vertex_groups.new('foo')
foo.add([1,3,5,7], 1.0, 'ADD')
for i in range(len(cube.data.vertices)):
try:
weight = foo.weight(i)
print('vert: %i weight: %f' % (i, weight))
except:
pass
foo.remove([1,3])
cube.vertex_groups.remove(foo)
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/editors/include/ED_mesh.h | 3 | ||||
-rw-r--r-- | source/blender/editors/object/object_vgroup.c | 61 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_object.c | 114 |
3 files changed, 138 insertions, 40 deletions
diff --git a/source/blender/editors/include/ED_mesh.h b/source/blender/editors/include/ED_mesh.h index d1bce26dd45..386bbc0a236 100644 --- a/source/blender/editors/include/ED_mesh.h +++ b/source/blender/editors/include/ED_mesh.h @@ -193,12 +193,15 @@ int paintface_minmax(struct Object *ob, float *min, float *max); struct bDeformGroup *ED_vgroup_add(struct Object *ob); struct bDeformGroup *ED_vgroup_add_name(struct Object *ob, const char *name); +void ED_vgroup_delete(struct Object *ob, struct bDeformGroup *defgroup); void ED_vgroup_select_by_name(struct Object *ob, char *name); void ED_vgroup_data_create(struct ID *id); int ED_vgroup_give_array(struct ID *id, struct MDeformVert **dvert_arr, int *dvert_tot); int ED_vgroup_copy_array(struct Object *ob, struct Object *ob_from); void ED_vgroup_mirror(struct Object *ob, int mirror_weights, int flip_vgroups); +int ED_vgroup_object_is_edit_mode(struct Object *ob); + void ED_vgroup_vert_add(struct Object *ob, struct bDeformGroup *dg, int vertnum, float weight, int assignmode); void ED_vgroup_vert_remove(struct Object *ob, struct bDeformGroup *dg, int vertnum); float ED_vgroup_vert_weight(struct Object *ob, struct bDeformGroup *dg, int vertnum); diff --git a/source/blender/editors/object/object_vgroup.c b/source/blender/editors/object/object_vgroup.c index d95220fa379..410ad9394a3 100644 --- a/source/blender/editors/object/object_vgroup.c +++ b/source/blender/editors/object/object_vgroup.c @@ -68,6 +68,8 @@ /************************ Exported Functions **********************/ static void vgroup_remap_update_users(Object *ob, int *map); +static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *defgroup); +static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg); static Lattice *vgroup_edit_lattice(Object *ob) { @@ -79,6 +81,16 @@ static Lattice *vgroup_edit_lattice(Object *ob) return NULL; } +int ED_vgroup_object_is_edit_mode(Object *ob) +{ + if(ob->type == OB_MESH) + return (((Mesh*)ob->data)->edit_mesh != NULL); + else if(ob->type == OB_LATTICE) + return (((Lattice*)ob->data)->editlatt != NULL); + + return 0; +} + bDeformGroup *ED_vgroup_add_name(Object *ob, const char *name) { bDeformGroup *defgroup; @@ -103,6 +115,25 @@ bDeformGroup *ED_vgroup_add(Object *ob) return ED_vgroup_add_name(ob, "Group"); } +void ED_vgroup_delete(Object *ob, bDeformGroup *defgroup) +{ + bDeformGroup *dg = (bDeformGroup *)ob->defbase.first; + + while (dg) { + if (dg == defgroup) + break; + dg = dg->next; + } + + if (dg == NULL) + return; + + if(ED_vgroup_object_is_edit_mode(ob)) + vgroup_delete_edit_mode(ob, dg); + else + vgroup_delete_object_mode(ob, dg); +} + void ED_vgroup_data_create(ID *id) { /* create deform verts */ @@ -508,7 +539,7 @@ static float get_vert_def_nr(Object *ob, int def_nr, int vertnum) } if(dvert==NULL) - return 0.0f; + return -1; dvert += vertnum; @@ -516,17 +547,17 @@ static float get_vert_def_nr(Object *ob, int def_nr, int vertnum) if(dvert->dw[i].def_nr == def_nr) return dvert->dw[i].weight; - return 0.0f; + return -1; } float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum) { int def_nr; - if(!ob) return 0.0f; + if(!ob) return -1; def_nr = defgroup_find_index(ob, dg); - if(def_nr < 0) return 0.0f; + if(def_nr < 0) return -1; return get_vert_def_nr(ob, def_nr, vertnum); } @@ -1094,16 +1125,11 @@ static void vgroup_delete_update_users(Object *ob, int id) } -static void vgroup_delete_object_mode(Object *ob) +static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg) { - bDeformGroup *dg; MDeformVert *dvert, *dvert_array=NULL; int i, e, dvert_tot=0; - dg = BLI_findlink(&ob->defbase, (ob->actdef-1)); - if(!dg) - return; - ED_vgroup_give_array(ob->data, &dvert_array, &dvert_tot); if(dvert_array) { @@ -1199,18 +1225,13 @@ static void vgroup_active_remove_verts(Object *ob, int allverts) } } -static void vgroup_delete_edit_mode(Object *ob) +static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *defgroup) { - bDeformGroup *defgroup; int i; if(!ob->actdef) return; - defgroup = BLI_findlink(&ob->defbase, ob->actdef-1); - if(!defgroup) - return; - /* Make sure that no verts are using this group */ vgroup_active_remove_verts(ob, 1); @@ -1286,10 +1307,14 @@ static int vgroup_object_in_edit_mode(Object *ob) static void vgroup_delete(Object *ob) { + bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef-1); + if(!dg) + return; + if(vgroup_object_in_edit_mode(ob)) - vgroup_delete_edit_mode(ob); + vgroup_delete_edit_mode(ob, dg); else - vgroup_delete_object_mode(ob); + vgroup_delete_object_mode(ob, dg); } static void vgroup_delete_all(Object *ob) diff --git a/source/blender/makesrna/intern/rna_object.c b/source/blender/makesrna/intern/rna_object.c index 978171997ef..6cb5a074c03 100644 --- a/source/blender/makesrna/intern/rna_object.c +++ b/source/blender/makesrna/intern/rna_object.c @@ -125,6 +125,7 @@ EnumPropertyItem object_type_curve_items[] = { #include "DNA_key_types.h" #include "DNA_constraint_types.h" +#include "DNA_lattice_types.h" #include "BKE_armature.h" #include "BKE_bullet.h" @@ -1114,10 +1115,60 @@ static void rna_Object_boundbox_get(PointerRNA *ptr, float *values) } -static void rna_Object_add_vertex_to_group(Object *ob, int index_len, int *index, bDeformGroup *def, float weight, int assignmode) +static bDeformGroup *rna_Object_vgroup_new(Object *ob, const char *name) { + bDeformGroup *defgroup = ED_vgroup_add_name(ob, name); + + WM_main_add_notifier(NC_OBJECT|ND_DRAW, ob); + + return defgroup; +} + +static void rna_Object_vgroup_remove(Object *ob, bDeformGroup *defgroup) +{ + ED_vgroup_delete(ob, defgroup); + + WM_main_add_notifier(NC_OBJECT|ND_DRAW, ob); +} + +static void rna_VertexGroup_vertex_add(ID *id, bDeformGroup *def, ReportList *reports, int index_len, int *index, float weight, int assignmode) +{ + Object *ob = (Object *)id; + + if(ED_vgroup_object_is_edit_mode(ob)) { + BKE_reportf(reports, RPT_ERROR, "VertexGroup.add(): Can't be called while object is in edit mode."); + return; + } + while(index_len--) - ED_vgroup_vert_add(ob, def, *index++, weight, assignmode); + ED_vgroup_vert_add(ob, def, *index++, weight, assignmode); /* XXX, not efficient calling within loop*/ + + WM_main_add_notifier(NC_GEOM|ND_DATA, (ID *)ob->data); +} + +static void rna_VertexGroup_vertex_remove(ID *id, bDeformGroup *dg, ReportList *reports, int index_len, int *index) +{ + Object *ob = (Object *)id; + + if(ED_vgroup_object_is_edit_mode(ob)) { + BKE_reportf(reports, RPT_ERROR, "VertexGroup.remove(): Can't be called while object is in edit mode."); + return; + } + + while(index_len--) + ED_vgroup_vert_remove(ob, dg, *index++); + + WM_main_add_notifier(NC_GEOM|ND_DATA, (ID *)ob->data); +} + +static float rna_VertexGroup_weight(ID *id, bDeformGroup *dg, ReportList *reports, int index) +{ + float weight = ED_vgroup_vert_weight((Object *)id, dg, index); + + if(weight < 0) { + BKE_reportf(reports, RPT_ERROR, "Vertex not in group"); + } + return weight; } /* generic poll functions */ @@ -1154,6 +1205,14 @@ static void rna_def_vertex_group(BlenderRNA *brna) { StructRNA *srna; PropertyRNA *prop; + FunctionRNA *func; + + static EnumPropertyItem assign_mode_items[] = { + {WEIGHT_REPLACE, "REPLACE", 0, "Replace", "Replace"}, + {WEIGHT_ADD, "ADD", 0, "Add", "Add"}, + {WEIGHT_SUBTRACT, "SUBTRACT", 0, "Subtract", "Subtract"}, + {0, NULL, 0, NULL, NULL} + }; srna= RNA_def_struct(brna, "VertexGroup", NULL); RNA_def_struct_sdna(srna, "bDeformGroup"); @@ -1169,6 +1228,32 @@ static void rna_def_vertex_group(BlenderRNA *brna) RNA_def_property_clear_flag(prop, PROP_EDITABLE); RNA_def_property_int_funcs(prop, "rna_VertexGroup_index_get", NULL, NULL); RNA_def_property_ui_text(prop, "Index", "Index number of the vertex group"); + + func= RNA_def_function(srna, "add", "rna_VertexGroup_vertex_add"); + RNA_def_function_ui_description(func, "Add vertices to the group."); + RNA_def_function_flag(func, FUNC_USE_REPORTS|FUNC_USE_SELF_ID); + /* TODO, see how array size of 0 works, this shouldnt be used */ + prop= RNA_def_int_array(func, "index", 1, NULL, 0, 0, "", "Index List.", 0, 0); + RNA_def_property_flag(prop, PROP_DYNAMIC|PROP_REQUIRED); + prop= RNA_def_float(func, "weight", 0, 0.0f, 1.0f, "", "Vertex weight.", 0.0f, 1.0f); + RNA_def_property_flag(prop, PROP_REQUIRED); + prop= RNA_def_enum(func, "type", assign_mode_items, 0, "", "Vertex assign mode."); + RNA_def_property_flag(prop, PROP_REQUIRED); + + func= RNA_def_function(srna, "remove", "rna_VertexGroup_vertex_remove"); + RNA_def_function_ui_description(func, "Remove a vertex from the group."); + RNA_def_function_flag(func, FUNC_USE_REPORTS|FUNC_USE_SELF_ID); + /* TODO, see how array size of 0 works, this shouldnt be used */ + prop= RNA_def_int_array(func, "index", 1, NULL, 0, 0, "", "Index List.", 0, 0); + RNA_def_property_flag(prop, PROP_DYNAMIC|PROP_REQUIRED); + + func= RNA_def_function(srna, "weight", "rna_VertexGroup_weight"); + RNA_def_function_ui_description(func, "Get a vertex weight from the group."); + RNA_def_function_flag(func, FUNC_USE_REPORTS|FUNC_USE_SELF_ID); + prop=RNA_def_int(func, "index", 0, 0, INT_MAX, "Index", "The index of the vertex.", 0, INT_MAX); + RNA_def_property_flag(prop, PROP_REQUIRED); + prop= RNA_def_float(func, "weight", 0, 0.0f, 1.0f, "", "Vertex weight.", 0.0f, 1.0f); + RNA_def_function_return(func, prop); } static void rna_def_material_slot(BlenderRNA *brna) @@ -1545,13 +1630,6 @@ static void rna_def_object_particle_systems(BlenderRNA *brna, PropertyRNA *cprop /* object.vertex_groups */ static void rna_def_object_vertex_groups(BlenderRNA *brna, PropertyRNA *cprop) { - static EnumPropertyItem assign_mode_items[] = { - {WEIGHT_REPLACE, "REPLACE", 0, "Replace", "Replace"}, - {WEIGHT_ADD, "ADD", 0, "Add", "Add"}, - {WEIGHT_SUBTRACT, "SUBTRACT", 0, "Subtract", "Subtract"}, - {0, NULL, 0, NULL, NULL} - }; - StructRNA *srna; PropertyRNA *prop; @@ -1578,24 +1656,16 @@ static void rna_def_object_vertex_groups(BlenderRNA *brna, PropertyRNA *cprop) RNA_def_property_update(prop, NC_GEOM|ND_DATA, "rna_Object_internal_update_data"); /* vertex groups */ // add_vertex_group - func= RNA_def_function(srna, "new", "ED_vgroup_add_name"); + func= RNA_def_function(srna, "new", "rna_Object_vgroup_new"); RNA_def_function_ui_description(func, "Add vertex group to object."); parm= RNA_def_string(func, "name", "Group", 0, "", "Vertex group name."); /* optional */ parm= RNA_def_pointer(func, "group", "VertexGroup", "", "New vertex group."); RNA_def_function_return(func, parm); - func= RNA_def_function(srna, "assign", "rna_Object_add_vertex_to_group"); - RNA_def_function_ui_description(func, "Add vertex to a vertex group."); - /* TODO, see how array size of 0 works, this shouldnt be used */ - parm= RNA_def_int_array(func, "index", 1, NULL, 0, 0, "", "Index List.", 0, 0); - RNA_def_property_flag(parm, PROP_DYNAMIC); - RNA_def_property_flag(parm, PROP_REQUIRED); - parm= RNA_def_pointer(func, "group", "VertexGroup", "", "Vertex group to add vertex to."); - RNA_def_property_flag(parm, PROP_REQUIRED); - parm= RNA_def_float(func, "weight", 0, 0.0f, 1.0f, "", "Vertex weight.", 0.0f, 1.0f); - RNA_def_property_flag(parm, PROP_REQUIRED); - parm= RNA_def_enum(func, "type", assign_mode_items, 0, "", "Vertex assign mode."); - RNA_def_property_flag(parm, PROP_REQUIRED); + func= RNA_def_function(srna, "remove", "rna_Object_vgroup_remove"); + RNA_def_function_ui_description(func, "Delete vertex group from object."); + parm= RNA_def_pointer(func, "group", "VertexGroup", "", "Vertex group to remove."); + RNA_def_property_flag(parm, PROP_REQUIRED|PROP_NEVER_NULL); } |