Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pose_group.c « armature « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38d15d8b880a8d071c7fd085ad5bf5d303b2fb08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation
 * All rights reserved.
 * Implementation of Bone Groups operators and editing API's
 */

/** \file
 * \ingroup edarmature
 */

#include <string.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"

#include "DNA_armature_types.h"
#include "DNA_object_types.h"

#include "BKE_action.h"
#include "BKE_armature.h"
#include "BKE_context.h"

#include "DEG_depsgraph.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_armature.h"
#include "ED_outliner.h"
#include "ED_screen.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "armature_intern.h"

/* ********************************************** */
/* Bone Groups */

static bool pose_group_poll(bContext *C)
{
  if (!ED_operator_posemode_context(C)) {
    return false;
  }

  Object *obpose = ED_pose_object_from_context(C);
  if ((obpose->proxy != NULL) || (obpose->proxy_group != NULL) || ID_IS_OVERRIDE_LIBRARY(obpose)) {
    CTX_wm_operator_poll_msg_set(C, "Cannot edit bone groups for proxies or library overrides");
    return false;
  }

  return true;
}

static int pose_group_add_exec(bContext *C, wmOperator *UNUSED(op))
{
  Object *ob = ED_pose_object_from_context(C);

  /* only continue if there's an object and pose */
  if (ELEM(NULL, ob, ob->pose)) {
    return OPERATOR_CANCELLED;
  }

  /* for now, just call the API function for this */
  BKE_pose_add_group(ob->pose, NULL);

  /* notifiers for updates */
  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);

  return OPERATOR_FINISHED;
}

void POSE_OT_group_add(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Add Bone Group";
  ot->idname = "POSE_OT_group_add";
  ot->description = "Add a new bone group";

  /* api callbacks */
  ot->exec = pose_group_add_exec;
  ot->poll = pose_group_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static int pose_group_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
  Object *ob = ED_pose_object_from_context(C);

  /* only continue if there's an object and pose */
  if (ELEM(NULL, ob, ob->pose)) {
    return OPERATOR_CANCELLED;
  }

  /* for now, just call the API function for this */
  BKE_pose_remove_group_index(ob->pose, ob->pose->active_group);

  /* notifiers for updates */
  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
  DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);

  return OPERATOR_FINISHED;
}

void POSE_OT_group_remove(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Remove Bone Group";
  ot->idname = "POSE_OT_group_remove";
  ot->description = "Remove the active bone group";

  /* api callbacks */
  ot->exec = pose_group_remove_exec;
  ot->poll = pose_group_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

/* ------------ */

/* invoke callback which presents a list of bone-groups for the user to choose from */
static int pose_groups_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
  Object *ob = ED_pose_object_from_context(C);
  bPose *pose;
  PropertyRNA *prop = RNA_struct_find_property(op->ptr, "type");

  uiPopupMenu *pup;
  uiLayout *layout;
  bActionGroup *grp;
  int i;

  /* only continue if there's an object, and a pose there too */
  if (ELEM(NULL, ob, ob->pose)) {
    return OPERATOR_CANCELLED;
  }
  pose = ob->pose;

  /* If group index is set, try to use it! */
  if (RNA_property_is_set(op->ptr, prop)) {
    const int num_groups = BLI_listbase_count(&pose->agroups);
    const int group = RNA_property_int_get(op->ptr, prop);

    /* just use the active group index, and call the exec callback for the calling operator */
    if (group > 0 && group <= num_groups) {
      return op->type->exec(C, op);
    }
  }

  /* if there's no active group (or active is invalid), create a new menu to find it */
  if (pose->active_group <= 0) {
    /* create a new menu, and start populating it with group names */
    pup = UI_popup_menu_begin(C, op->type->name, ICON_NONE);
    layout = UI_popup_menu_layout(pup);

    /* special entry - allow creating a new group, then using that
     * (not to be used for removing though)
     */
    if (strstr(op->idname, "assign")) {
      uiItemIntO(layout, "New Group", ICON_NONE, op->idname, "type", 0);
      uiItemS(layout);
    }

    /* add entries for each group */
    for (grp = pose->agroups.first, i = 1; grp; grp = grp->next, i++) {
      uiItemIntO(layout, grp->name, ICON_NONE, op->idname, "type", i);
    }

    /* finish building the menu, and process it (should result in calling self again) */
    UI_popup_menu_end(C, pup);

    return OPERATOR_INTERFACE;
  }

  /* just use the active group index, and call the exec callback for the calling operator */
  RNA_int_set(op->ptr, "type", pose->active_group);
  return op->type->exec(C, op);
}

