copy vgroups to selected objects as long as they have aligned arrays.
authorCampbell Barton <ideasman42@gmail.com>
Tue, 9 Feb 2010 22:00:19 +0000 (22:00 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 9 Feb 2010 22:00:19 +0000 (22:00 +0000)
access in the vertex group panel menu.

release/scripts/ui/properties_data_mesh.py
source/blender/editors/include/ED_mesh.h
source/blender/editors/object/object_intern.h
source/blender/editors/object/object_ops.c
source/blender/editors/object/object_vgroup.c

index 63daf3f..630dd5d 100644 (file)
@@ -32,6 +32,7 @@ class MESH_MT_vertex_group_specials(bpy.types.Menu):
         layout.operator("object.vertex_group_sort", icon='SORTALPHA')
         layout.operator("object.vertex_group_copy", icon='COPY_ID')
         layout.operator("object.vertex_group_copy_to_linked", icon='LINK_AREA')
+        layout.operator("object.vertex_group_copy_to_selected", icon='LINK_AREA')
         layout.operator("object.vertex_group_mirror", icon='ARROW_LEFTRIGHT')
 
 
index 1e0dd78..e9cc3fa 100644 (file)
@@ -190,6 +190,7 @@ struct bDeformGroup         *ED_vgroup_add_name(struct Object *ob, char *name);
 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);
 
 void           ED_vgroup_vert_add(struct Object *ob, struct bDeformGroup *dg, int vertnum,  float weight, int assignmode);
index 0f931e6..985132a 100644 (file)
@@ -184,6 +184,7 @@ void OBJECT_OT_vertex_group_remove_from(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_select(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_deselect(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_copy_to_linked(struct wmOperatorType *ot);
+void OBJECT_OT_vertex_group_copy_to_selected(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_copy(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_normalize(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_normalize_all(struct wmOperatorType *ot);
index 1e71f9b..16c75e7 100644 (file)
@@ -169,6 +169,7 @@ void ED_operatortypes_object(void)
        WM_operatortype_append(OBJECT_OT_vertex_group_select);
        WM_operatortype_append(OBJECT_OT_vertex_group_deselect);
        WM_operatortype_append(OBJECT_OT_vertex_group_copy_to_linked);
+       WM_operatortype_append(OBJECT_OT_vertex_group_copy_to_selected);
        WM_operatortype_append(OBJECT_OT_vertex_group_copy);
        WM_operatortype_append(OBJECT_OT_vertex_group_normalize);
        WM_operatortype_append(OBJECT_OT_vertex_group_normalize_all);
index 0c0b48d..70796bd 100644 (file)
@@ -75,6 +75,7 @@
 #include "object_intern.h"
 
 /************************ Exported Functions **********************/
+static void vgroup_remap_update_users(Object *ob, int *map);
 
 static Lattice *vgroup_edit_lattice(Object *ob)
 {
@@ -151,6 +152,56 @@ int ED_vgroup_give_array(ID *id, MDeformVert **dvert_arr, int *dvert_tot)
        *dvert_tot= 0;
        return FALSE;
 }
+
+/* matching index only */
+int ED_vgroup_copy_array(Object *ob, Object *ob_from)
+{
+       MDeformVert *dvert_array_from, *dvf;
+       MDeformVert *dvert_array, *dv;
+
+       int dvert_tot_from;
+       int dvert_tot;
+       int i;
+       int totdef_from= BLI_countlist(&ob_from->defbase);
+       int totdef= BLI_countlist(&ob->defbase);
+
+       ED_vgroup_give_array(ob_from->data, &dvert_array_from, &dvert_tot_from);
+       ED_vgroup_give_array(ob->data, &dvert_array, &dvert_tot);
+
+       if(ob==ob_from || dvert_tot==0 || (dvert_tot != dvert_tot_from))
+               return 0;
+
+       /* do the copy */
+       BLI_freelistN(&ob->defbase);
+       BLI_duplicatelist(&ob->defbase, &ob_from->defbase);
+       ob->actdef= ob_from->actdef;
+
+       if(totdef_from < totdef) {
+               /* correct vgroup indices because the number of vgroups is being reduced. */
+               int *remap= MEM_mallocN(sizeof(int) * (totdef + 1), "ED_vgroup_copy_array");
+               for(i=0; i<=totdef_from; i++) remap[i]= i;
+               for(; i<=totdef; i++) remap[i]= 0; /* cany use these, so disable */
+
+               vgroup_remap_update_users(ob, remap);
+               MEM_freeN(remap);
+       }
+
+       dvf= dvert_array_from;
+       dv= dvert_array;
+
+       for(i=0; i<dvert_tot; i++, dvf++, dv++) {
+               if(dv->dw)
+                       MEM_freeN(dv->dw);
+
+               *dv= *dvf;
+
+               if(dv->dw)
+                       dv->dw= MEM_dupallocN(dv->dw);
+       }
+
+       return 1;
+}
+
 /* for mesh in object mode
    lattice can be in editmode */
 void ED_vgroup_nr_vert_remove(Object *ob, int def_nr, int vertnum)
@@ -1758,7 +1809,7 @@ static int vertex_group_copy_to_linked_exec(bContext *C, wmOperator *op)
 void OBJECT_OT_vertex_group_copy_to_linked(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Copy Vertex Group to Linked";
+       ot->name= "Copy Vertex Groups to Linked";
        ot->idname= "OBJECT_OT_vertex_group_copy_to_linked";
        ot->description= "Copy Vertex Groups to all users of the same Geometry data.";
 
@@ -1770,6 +1821,36 @@ void OBJECT_OT_vertex_group_copy_to_linked(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
+static int vertex_group_copy_to_selected_exec(bContext *C, wmOperator *op)
+{
+       Object *obact= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+
+       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)
+       {
+               if(obact != ob)
+                       ED_vgroup_copy_array(ob, obact);
+       }
+       CTX_DATA_END;
+
+       return OPERATOR_FINISHED;
+}
+
+
+void OBJECT_OT_vertex_group_copy_to_selected(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Copy Vertex Group to Selected";
+       ot->idname= "OBJECT_OT_vertex_group_copy_to_selected";
+       ot->description= "Copy Vertex Groups to other selected objects with matching indicies.";
+
+       /* api callbacks */
+       ot->poll= vertex_group_poll;
+       ot->exec= vertex_group_copy_to_selected_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
 static EnumPropertyItem vgroup_items[]= {
        {0, NULL, 0, NULL, NULL}};