Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / armature / armature_naming.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2009 full recode.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  * Operators and API's for renaming bones both in and out of Edit Mode
26  */
27
28 /** \file blender/editors/armature/armature_naming.c
29  *  \ingroup edarmature
30  */
31
32 #include <string.h>
33
34 #include "DNA_armature_types.h"
35 #include "DNA_constraint_types.h"
36 #include "DNA_object_types.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_ghash.h"
40 #include "BLI_string_utils.h"
41 #include "BLI_utildefines.h"
42
43 #include "BLT_translation.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_action.h"
47 #include "BKE_armature.h"
48 #include "BKE_constraint.h"
49 #include "BKE_context.h"
50 #include "BKE_deform.h"
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53 #include "BKE_modifier.h"
54
55 #include "DEG_depsgraph.h"
56
57 #include "RNA_access.h"
58 #include "RNA_define.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "ED_armature.h"
64 #include "ED_screen.h"
65
66 #include "armature_intern.h"
67
68 /* This file contains functions/API's for renaming bones and/or working with them */
69
70 /* ************************************************** */
71 /* EditBone Names */
72
73 /* note: there's a unique_bone_name() too! */
74 static bool editbone_unique_check(void *arg, const char *name)
75 {
76         struct {ListBase *lb; void *bone; } *data = arg;
77         EditBone *dupli = ED_armature_ebone_find_name(data->lb, name);
78         return dupli && dupli != data->bone;
79 }
80
81 void ED_armature_ebone_unique_name(ListBase *edbo, char *name, EditBone *bone)
82 {
83         struct {ListBase *lb; void *bone; } data;
84         data.lb = edbo;
85         data.bone = bone;
86
87         BLI_uniquename_cb(editbone_unique_check, &data, DATA_("Bone"), '.', name, sizeof(bone->name));
88 }
89
90 /* ************************************************** */
91 /* Bone Renaming - API */
92
93 static bool bone_unique_check(void *arg, const char *name)
94 {
95         return BKE_armature_find_bone_name((bArmature *)arg, name) != NULL;
96 }
97
98 static void unique_bone_name(bArmature *arm, char *name)
99 {
100         BLI_uniquename_cb(bone_unique_check, (void *)arm, DATA_("Bone"), '.', name, sizeof(((Bone *)NULL)->name));
101 }
102
103 /* helper call for armature_bone_rename */
104 static void constraint_bone_name_fix(Object *ob, ListBase *conlist, const char *oldname, const char *newname)
105 {
106         bConstraint *curcon;
107         bConstraintTarget *ct;
108
109         for (curcon = conlist->first; curcon; curcon = curcon->next) {
110                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(curcon);
111                 ListBase targets = {NULL, NULL};
112
113                 /* constraint targets */
114                 if (cti && cti->get_constraint_targets) {
115                         cti->get_constraint_targets(curcon, &targets);
116
117                         for (ct = targets.first; ct; ct = ct->next) {
118                                 if (ct->tar == ob) {
119                                         if (STREQ(ct->subtarget, oldname)) {
120                                                 BLI_strncpy(ct->subtarget, newname, MAXBONENAME);
121                                         }
122                                 }
123                         }
124
125                         if (cti->flush_constraint_targets)
126                                 cti->flush_constraint_targets(curcon, &targets, 0);
127                 }
128
129                 /* action constraints */
130                 if (curcon->type == CONSTRAINT_TYPE_ACTION) {
131                         bActionConstraint *actcon = (bActionConstraint *)curcon->data;
132                         BKE_action_fix_paths_rename(&ob->id, actcon->act, "pose.bones", oldname, newname, 0, 0, 1);
133                 }
134         }
135 }
136
137 /* called by UI for renaming a bone */
138 /* warning: make sure the original bone was not renamed yet! */
139 /* seems messy, but thats what you get with not using pointers but channel names :) */
140 void ED_armature_bone_rename(bArmature *arm, const char *oldnamep, const char *newnamep)
141 {
142         Object *ob;
143         char newname[MAXBONENAME];
144         char oldname[MAXBONENAME];
145
146         /* names better differ! */
147         if (!STREQLEN(oldnamep, newnamep, MAXBONENAME)) {
148
149                 /* we alter newname string... so make copy */
150                 BLI_strncpy(newname, newnamep, MAXBONENAME);
151                 /* we use oldname for search... so make copy */
152                 BLI_strncpy(oldname, oldnamep, MAXBONENAME);
153
154                 /* now check if we're in editmode, we need to find the unique name */
155                 if (arm->edbo) {
156                         EditBone *eBone = ED_armature_ebone_find_name(arm->edbo, oldname);
157
158                         if (eBone) {
159                                 ED_armature_ebone_unique_name(arm->edbo, newname, NULL);
160                                 BLI_strncpy(eBone->name, newname, MAXBONENAME);
161                         }
162                         else {
163                                 return;
164                         }
165                 }
166                 else {
167                         Bone *bone = BKE_armature_find_bone_name(arm, oldname);
168
169                         if (bone) {
170                                 unique_bone_name(arm, newname);
171                                 BLI_strncpy(bone->name, newname, MAXBONENAME);
172                         }
173                         else {
174                                 return;
175                         }
176                 }
177
178                 /* do entire dbase - objects */
179                 for (ob = G.main->object.first; ob; ob = ob->id.next) {
180                         ModifierData *md;
181
182                         /* we have the object using the armature */
183                         if (arm == ob->data) {
184                                 Object *cob;
185
186                                 /* Rename the pose channel, if it exists */
187                                 if (ob->pose) {
188                                         bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, oldname);
189                                         if (pchan) {
190                                                 GHash *gh = ob->pose->chanhash;
191
192                                                 /* remove the old hash entry, and replace with the new name */
193                                                 if (gh) {
194                                                         BLI_assert(BLI_ghash_haskey(gh, pchan->name));
195                                                         BLI_ghash_remove(gh, pchan->name, NULL, NULL);
196                                                 }
197
198                                                 BLI_strncpy(pchan->name, newname, MAXBONENAME);
199
200                                                 if (gh) {
201                                                         BLI_ghash_insert(gh, pchan->name, pchan);
202                                                 }
203                                         }
204
205                                         BLI_assert(BKE_pose_channels_is_valid(ob->pose) == true);
206                                 }
207
208                                 /* Update any object constraints to use the new bone name */
209                                 for (cob = G.main->object.first; cob; cob = cob->id.next) {
210                                         if (cob->constraints.first)
211                                                 constraint_bone_name_fix(ob, &cob->constraints, oldname, newname);
212                                         if (cob->pose) {
213                                                 bPoseChannel *pchan;
214                                                 for (pchan = cob->pose->chanbase.first; pchan; pchan = pchan->next) {
215                                                         constraint_bone_name_fix(ob, &pchan->constraints, oldname, newname);
216                                                 }
217                                         }
218                                 }
219                         }
220
221                         /* See if an object is parented to this armature */
222                         if (ob->parent && (ob->parent->data == arm)) {
223                                 if (ob->partype == PARBONE) {
224                                         /* bone name in object */
225                                         if (STREQ(ob->parsubstr, oldname))
226                                                 BLI_strncpy(ob->parsubstr, newname, MAXBONENAME);
227                                 }
228                         }
229
230                         if (modifiers_usesArmature(ob, arm)) {
231                                 bDeformGroup *dg = defgroup_find_name(ob, oldname);
232                                 if (dg) {
233                                         BLI_strncpy(dg->name, newname, MAXBONENAME);
234                                 }
235                         }
236
237                         /* fix modifiers that might be using this name */
238                         for (md = ob->modifiers.first; md; md = md->next) {
239                                 switch (md->type) {
240                                         case eModifierType_Hook:
241                                         {
242                                                 HookModifierData *hmd = (HookModifierData *)md;
243
244                                                 if (hmd->object && (hmd->object->data == arm)) {
245                                                         if (STREQ(hmd->subtarget, oldname))
246                                                                 BLI_strncpy(hmd->subtarget, newname, MAXBONENAME);
247                                                 }
248                                                 break;
249                                         }
250                                         case eModifierType_UVWarp:
251                                         {
252                                                 UVWarpModifierData *umd = (UVWarpModifierData *)md;
253
254                                                 if (umd->object_src && (umd->object_src->data == arm)) {
255                                                         if (STREQ(umd->bone_src, oldname))
256                                                                 BLI_strncpy(umd->bone_src, newname, MAXBONENAME);
257                                                 }
258                                                 if (umd->object_dst && (umd->object_dst->data == arm)) {
259                                                         if (STREQ(umd->bone_dst, oldname))
260                                                                 BLI_strncpy(umd->bone_dst, newname, MAXBONENAME);
261                                                 }
262                                                 break;
263                                         }
264                                         default:
265                                                 break;
266                                 }
267                         }
268                 }
269
270                 /* Fix all animdata that may refer to this bone - we can't just do the ones attached to objects, since
271                  * other ID-blocks may have drivers referring to this bone [#29822]
272                  */
273                 // XXX: the ID here is for armatures, but most bone drivers are actually on the object instead...
274                 {
275
276                         BKE_animdata_fix_paths_rename_all(&arm->id, "pose.bones", oldname, newname);
277                 }
278
279                 /* correct view locking */
280                 {
281                         bScreen *screen;
282                         for (screen = G.main->screen.first; screen; screen = screen->id.next) {
283                                 ScrArea *sa;
284                                 /* add regions */
285                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
286                                         SpaceLink *sl;
287                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
288                                                 if (sl->spacetype == SPACE_VIEW3D) {
289                                                         View3D *v3d = (View3D *)sl;
290                                                         if (v3d->ob_centre && v3d->ob_centre->data == arm) {
291                                                                 if (STREQ(v3d->ob_centre_bone, oldname)) {
292                                                                         BLI_strncpy(v3d->ob_centre_bone, newname, MAXBONENAME);
293                                                                 }
294                                                         }
295                                                 }
296                                         }
297                                 }
298                         }
299                 }
300         }
301 }
302
303 typedef struct BoneFlipNameData {
304         struct BoneFlipNameData *next, *prev;
305         char *name;
306         char name_flip[MAXBONENAME];
307 } BoneFlipNameData;
308
309 /**
310  * Renames (by flipping) all selected bones at once.
311  *
312  * This way if we are flipping related bones (e.g., Bone.L, Bone.R) at the same time
313  * all the bones are safely renamed, without conflicting with each other.
314  *
315  * \param arm: Armature the bones belong to
316  * \param bones_names: List of BoneConflict elems.
317  * \param do_strip_numbers: if set, try to get rid of dot-numbers at end of bone names.
318  */
319 void ED_armature_bones_flip_names(bArmature *arm, ListBase *bones_names, const bool do_strip_numbers)
320 {
321         ListBase bones_names_conflicts = {NULL};
322         BoneFlipNameData *bfn;
323
324         /* First pass: generate flip names, and blindly rename.
325          * If rename did not yield expected result, store both bone's name and expected flipped one into temp list
326          * for second pass. */
327         for (LinkData *link = bones_names->first; link; link = link->next) {
328                 char name_flip[MAXBONENAME];
329                 char *name = link->data;
330
331                 /* WARNING: if do_strip_numbers is set, expect completely mismatched names in cases like
332                  * Bone.R, Bone.R.001, Bone.R.002, etc. */
333                 BLI_string_flip_side_name(name_flip, name, do_strip_numbers, sizeof(name_flip));
334
335                 ED_armature_bone_rename(arm, name, name_flip);
336
337                 if (!STREQ(name, name_flip)) {
338                         bfn = alloca(sizeof(BoneFlipNameData));
339                         bfn->name = name;
340                         BLI_strncpy(bfn->name_flip, name_flip, sizeof(bfn->name_flip));
341                         BLI_addtail(&bones_names_conflicts, bfn);
342                 }
343         }
344
345         /* Second pass to handle the bones that have naming conflicts with other bones.
346          * Note that if the other bone was not selected, its name was not flipped, so conflict remains and that second
347          * rename simply generates a new numbered alternative name. */
348         for (bfn = bones_names_conflicts.first; bfn; bfn = bfn->next) {
349                 ED_armature_bone_rename(arm, bfn->name, bfn->name_flip);
350         }
351 }
352
353 /* ************************************************** */
354 /* Bone Renaming - EditMode */
355
356 static int armature_flip_names_exec(bContext *C, wmOperator *op)
357 {
358         Object *ob = CTX_data_edit_object(C);
359         bArmature *arm;
360
361         /* paranoia checks */
362         if (ELEM(NULL, ob, ob->pose))
363                 return OPERATOR_CANCELLED;
364
365         const bool do_strip_numbers = RNA_boolean_get(op->ptr, "do_strip_numbers");
366
367         arm = ob->data;
368
369         ListBase bones_names = {NULL};
370
371         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones)
372         {
373                 BLI_addtail(&bones_names, BLI_genericNodeN(ebone->name));
374         }
375         CTX_DATA_END;
376
377         ED_armature_bones_flip_names(arm, &bones_names, do_strip_numbers);
378
379         BLI_freelistN(&bones_names);
380
381         /* since we renamed stuff... */
382         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
383
384         /* copied from #rna_Bone_update_renamed */
385         /* redraw view */
386         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
387
388         /* update animation channels */
389         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN, ob->data);
390
391         return OPERATOR_FINISHED;
392 }
393
394 void ARMATURE_OT_flip_names(wmOperatorType *ot)
395 {
396         /* identifiers */
397         ot->name = "Flip Names";
398         ot->idname = "ARMATURE_OT_flip_names";
399         ot->description = "Flips (and corrects) the axis suffixes of the names of selected bones";
400
401         /* api callbacks */
402         ot->exec = armature_flip_names_exec;
403         ot->poll = ED_operator_editarmature;
404
405         /* flags */
406         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
407
408         RNA_def_boolean(ot->srna, "do_strip_numbers", false, "Strip Numbers",
409                         "Try to remove right-most dot-number from flipped names "
410                         "(WARNING: may result in incoherent naming in some cases)");
411 }
412
413
414 static int armature_autoside_names_exec(bContext *C, wmOperator *op)
415 {
416         Object *ob = CTX_data_edit_object(C);
417         bArmature *arm;
418         char newname[MAXBONENAME];
419         short axis = RNA_enum_get(op->ptr, "type");
420
421         /* paranoia checks */
422         if (ELEM(NULL, ob, ob->pose))
423                 return OPERATOR_CANCELLED;
424         arm = ob->data;
425
426         /* loop through selected bones, auto-naming them */
427         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones)
428         {
429                 BLI_strncpy(newname, ebone->name, sizeof(newname));
430                 if (bone_autoside_name(newname, 1, axis, ebone->head[axis], ebone->tail[axis]))
431                         ED_armature_bone_rename(arm, ebone->name, newname);
432         }
433         CTX_DATA_END;
434
435         /* since we renamed stuff... */
436         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
437
438         /* note, notifier might evolve */
439         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
440
441         return OPERATOR_FINISHED;
442 }
443
444 void ARMATURE_OT_autoside_names(wmOperatorType *ot)
445 {
446         static const EnumPropertyItem axis_items[] = {
447                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
448                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
449                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
450                 {0, NULL, 0, NULL, NULL}
451         };
452
453         /* identifiers */
454         ot->name = "AutoName by Axis";
455         ot->idname = "ARMATURE_OT_autoside_names";
456         ot->description = "Automatically renames the selected bones according to which side of the target axis they fall on";
457
458         /* api callbacks */
459         ot->invoke = WM_menu_invoke;
460         ot->exec = armature_autoside_names_exec;
461         ot->poll = ED_operator_editarmature;
462
463         /* flags */
464         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
465
466         /* settings */
467         ot->prop = RNA_def_enum(ot->srna, "type", axis_items, 0, "Axis", "Axis tag names with");
468 }
469