/* Assign selected pchans to the bone group that the user selects */
static int pose_group_assign_exec(bContext *C, wmOperator *op)
{
  Object *ob = ED_pose_object_from_context(C);
  bPose *pose;
  bool done = false;

  /* only continue if there's an object, and a pose there too */
  if (ELEM(NULL, ob, ob->pose)) {
    return OPERATOR_CANCELLED;
  }

  pose = ob->pose;

  /* set the active group number to the one from operator props
   * - if 0 after this, make a new group...
   */
  pose->active_group = RNA_int_get(op->ptr, "type");
  if (pose->active_group == 0) {
    BKE_pose_add_group(ob->pose, NULL);
  }

  /* add selected bones to group then */
  FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob, pchan) {
    pchan->agrp_index = pose->active_group;
    done = true;
  }
  FOREACH_PCHAN_SELECTED_IN_OBJECT_END;

  /* notifiers for updates */
  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
  DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);

  /* report done status */
  if (done) {
    return OPERATOR_FINISHED;
  }
  return OPERATOR_CANCELLED;
}

void POSE_OT_group_assign(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Add Selected to Bone Group";
  ot->idname = "POSE_OT_group_assign";
  ot->description = "Add selected bones to the chosen bone group";

  /* api callbacks */
  ot->invoke = pose_groups_menu_invoke;
  ot->exec = pose_group_assign_exec;
  ot->poll = pose_group_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

  /* properties */
  RNA_def_int(ot->srna, "type", 0, 0, INT_MAX, "Bone Group Index", "", 0, 10);
}

static int pose_group_unassign_exec(bContext *C, wmOperator *UNUSED(op))
{
  Object *ob = ED_pose_object_from_context(C);
  bool done = false;

  /* only continue if there's an object, and a pose there too */
  if (ELEM(NULL, ob, ob->pose)) {
    return OPERATOR_CANCELLED;
  }

  /* find selected bones to remove from all bone groups */
  FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob, pchan) {
    if (pchan->agrp_index) {
      pchan->agrp_index = 0;
      done = true;
    }
  }
  FOREACH_PCHAN_SELECTED_IN_OBJECT_END;

  /* notifiers for updates */
  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
  DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);

  /* report done status */
  if (done) {
    return OPERATOR_FINISHED;
  }
  return OPERATOR_CANCELLED;
}

void POSE_OT_group_unassign(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Remove Selected from Bone Groups";
  ot->idname = "POSE_OT_group_unassign";
  ot->description = "Remove selected bones from all bone groups";

  /* api callbacks */
  ot->exec = pose_group_unassign_exec;
  ot->poll = pose_group_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static int group_move_exec(bContext *C, wmOperator *op)
{
  Object *ob = ED_pose_object_from_context(C);
  bPose *pose = (ob) ? ob->pose : NULL;
  bPoseChannel *pchan;
  bActionGroup *grp;
  int dir = RNA_enum_get(op->ptr, "direction");

  if (ELEM(NULL, ob, pose)) {
    return OPERATOR_CANCELLED;
  }
  if (pose->active_group <= 0) {
    return OPERATOR_CANCELLED;
  }

  /* get group to move */
  grp = BLI_findlink(&pose->agroups, pose->active_group - 1);
  if (grp == NULL) {
    return OPERATOR_CANCELLED;
  }

  /* move bone group */
  if (BLI_listbase_link_move(&pose->agroups, grp, dir)) {
    int grpIndexA = pose->active_group;
    int grpIndexB = grpIndexA + dir;

    pose->active_group += dir;
    /* fix changed bone group indices in bones (swap grpIndexA with grpIndexB) */
    for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
      if (pchan->agrp_index == grpIndexB) {
        pchan->agrp_index = grpIndexA;
      }
      else if (pchan->agrp_index == grpIndexA) {
        pchan->agrp_index = grpIndexB;
      }
    }

    /* notifiers for updates */
    WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
  }

  return OPERATOR_FINISHED;
}

void POSE_OT_group_move(wmOperatorType *ot)
{
  static const EnumPropertyItem group_slot_move[] = {
      {-1, "UP", 0, "Up", ""},
      {1, "DOWN", 0, "Down", ""},
      {0, NULL, 0, NULL, NULL},
  };

  /* identifiers */
  ot->name = "Move Bone Group";
  ot->idname = "POSE_OT_group_move";
  ot->description = "Change position of active Bone Group in list of Bone Groups";

  /* api callbacks */
  ot->exec = group_move_exec;
  ot->poll = pose_group_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

  RNA_def_enum(ot->srna,
               "direction",
               group_slot_move,
               0,
               "Direction",
               "Direction to move the active Bone Group towards");
}

/* bone group sort element */
typedef struct tSortActionGroup {
  bActionGroup *agrp;
  int index;
} tSortActionGroup;

