diff options
Diffstat (limited to 'source/blender/editors/armature/editarmature.c')
-rw-r--r-- | source/blender/editors/armature/editarmature.c | 212 |
1 files changed, 103 insertions, 109 deletions
diff --git a/source/blender/editors/armature/editarmature.c b/source/blender/editors/armature/editarmature.c index 7a2d26af4ed..c0f84064aba 100644 --- a/source/blender/editors/armature/editarmature.c +++ b/source/blender/editors/armature/editarmature.c @@ -27,11 +27,11 @@ #include <ctype.h> #include <stdlib.h> +#include <stddef.h> #include <string.h> #include <math.h> #include <float.h> -#include "MEM_guardedalloc.h" #include "DNA_anim_types.h" #include "DNA_armature_types.h" @@ -39,6 +39,8 @@ #include "DNA_meshdata_types.h" #include "DNA_scene_types.h" +#include "MEM_guardedalloc.h" + #include "BLI_blenlib.h" #include "BLI_math.h" #include "BLI_editVert.h" @@ -58,11 +60,10 @@ #include "BKE_object.h" #include "BKE_report.h" #include "BKE_subsurf.h" -#include "BKE_utildefines.h" #include "BKE_modifier.h" +#include "DNA_object_types.h" #include "BIF_gl.h" -#include "BIF_generate.h" #include "RNA_access.h" #include "RNA_define.h" @@ -421,37 +422,45 @@ void ED_armature_apply_transform(Object *ob, float mat[4][4]) /* exported for use in editors/object/ */ /* 0 == do center, 1 == center new, 2 == center cursor */ -void docenter_armature (Scene *scene, View3D *v3d, Object *ob, int centermode) +void docenter_armature (Scene *scene, Object *ob, float cursor[3], int centermode, int around) { Object *obedit= scene->obedit; // XXX get from context EditBone *ebone; bArmature *arm= ob->data; - float cent[3] = {0.0f, 0.0f, 0.0f}; - float min[3], max[3]; - float omat[3][3]; + float cent[3]; /* Put the armature into editmode */ - if(ob!=obedit) + if(ob != obedit) { ED_armature_to_edit(ob); + obedit= NULL; /* we cant use this so behave as if there is no obedit */ + } /* Find the centerpoint */ if (centermode == 2) { - float *fp= give_cursor(scene, v3d); - VECCOPY(cent, fp); + copy_v3_v3(cent, cursor); invert_m4_m4(ob->imat, ob->obmat); mul_m4_v3(ob->imat, cent); } else { - INIT_MINMAX(min, max); - - for (ebone= arm->edbo->first; ebone; ebone=ebone->next) { - DO_MINMAX(ebone->head, min, max); - DO_MINMAX(ebone->tail, min, max); + if(around==V3D_CENTROID) { + int total= 0; + zero_v3(cent); + for (ebone= arm->edbo->first; ebone; ebone=ebone->next) { + total+=2; + add_v3_v3(cent, ebone->head); + add_v3_v3(cent, ebone->tail); + } + mul_v3_fl(cent, 1.0f/(float)total); + } + else { + float min[3], max[3]; + INIT_MINMAX(min, max); + for (ebone= arm->edbo->first; ebone; ebone=ebone->next) { + DO_MINMAX(ebone->head, min, max); + DO_MINMAX(ebone->tail, min, max); + } + mid_v3_v3v3(cent, min, max); } - - cent[0]= (min[0] + max[0]) / 2.0f; - cent[1]= (min[1] + max[1]) / 2.0f; - cent[2]= (min[2] + max[2]) / 2.0f; } /* Do the adjustments */ @@ -461,17 +470,16 @@ void docenter_armature (Scene *scene, View3D *v3d, Object *ob, int centermode) } /* Turn the list into an armature */ - ED_armature_from_edit(ob); - + if(obedit==NULL) { + ED_armature_from_edit(ob); + ED_armature_edit_free(ob); + } + /* Adjust object location for new centerpoint */ if(centermode && obedit==NULL) { - copy_m3_m4(omat, ob->obmat); - - mul_m3_v3(omat, cent); + mul_mat3_m4_v3(ob->obmat, cent); /* ommit translation part */ add_v3_v3(ob->loc, cent); } - else - ED_armature_edit_free(ob); } /* ---------------------- */ @@ -479,13 +487,7 @@ void docenter_armature (Scene *scene, View3D *v3d, Object *ob, int centermode) /* checks if an EditBone with a matching name already, returning the matching bone if it exists */ static EditBone *editbone_name_exists (ListBase *edbo, const char *name) { - EditBone *eBone; - - for (eBone=edbo->first; eBone; eBone=eBone->next) { - if (!strcmp(name, eBone->name)) - return eBone; - } - return NULL; + return BLI_findstring(edbo, name, offsetof(EditBone, name)); } /* note: there's a unique_bone_name() too! */ @@ -802,6 +804,7 @@ static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChann /* join armature exec is exported for use in object->join objects operator... */ int join_armature_exec(bContext *C, wmOperator *op) { + Main *bmain= CTX_data_main(C); Scene *scene= CTX_data_scene(C); Object *ob= CTX_data_active_object(C); bArmature *arm= (ob)? ob->data: NULL; @@ -894,12 +897,12 @@ int join_armature_exec(bContext *C, wmOperator *op) free_pose_channels_hash(pose); } - ED_base_object_free_and_unlink(scene, base); + ED_base_object_free_and_unlink(bmain, scene, base); } } CTX_DATA_END; - DAG_scene_sort(scene); // because we removed object(s) + DAG_scene_sort(bmain, scene); // because we removed object(s) ED_armature_from_edit(ob); ED_armature_edit_free(ob); @@ -1111,6 +1114,7 @@ static void separate_armature_bones (Scene *scene, Object *ob, short sel) /* separate selected bones into their armature */ static int separate_armature_exec (bContext *C, wmOperator *op) { + Main *bmain= CTX_data_main(C); Scene *scene= CTX_data_scene(C); Object *obedit= CTX_data_edit_object(C); Object *oldob, *newob; @@ -1151,7 +1155,7 @@ static int separate_armature_exec (bContext *C, wmOperator *op) ED_armature_edit_free(obedit); /* 2) duplicate base */ - newbase= ED_object_add_duplicate(scene, oldbase, USER_DUP_ARM); /* only duplicate linked armature */ + newbase= ED_object_add_duplicate(bmain, scene, oldbase, USER_DUP_ARM); /* only duplicate linked armature */ newob= newbase->object; newbase->flag &= ~SELECT; @@ -1752,9 +1756,8 @@ EditBone *ED_armature_bone_get_mirrored(ListBase *edbo, EditBone *ebo) if (ebo == NULL) return NULL; - - BLI_strncpy(name, ebo->name, sizeof(name)); - bone_flip_name(name, 0); // 0 = don't strip off number extensions + + flip_side_name(name, ebo->name, FALSE); for (eboflip= edbo->first; eboflip; eboflip=eboflip->next) { if (ebo != eboflip) { @@ -2023,7 +2026,6 @@ void ED_armature_edit_free(struct Object *ob) BLI_freelistN(arm->edbo); } - MEM_freeN(arm->edbo); arm->edbo= NULL; } @@ -2328,12 +2330,10 @@ void add_primitive_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d) Object *obedit= scene->obedit; // XXX get from context float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3]; EditBone *bone; - - VECCOPY(curs, give_cursor(scene, v3d)); /* Get inverse point for head and orientation for tail */ invert_m4_m4(obedit->imat, obedit->obmat); - mul_m4_v3(obedit->imat, curs); + mul_v3_m4v3(curs, obedit->imat, give_cursor(scene, v3d)); if (rv3d && (U.flag & USER_ADD_VIEWALIGNED)) copy_m3_m4(obmat, rv3d->viewmat); @@ -2348,7 +2348,7 @@ void add_primitive_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d) /* Create a bone */ bone= ED_armature_edit_bone_add(obedit->data, "Bone"); - VECCOPY(bone->head, curs); + copy_v3_v3(bone->head, curs); if (rv3d && (U.flag & USER_ADD_VIEWALIGNED)) add_v3_v3v3(bone->tail, bone->head, imat[1]); // bone with unit length 1 @@ -2941,17 +2941,14 @@ static int armature_fill_bones_exec (bContext *C, wmOperator *op) } else if (count == 1) { EditBonePoint *ebp; - float *fp, curs[3]; + float curs[3]; /* Get Points - selected joint */ ebp= (EditBonePoint *)points.first; /* Get points - cursor (tail) */ - fp= give_cursor(scene, v3d); - VECCOPY (curs, fp); - invert_m4_m4(obedit->imat, obedit->obmat); - mul_m4_v3(obedit->imat, curs); + mul_v3_m4v3(curs, obedit->imat, give_cursor(scene, v3d)); /* Create a bone */ newbone= add_points_bone(obedit, ebp->vec, curs); @@ -2979,15 +2976,13 @@ static int armature_fill_bones_exec (bContext *C, wmOperator *op) /* find which one should be the 'head' */ if ((ebp->head_owner && ebp2->head_owner) || (ebp->tail_owner && ebp2->tail_owner)) { /* rule: whichever one is closer to 3d-cursor */ - float curs[3], *fp= give_cursor(scene, v3d); + float curs[3]; float vecA[3], vecB[3]; float distA, distB; /* get cursor location */ - VECCOPY(curs, fp); - invert_m4_m4(obedit->imat, obedit->obmat); - mul_m4_v3(obedit->imat, curs); + mul_v3_m4v3(curs, obedit->imat, give_cursor(scene, v3d)); /* get distances */ sub_v3_v3v3(vecA, ebp->vec, curs); @@ -3007,12 +3002,12 @@ static int armature_fill_bones_exec (bContext *C, wmOperator *op) /* assign head/tail combinations */ if (headtail == 2) { - VECCOPY(head, ebp->vec); - VECCOPY(tail, ebp2->vec); + copy_v3_v3(head, ebp->vec); + copy_v3_v3(tail, ebp2->vec); } else if (headtail == 1) { - VECCOPY(head, ebp2->vec); - VECCOPY(tail, ebp->vec); + copy_v3_v3(head, ebp2->vec); + copy_v3_v3(tail, ebp->vec); } /* add new bone and parent it to the appropriate end */ @@ -3095,16 +3090,16 @@ static void bones_merge(Object *obedit, EditBone *start, EditBone *end, EditBone * - parent = parent of start */ if ((start->flag & BONE_TIPSEL) && ((start->flag & BONE_SELECTED) || start==arm->act_edbone)==0) { - VECCOPY(head, start->tail); + copy_v3_v3(head, start->tail); } else { - VECCOPY(head, start->head); + copy_v3_v3(head, start->head); } if ((end->flag & BONE_ROOTSEL) && ((end->flag & BONE_SELECTED) || end==arm->act_edbone)==0) { - VECCOPY(tail, end->head); + copy_v3_v3(tail, end->head); } else { - VECCOPY(tail, end->tail); + copy_v3_v3(tail, end->tail); } newbone= add_points_bone(obedit, head, tail); newbone->parent = start->parent; @@ -3376,8 +3371,8 @@ static int armature_extrude_exec(bContext *C, wmOperator *op) newbone = MEM_callocN(sizeof(EditBone), "extrudebone"); if (do_extrude==1) { - VECCOPY (newbone->head, ebone->tail); - VECCOPY (newbone->tail, newbone->head); + copy_v3_v3(newbone->head, ebone->tail); + copy_v3_v3(newbone->tail, newbone->head); newbone->parent = ebone; newbone->flag = ebone->flag & BONE_TIPSEL; // copies it, in case mirrored bone @@ -3385,8 +3380,8 @@ static int armature_extrude_exec(bContext *C, wmOperator *op) if (newbone->parent) newbone->flag |= BONE_CONNECTED; } else { - VECCOPY(newbone->head, ebone->head); - VECCOPY(newbone->tail, ebone->head); + copy_v3_v3(newbone->head, ebone->head); + copy_v3_v3(newbone->tail, ebone->head); newbone->parent= ebone->parent; newbone->flag= BONE_TIPSEL; @@ -3476,7 +3471,7 @@ static int armature_bone_primitive_add_exec(bContext *C, wmOperator *op) RNA_string_get(op->ptr, "name", name); - VECCOPY(curs, give_cursor(CTX_data_scene(C),CTX_wm_view3d(C))); + copy_v3_v3(curs, give_cursor(CTX_data_scene(C),CTX_wm_view3d(C))); /* Get inverse point for head and orientation for tail */ invert_m4_m4(obedit->imat, obedit->obmat); @@ -3495,7 +3490,7 @@ static int armature_bone_primitive_add_exec(bContext *C, wmOperator *op) /* Create a bone */ bone= ED_armature_edit_bone_add(obedit->data, name); - VECCOPY(bone->head, curs); + copy_v3_v3(bone->head, curs); if(rv3d && (U.flag & USER_ADD_VIEWALIGNED)) add_v3_v3v3(bone->tail, bone->head, imat[1]); // bone with unit length 1 @@ -3565,17 +3560,17 @@ static int armature_subdivide_exec(bContext *C, wmOperator *op) BLI_addtail(arm->edbo, newbone); /* calculate location of newbone->head */ - VECCOPY(val1, ebone->head); - VECCOPY(val2, ebone->tail); - VECCOPY(val3, newbone->head); + copy_v3_v3(val1, ebone->head); + copy_v3_v3(val2, ebone->tail); + copy_v3_v3(val3, newbone->head); val3[0]= val1[0]*cutratio + val2[0]*cutratioI; val3[1]= val1[1]*cutratio + val2[1]*cutratioI; val3[2]= val1[2]*cutratio + val2[2]*cutratioI; - VECCOPY(newbone->head, val3); - VECCOPY(newbone->tail, ebone->tail); - VECCOPY(ebone->tail, newbone->head); + copy_v3_v3(newbone->head, val3); + copy_v3_v3(newbone->tail, ebone->tail); + copy_v3_v3(ebone->tail, newbone->head); newbone->rad_head= 0.5f * (ebone->rad_head + ebone->rad_tail); ebone->rad_tail= newbone->rad_head; @@ -3793,7 +3788,7 @@ void ARMATURE_OT_switch_direction(wmOperatorType *ot) static void bone_connect_to_existing_parent(EditBone *bone) { bone->flag |= BONE_CONNECTED; - VECCOPY(bone->head, bone->parent->tail); + copy_v3_v3(bone->head, bone->parent->tail); bone->rad_head = bone->parent->rad_tail; } @@ -3821,7 +3816,7 @@ static void bone_connect_to_new_parent(ListBase *edbo, EditBone *selbone, EditBo selbone->flag |= BONE_CONNECTED; sub_v3_v3v3(offset, actbone->tail, selbone->head); - VECCOPY(selbone->head, actbone->tail); + copy_v3_v3(selbone->head, actbone->tail); selbone->rad_head= actbone->rad_tail; add_v3_v3(selbone->tail, offset); @@ -4095,7 +4090,7 @@ void ARMATURE_OT_select_all(wmOperatorType *ot) { /* identifiers */ - ot->name= "deselect all editbone"; + ot->name= "Select or Deselect All"; ot->idname= "ARMATURE_OT_select_all"; /* api callbacks */ @@ -4594,7 +4589,7 @@ static void add_vgroups__mapFunc(void *userData, int index, float *co, float *no /* DerivedMesh mapFunc for getting final coords in weight paint mode */ float (*verts)[3] = userData; - VECCOPY(verts[index], co); + copy_v3_v3(verts[index], co); } static void envelope_bone_weighting(Object *ob, Mesh *mesh, float (*verts)[3], int numbones, Bone **bonelist, bDeformGroup **dgrouplist, bDeformGroup **dgroupflip, float (*root)[3], float (*tip)[3], int *selected, float scale) @@ -4657,7 +4652,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m bArmature *arm= par->data; Bone **bonelist, *bone; bDeformGroup **dgrouplist, **dgroupflip; - bDeformGroup *dgroup, *curdg; + bDeformGroup *dgroup; bPoseChannel *pchan; Mesh *mesh; Mat4 *bbone = NULL; @@ -4721,18 +4716,17 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m /* compute root and tip */ if (bbone) { - VECCOPY(root[j], bbone[segments].mat[3]); - mul_m4_v3(bone->arm_mat, root[j]); + mul_v3_m4v3(root[j], bone->arm_mat, bbone[segments].mat[3]); if ((segments+1) < bone->segments) { - VECCOPY(tip[j], bbone[segments+1].mat[3]) - mul_m4_v3(bone->arm_mat, tip[j]); + mul_v3_m4v3(tip[j], bone->arm_mat, bbone[segments+1].mat[3]); + } + else { + copy_v3_v3(tip[j], bone->arm_tail); } - else - VECCOPY(tip[j], bone->arm_tail) } else { - VECCOPY(root[j], bone->arm_head); - VECCOPY(tip[j], bone->arm_tail); + copy_v3_v3(root[j], bone->arm_head); + copy_v3_v3(tip[j], bone->arm_tail); } mul_m4_v3(par->obmat, root[j]); @@ -4749,17 +4743,10 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m /* find flipped group */ if (dgroup && mirror) { char name[32]; - - BLI_strncpy(name, dgroup->name, 32); + // 0 = don't strip off number extensions - bone_flip_name(name, 0); - - for (curdg = ob->defbase.first; curdg; curdg=curdg->next) { - if (!strcmp(curdg->name, name)) - break; - } - - dgroupflip[j] = curdg; + flip_side_name(name, dgroup->name, FALSE); + dgroupflip[j] = defgroup_find_name(ob, name); } } @@ -4789,7 +4776,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m /* transform verts to global space */ for (i=0; i < mesh->totvert; i++) { if (!vertsfilled) - VECCOPY(verts[i], mesh->mvert[i].co) + copy_v3_v3(verts[i], mesh->mvert[i].co); mul_m4_v3(ob->obmat, verts[i]); } @@ -5027,14 +5014,14 @@ static int pose_clear_rot_exec(bContext *C, wmOperator *op) float eul[3], oldeul[3], quat1[4] = {0}; if (pchan->rotmode == ROT_MODE_QUAT) { - QUATCOPY(quat1, pchan->quat); + copy_qt_qt(quat1, pchan->quat); quat_to_eul( oldeul,pchan->quat); } else if (pchan->rotmode == ROT_MODE_AXISANGLE) { axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,pchan->rotAxis, pchan->rotAngle); } else { - VECCOPY(oldeul, pchan->eul); + copy_v3_v3(oldeul, pchan->eul); } eul[0]= eul[1]= eul[2]= 0.0f; @@ -5057,7 +5044,7 @@ static int pose_clear_rot_exec(bContext *C, wmOperator *op) eulO_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,eul, EULER_ORDER_DEFAULT); } else { - VECCOPY(pchan->eul, eul); + copy_v3_v3(pchan->eul, eul); } } } // Duplicated in source/blender/editors/object/object_transform.c @@ -5213,7 +5200,7 @@ void POSE_OT_select_all(wmOperatorType *ot) { /* identifiers */ - ot->name= "deselect all bones"; + ot->name= "Select or Deselect All"; ot->idname= "POSE_OT_select_all"; /* api callbacks */ @@ -5277,6 +5264,7 @@ static int hide_selected_pose_bone(Object *ob, Bone *bone, void *ptr) if (arm->layer & bone->layer) { if (bone->flag & BONE_SELECTED) { bone->flag |= BONE_HIDDEN_P; + bone->flag &= ~BONE_SELECTED; if(arm->act_bone==bone) arm->act_bone= NULL; } @@ -5479,8 +5467,17 @@ void ED_armature_bone_rename(bArmature *arm, char *oldnamep, char *newnamep) /* Rename the pose channel, if it exists */ if (ob->pose) { bPoseChannel *pchan = get_pose_channel(ob->pose, oldname); - if (pchan) + if (pchan) { BLI_strncpy(pchan->name, newname, MAXBONENAME); + + if (ob->pose->chanhash) { + GHash *gh = ob->pose->chanhash; + + /* remove the old hash entry, and replace with the new name */ + BLI_ghash_remove(gh, oldname, NULL, NULL); + BLI_ghash_insert(gh, pchan->name, pchan); + } + } } /* Update any object constraints to use the new bone name */ @@ -5506,11 +5503,9 @@ void ED_armature_bone_rename(bArmature *arm, char *oldnamep, char *newnamep) } if (modifiers_usesArmature(ob, arm)) { - bDeformGroup *dg; - /* bone name in defgroup */ - for (dg=ob->defbase.first; dg; dg=dg->next) { - if (!strcmp(dg->name, oldname)) - BLI_strncpy(dg->name, newname, MAXBONENAME); + bDeformGroup *dg= defgroup_find_name(ob, oldname); + if(dg) { + BLI_strncpy(dg->name, newname, MAXBONENAME); } } @@ -5539,8 +5534,7 @@ static int armature_flip_names_exec (bContext *C, wmOperator *op) /* loop through selected bones, auto-naming them */ CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) { - BLI_strncpy(newname, ebone->name, sizeof(newname)); - bone_flip_name(newname, 1); // 1 = do strip off number extensions + flip_side_name(newname, ebone->name, TRUE); // 1 = do strip off number extensions ED_armature_bone_rename(arm, ebone->name, newname); } CTX_DATA_END; |