/* compare bone groups by name */
static int compare_agroup(const void *sgrp_a_ptr, const void *sgrp_b_ptr)
{
  const tSortActionGroup *sgrp_a = sgrp_a_ptr;
  const tSortActionGroup *sgrp_b = sgrp_b_ptr;

  return strcmp(sgrp_a->agrp->name, sgrp_b->agrp->name);
}

static int group_sort_exec(bContext *C, wmOperator *UNUSED(op))
{
  Object *ob = ED_pose_object_from_context(C);
  bPose *pose = (ob) ? ob->pose : NULL;
  bPoseChannel *pchan;
  tSortActionGroup *agrp_array;
  bActionGroup *agrp;

  if (ELEM(NULL, ob, pose)) {
    return OPERATOR_CANCELLED;
  }
  if (pose->active_group <= 0) {
    return OPERATOR_CANCELLED;
  }

  /* create temporary array with bone groups and indices */
  int agrp_count = BLI_listbase_count(&pose->agroups);
  agrp_array = MEM_mallocN(sizeof(tSortActionGroup) * agrp_count, "sort bone groups");
  int i;
  for (agrp = pose->agroups.first, i = 0; agrp; agrp = agrp->next, i++) {
    BLI_assert(i < agrp_count);
    agrp_array[i].agrp = agrp;
    agrp_array[i].index = i + 1;
  }

  /* sort bone groups by name */
  qsort(agrp_array, agrp_count, sizeof(tSortActionGroup), compare_agroup);

  /* create sorted bone group list from sorted array */
  BLI_listbase_clear(&pose->agroups);
  for (i = 0; i < agrp_count; i++) {
    BLI_addtail(&pose->agroups, agrp_array[i].agrp);
  }

  /* Fix changed bone group indices in bones. */
  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
    for (i = 0; i < agrp_count; i++) {
      if (pchan->agrp_index == agrp_array[i].index) {
        pchan->agrp_index = i + 1;
        break;
      }
    }
  }

  /* free temp resources */
  MEM_freeN(agrp_array);

  /* notifiers for updates */
  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
  DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);

  return OPERATOR_FINISHED;
}

void POSE_OT_group_sort(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Sort Bone Groups";
  ot->idname = "POSE_OT_group_sort";
  ot->description = "Sort Bone Groups by their names in ascending order";

  /* api callbacks */
  ot->exec = group_sort_exec;
  ot->poll = pose_group_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static void pose_group_select(Object *ob, bool select)
{
  bPose *pose = ob->pose;

  FOREACH_PCHAN_VISIBLE_IN_OBJECT_BEGIN (ob, pchan) {
    if ((pchan->bone->flag & BONE_UNSELECTABLE) == 0) {
      if (select) {
        if (pchan->agrp_index == pose->active_group) {
          pchan->bone->flag |= BONE_SELECTED;
        }
      }
      else {
        if (pchan->agrp_index == pose->active_group) {
          pchan->bone->flag &= ~BONE_SELECTED;
        }
      }
    }
  }
  FOREACH_PCHAN_VISIBLE_IN_OBJECT_END;
}

static int pose_group_select_exec(bContext *C, wmOperator *UNUSED(op))
{
  Object *ob = ED_pose_object_from_context(C);

  /* only continue if there's an object, and a pose there too */
  if (ELEM(NULL, ob, ob->pose)) {
    return OPERATOR_CANCELLED;
  }

  pose_group_select(ob, 1);

  /* notifiers for updates */
  bArmature *arm = ob->data;
  DEG_id_tag_update(&arm->id, ID_RECALC_SELECT);
  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
  ED_outliner_select_sync_from_pose_bone_tag(C);

  return OPERATOR_FINISHED;
}

void POSE_OT_group_select(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Select Bones of Bone Group";
  ot->idname = "POSE_OT_group_select";
  ot->description = "Select bones in active Bone Group";

  /* api callbacks */
  ot->exec = pose_group_select_exec;
  ot->poll = ED_operator_posemode_context;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static int pose_group_deselect_exec(bContext *C, wmOperator *UNUSED(op))
{
  Object *ob = ED_pose_object_from_context(C);

  /* only continue if there's an object, and a pose there too */
  if (ELEM(NULL, ob, ob->pose)) {
    return OPERATOR_CANCELLED;
  }

  pose_group_select(ob, 0);

  /* notifiers for updates */
  bArmature *arm = ob->data;
  DEG_id_tag_update(&arm->id, ID_RECALC_SELECT);
  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
  ED_outliner_select_sync_from_pose_bone_tag(C);

  return OPERATOR_FINISHED;
}

void POSE_OT_group_deselect(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Deselect Bone Group";
  ot->idname = "POSE_OT_group_deselect";
  ot->description = "Deselect bones of active Bone Group";

  /* api callbacks */
  ot->exec = pose_group_deselect_exec;
  ot->poll = ED_operator_posemode_context;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

/* ********************************************** */