Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/3d/bone_map_editor_plugin.cpp
9903 views
1
/**************************************************************************/
2
/* bone_map_editor_plugin.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "bone_map_editor_plugin.h"
32
33
#include "editor/import/3d/post_import_plugin_skeleton_renamer.h"
34
#include "editor/import/3d/post_import_plugin_skeleton_rest_fixer.h"
35
#include "editor/import/3d/post_import_plugin_skeleton_track_organizer.h"
36
#include "editor/import/3d/scene_import_settings.h"
37
#include "editor/settings/editor_settings.h"
38
#include "editor/themes/editor_scale.h"
39
#include "editor/themes/editor_theme_manager.h"
40
#include "scene/gui/aspect_ratio_container.h"
41
#include "scene/gui/separator.h"
42
#include "scene/gui/texture_rect.h"
43
44
#include "modules/regex/regex.h"
45
46
void BoneMapperButton::fetch_textures() {
47
if (selected) {
48
set_texture_normal(get_editor_theme_icon(SNAME("BoneMapperHandleSelected")));
49
} else {
50
set_texture_normal(get_editor_theme_icon(SNAME("BoneMapperHandle")));
51
}
52
set_offset(SIDE_LEFT, 0);
53
set_offset(SIDE_RIGHT, 0);
54
set_offset(SIDE_TOP, 0);
55
set_offset(SIDE_BOTTOM, 0);
56
57
// Hack to avoid handle color darkening...
58
set_modulate(EditorThemeManager::is_dark_theme() ? Color(1, 1, 1) : Color(4.25, 4.25, 4.25));
59
60
circle = memnew(TextureRect);
61
circle->set_texture(get_editor_theme_icon(SNAME("BoneMapperHandleCircle")));
62
add_child(circle);
63
set_state(BONE_MAP_STATE_UNSET);
64
}
65
66
StringName BoneMapperButton::get_profile_bone_name() const {
67
return profile_bone_name;
68
}
69
70
void BoneMapperButton::set_state(BoneMapState p_state) {
71
switch (p_state) {
72
case BONE_MAP_STATE_UNSET: {
73
circle->set_modulate(EDITOR_GET("editors/bone_mapper/handle_colors/unset"));
74
} break;
75
case BONE_MAP_STATE_SET: {
76
circle->set_modulate(EDITOR_GET("editors/bone_mapper/handle_colors/set"));
77
} break;
78
case BONE_MAP_STATE_MISSING: {
79
circle->set_modulate(EDITOR_GET("editors/bone_mapper/handle_colors/missing"));
80
} break;
81
case BONE_MAP_STATE_ERROR: {
82
circle->set_modulate(EDITOR_GET("editors/bone_mapper/handle_colors/error"));
83
} break;
84
default: {
85
} break;
86
}
87
}
88
89
bool BoneMapperButton::is_require() const {
90
return require;
91
}
92
93
void BoneMapperButton::_notification(int p_what) {
94
switch (p_what) {
95
case NOTIFICATION_ENTER_TREE: {
96
fetch_textures();
97
} break;
98
}
99
}
100
101
BoneMapperButton::BoneMapperButton(const StringName &p_profile_bone_name, bool p_require, bool p_selected) {
102
profile_bone_name = p_profile_bone_name;
103
require = p_require;
104
selected = p_selected;
105
}
106
107
void BoneMapperItem::create_editor() {
108
HBoxContainer *hbox = memnew(HBoxContainer);
109
add_child(hbox);
110
111
skeleton_bone_selector = memnew(EditorPropertyText);
112
skeleton_bone_selector->set_label(profile_bone_name);
113
skeleton_bone_selector->set_selectable(false);
114
skeleton_bone_selector->set_h_size_flags(SIZE_EXPAND_FILL);
115
skeleton_bone_selector->set_object_and_property(bone_map.ptr(), "bone_map/" + String(profile_bone_name));
116
skeleton_bone_selector->update_property();
117
skeleton_bone_selector->connect("property_changed", callable_mp(this, &BoneMapperItem::_value_changed));
118
hbox->add_child(skeleton_bone_selector);
119
120
picker_button = memnew(Button);
121
picker_button->set_button_icon(get_editor_theme_icon(SNAME("ClassList")));
122
picker_button->connect(SceneStringName(pressed), callable_mp(this, &BoneMapperItem::_open_picker));
123
hbox->add_child(picker_button);
124
125
add_child(memnew(HSeparator));
126
}
127
128
void BoneMapperItem::_update_property() {
129
if (skeleton_bone_selector->get_edited_object() && skeleton_bone_selector->get_edited_property()) {
130
skeleton_bone_selector->update_property();
131
}
132
}
133
134
void BoneMapperItem::_open_picker() {
135
emit_signal(SNAME("pick"), profile_bone_name);
136
}
137
138
void BoneMapperItem::_value_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
139
bone_map->set(p_property, p_value);
140
}
141
142
void BoneMapperItem::_notification(int p_what) {
143
switch (p_what) {
144
case NOTIFICATION_ENTER_TREE: {
145
create_editor();
146
bone_map->connect("bone_map_updated", callable_mp(this, &BoneMapperItem::_update_property));
147
} break;
148
case NOTIFICATION_EXIT_TREE: {
149
if (bone_map.is_valid() && bone_map->is_connected("bone_map_updated", callable_mp(this, &BoneMapperItem::_update_property))) {
150
bone_map->disconnect("bone_map_updated", callable_mp(this, &BoneMapperItem::_update_property));
151
}
152
} break;
153
}
154
}
155
156
void BoneMapperItem::_bind_methods() {
157
ADD_SIGNAL(MethodInfo("pick", PropertyInfo(Variant::STRING_NAME, "profile_bone_name")));
158
}
159
160
BoneMapperItem::BoneMapperItem(Ref<BoneMap> &p_bone_map, const StringName &p_profile_bone_name) {
161
bone_map = p_bone_map;
162
profile_bone_name = p_profile_bone_name;
163
}
164
165
void BonePicker::create_editors() {
166
set_title(TTR("Bone Picker:"));
167
168
VBoxContainer *vbox = memnew(VBoxContainer);
169
add_child(vbox);
170
171
bones = memnew(Tree);
172
bones->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
173
bones->set_select_mode(Tree::SELECT_SINGLE);
174
bones->set_v_size_flags(Control::SIZE_EXPAND_FILL);
175
bones->set_hide_root(true);
176
bones->connect("item_activated", callable_mp(this, &BonePicker::_confirm));
177
vbox->add_child(bones);
178
179
create_bones_tree(skeleton);
180
}
181
182
void BonePicker::create_bones_tree(Skeleton3D *p_skeleton) {
183
bones->clear();
184
185
if (!p_skeleton) {
186
return;
187
}
188
189
TreeItem *root = bones->create_item();
190
191
HashMap<int, TreeItem *> items;
192
193
items.insert(-1, root);
194
195
Ref<Texture> bone_icon = get_editor_theme_icon(SNAME("Bone"));
196
197
Vector<int> bones_to_process = p_skeleton->get_parentless_bones();
198
bool is_first = true;
199
while (bones_to_process.size() > 0) {
200
int current_bone_idx = bones_to_process[0];
201
bones_to_process.erase(current_bone_idx);
202
203
Vector<int> current_bone_child_bones = p_skeleton->get_bone_children(current_bone_idx);
204
int child_bone_size = current_bone_child_bones.size();
205
for (int i = 0; i < child_bone_size; i++) {
206
bones_to_process.push_back(current_bone_child_bones[i]);
207
}
208
209
const int parent_idx = p_skeleton->get_bone_parent(current_bone_idx);
210
TreeItem *parent_item = items.find(parent_idx)->value;
211
212
TreeItem *joint_item = bones->create_item(parent_item);
213
items.insert(current_bone_idx, joint_item);
214
215
joint_item->set_text(0, p_skeleton->get_bone_name(current_bone_idx));
216
joint_item->set_icon(0, bone_icon);
217
joint_item->set_selectable(0, true);
218
joint_item->set_metadata(0, "bones/" + itos(current_bone_idx));
219
if (is_first) {
220
is_first = false;
221
} else {
222
joint_item->set_collapsed(true);
223
}
224
}
225
}
226
227
void BonePicker::_confirm() {
228
_ok_pressed();
229
}
230
231
void BonePicker::popup_bones_tree(const Size2i &p_minsize) {
232
popup_centered(p_minsize);
233
}
234
235
bool BonePicker::has_selected_bone() {
236
TreeItem *selected = bones->get_selected();
237
if (!selected) {
238
return false;
239
}
240
return true;
241
}
242
243
StringName BonePicker::get_selected_bone() {
244
TreeItem *selected = bones->get_selected();
245
if (!selected) {
246
return StringName();
247
}
248
return selected->get_text(0);
249
}
250
251
void BonePicker::_notification(int p_what) {
252
switch (p_what) {
253
case NOTIFICATION_ENTER_TREE: {
254
create_editors();
255
} break;
256
}
257
}
258
259
BonePicker::BonePicker(Skeleton3D *p_skeleton) {
260
skeleton = p_skeleton;
261
}
262
263
void BoneMapper::create_editor() {
264
// Create Bone picker.
265
picker = memnew(BonePicker(skeleton));
266
picker->connect(SceneStringName(confirmed), callable_mp(this, &BoneMapper::_apply_picker_selection));
267
add_child(picker, false, INTERNAL_MODE_FRONT);
268
269
profile_selector = memnew(EditorPropertyResource);
270
profile_selector->setup(bone_map.ptr(), "profile", "SkeletonProfile");
271
profile_selector->set_label("Profile");
272
profile_selector->set_selectable(false);
273
profile_selector->set_object_and_property(bone_map.ptr(), "profile");
274
profile_selector->update_property();
275
profile_selector->connect("property_changed", callable_mp(this, &BoneMapper::_profile_changed));
276
add_child(profile_selector);
277
add_child(memnew(HSeparator));
278
279
HBoxContainer *group_hbox = memnew(HBoxContainer);
280
add_child(group_hbox);
281
282
profile_group_selector = memnew(EditorPropertyEnum);
283
profile_group_selector->set_label("Group");
284
profile_group_selector->set_selectable(false);
285
profile_group_selector->set_h_size_flags(SIZE_EXPAND_FILL);
286
profile_group_selector->set_object_and_property(this, "current_group_idx");
287
profile_group_selector->update_property();
288
profile_group_selector->connect("property_changed", callable_mp(this, &BoneMapper::_value_changed));
289
group_hbox->add_child(profile_group_selector);
290
291
clear_mapping_button = memnew(Button);
292
clear_mapping_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));
293
clear_mapping_button->set_tooltip_text(TTR("Clear mappings in current group."));
294
clear_mapping_button->connect(SceneStringName(pressed), callable_mp(this, &BoneMapper::_clear_mapping_current_group));
295
group_hbox->add_child(clear_mapping_button);
296
297
bone_mapper_field = memnew(AspectRatioContainer);
298
bone_mapper_field->set_stretch_mode(AspectRatioContainer::STRETCH_FIT);
299
bone_mapper_field->set_custom_minimum_size(Vector2(0, 256.0) * EDSCALE);
300
bone_mapper_field->set_h_size_flags(Control::SIZE_FILL);
301
add_child(bone_mapper_field);
302
303
profile_bg = memnew(ColorRect);
304
profile_bg->set_color(Color(0, 0, 0, 1));
305
profile_bg->set_h_size_flags(Control::SIZE_FILL);
306
profile_bg->set_v_size_flags(Control::SIZE_FILL);
307
bone_mapper_field->add_child(profile_bg);
308
309
profile_texture = memnew(TextureRect);
310
profile_texture->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
311
profile_texture->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
312
profile_texture->set_h_size_flags(Control::SIZE_FILL);
313
profile_texture->set_v_size_flags(Control::SIZE_FILL);
314
bone_mapper_field->add_child(profile_texture);
315
316
mapper_item_vbox = memnew(VBoxContainer);
317
add_child(mapper_item_vbox);
318
319
recreate_items();
320
}
321
322
void BoneMapper::update_group_idx() {
323
if (bone_map->get_profile().is_null()) {
324
return;
325
}
326
327
PackedStringArray group_names;
328
int len = bone_map->get_profile()->get_group_size();
329
for (int i = 0; i < len; i++) {
330
group_names.push_back(bone_map->get_profile()->get_group_name(i));
331
}
332
if (current_group_idx >= len) {
333
current_group_idx = 0;
334
}
335
if (len > 0) {
336
profile_group_selector->setup(group_names);
337
profile_group_selector->update_property();
338
profile_group_selector->set_read_only(false);
339
}
340
}
341
342
void BoneMapper::_pick_bone(const StringName &p_bone_name) {
343
picker_key_name = p_bone_name;
344
picker->popup_bones_tree(Size2(500, 500) * EDSCALE);
345
}
346
347
void BoneMapper::_apply_picker_selection() {
348
if (!picker->has_selected_bone()) {
349
return;
350
}
351
bone_map->set_skeleton_bone_name(picker_key_name, picker->get_selected_bone());
352
}
353
354
void BoneMapper::set_current_group_idx(int p_group_idx) {
355
current_group_idx = p_group_idx;
356
recreate_editor();
357
}
358
359
int BoneMapper::get_current_group_idx() const {
360
return current_group_idx;
361
}
362
363
void BoneMapper::set_current_bone_idx(int p_bone_idx) {
364
current_bone_idx = p_bone_idx;
365
recreate_editor();
366
}
367
368
int BoneMapper::get_current_bone_idx() const {
369
return current_bone_idx;
370
}
371
372
void BoneMapper::recreate_editor() {
373
// Clear buttons.
374
int len = bone_mapper_buttons.size();
375
for (int i = 0; i < len; i++) {
376
profile_texture->remove_child(bone_mapper_buttons[i]);
377
memdelete(bone_mapper_buttons[i]);
378
}
379
bone_mapper_buttons.clear();
380
381
// Organize mapper items.
382
len = bone_mapper_items.size();
383
for (int i = 0; i < len; i++) {
384
bone_mapper_items[i]->set_visible(current_bone_idx == i);
385
}
386
387
Ref<SkeletonProfile> profile = bone_map->get_profile();
388
if (profile.is_valid()) {
389
SkeletonProfileHumanoid *hmn = Object::cast_to<SkeletonProfileHumanoid>(profile.ptr());
390
if (hmn) {
391
StringName hmn_group_name = profile->get_group_name(current_group_idx);
392
if (hmn_group_name == "Body") {
393
profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanBody")));
394
} else if (hmn_group_name == "Face") {
395
profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanFace")));
396
} else if (hmn_group_name == "LeftHand") {
397
profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanLeftHand")));
398
} else if (hmn_group_name == "RightHand") {
399
profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanRightHand")));
400
}
401
} else {
402
profile_texture->set_texture(profile->get_texture(current_group_idx));
403
}
404
} else {
405
profile_texture->set_texture(Ref<Texture2D>());
406
}
407
408
if (profile.is_null()) {
409
return;
410
}
411
412
for (int i = 0; i < len; i++) {
413
if (profile->get_group(i) == profile->get_group_name(current_group_idx)) {
414
BoneMapperButton *mb = memnew(BoneMapperButton(profile->get_bone_name(i), profile->is_required(i), current_bone_idx == i));
415
mb->connect(SceneStringName(pressed), callable_mp(this, &BoneMapper::set_current_bone_idx).bind(i), CONNECT_DEFERRED);
416
mb->set_h_grow_direction(GROW_DIRECTION_BOTH);
417
mb->set_v_grow_direction(GROW_DIRECTION_BOTH);
418
Vector2 vc = profile->get_handle_offset(i);
419
bone_mapper_buttons.push_back(mb);
420
profile_texture->add_child(mb);
421
mb->set_anchor(SIDE_LEFT, vc.x);
422
mb->set_anchor(SIDE_RIGHT, vc.x);
423
mb->set_anchor(SIDE_TOP, vc.y);
424
mb->set_anchor(SIDE_BOTTOM, vc.y);
425
}
426
}
427
428
_update_state();
429
}
430
431
void BoneMapper::clear_items() {
432
// Clear items.
433
int len = bone_mapper_items.size();
434
for (int i = 0; i < len; i++) {
435
bone_mapper_items[i]->disconnect("pick", callable_mp(this, &BoneMapper::_pick_bone));
436
mapper_item_vbox->remove_child(bone_mapper_items[i]);
437
memdelete(bone_mapper_items[i]);
438
}
439
bone_mapper_items.clear();
440
}
441
442
void BoneMapper::recreate_items() {
443
clear_items();
444
// Create items by profile.
445
Ref<SkeletonProfile> profile = bone_map->get_profile();
446
if (profile.is_valid()) {
447
int len = profile->get_bone_size();
448
for (int i = 0; i < len; i++) {
449
StringName bn = profile->get_bone_name(i);
450
bone_mapper_items.append(memnew(BoneMapperItem(bone_map, bn)));
451
bone_mapper_items[i]->connect("pick", callable_mp(this, &BoneMapper::_pick_bone), CONNECT_DEFERRED);
452
mapper_item_vbox->add_child(bone_mapper_items[i]);
453
}
454
}
455
456
update_group_idx();
457
recreate_editor();
458
}
459
460
void BoneMapper::_update_state() {
461
int len = bone_mapper_buttons.size();
462
for (int i = 0; i < len; i++) {
463
StringName pbn = bone_mapper_buttons[i]->get_profile_bone_name();
464
StringName sbn = bone_map->get_skeleton_bone_name(pbn);
465
int bone_idx = skeleton->find_bone(sbn);
466
if (bone_idx >= 0) {
467
if (bone_map->get_skeleton_bone_name_count(sbn) == 1) {
468
Ref<SkeletonProfile> prof = bone_map->get_profile();
469
470
StringName parent_name = prof->get_bone_parent(prof->find_bone(pbn));
471
Vector<int> prof_parent_bones;
472
while (parent_name != StringName()) {
473
prof_parent_bones.push_back(skeleton->find_bone(bone_map->get_skeleton_bone_name(parent_name)));
474
if (prof->find_bone(parent_name) == -1) {
475
break;
476
}
477
parent_name = prof->get_bone_parent(prof->find_bone(parent_name));
478
}
479
480
int parent_id = skeleton->get_bone_parent(bone_idx);
481
Vector<int> skel_parent_bones;
482
while (parent_id >= 0) {
483
skel_parent_bones.push_back(parent_id);
484
parent_id = skeleton->get_bone_parent(parent_id);
485
}
486
487
bool is_broken = false;
488
for (int j = 0; j < prof_parent_bones.size(); j++) {
489
if (prof_parent_bones[j] != -1 && !skel_parent_bones.has(prof_parent_bones[j])) {
490
is_broken = true;
491
}
492
}
493
494
if (is_broken) {
495
bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_ERROR);
496
} else {
497
bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_SET);
498
}
499
} else {
500
bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_ERROR);
501
}
502
} else {
503
if (bone_mapper_buttons[i]->is_require()) {
504
bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_MISSING);
505
} else {
506
bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_UNSET);
507
}
508
}
509
}
510
}
511
512
void BoneMapper::_clear_mapping_current_group() {
513
if (bone_map.is_valid()) {
514
Ref<SkeletonProfile> profile = bone_map->get_profile();
515
if (profile.is_valid() && profile->get_group_size() > 0) {
516
int len = profile->get_bone_size();
517
for (int i = 0; i < len; i++) {
518
if (profile->get_group(i) == profile->get_group_name(current_group_idx)) {
519
bone_map->_set_skeleton_bone_name(profile->get_bone_name(i), StringName());
520
}
521
}
522
recreate_items();
523
}
524
}
525
}
526
527
bool BoneMapper::is_match_with_bone_name(const String &p_bone_name, const String &p_word) {
528
RegEx re = RegEx(p_word);
529
return re.search(p_bone_name.to_lower()).is_valid();
530
}
531
532
int BoneMapper::search_bone_by_name(Skeleton3D *p_skeleton, const Vector<String> &p_picklist, BoneSegregation p_segregation, int p_parent, int p_child, int p_children_count) {
533
// There may be multiple candidates hit by existing the subsidiary bone.
534
// The one with the shortest name is probably the original.
535
LocalVector<String> hit_list;
536
String shortest = "";
537
538
for (int word_idx = 0; word_idx < p_picklist.size(); word_idx++) {
539
if (p_child == -1) {
540
Vector<int> bones_to_process = p_parent == -1 ? p_skeleton->get_parentless_bones() : p_skeleton->get_bone_children(p_parent);
541
while (bones_to_process.size() > 0) {
542
int idx = bones_to_process[0];
543
bones_to_process.erase(idx);
544
Vector<int> children = p_skeleton->get_bone_children(idx);
545
for (int i = 0; i < children.size(); i++) {
546
bones_to_process.push_back(children[i]);
547
}
548
549
if (p_children_count == 0 && children.size() > 0) {
550
continue;
551
}
552
if (p_children_count > 0 && children.size() < p_children_count) {
553
continue;
554
}
555
556
String bn = skeleton->get_bone_name(idx);
557
if (is_match_with_bone_name(bn, p_picklist[word_idx]) && guess_bone_segregation(bn) == p_segregation) {
558
hit_list.push_back(bn);
559
}
560
}
561
562
if (hit_list.size() > 0) {
563
shortest = hit_list[0];
564
for (const String &hit : hit_list) {
565
if (hit.length() < shortest.length()) {
566
shortest = hit; // Prioritize parent.
567
}
568
}
569
}
570
} else {
571
int idx = skeleton->get_bone_parent(p_child);
572
while (idx != p_parent && idx >= 0) {
573
Vector<int> children = p_skeleton->get_bone_children(idx);
574
if (p_children_count == 0 && children.size() > 0) {
575
continue;
576
}
577
if (p_children_count > 0 && children.size() < p_children_count) {
578
continue;
579
}
580
581
String bn = skeleton->get_bone_name(idx);
582
if (is_match_with_bone_name(bn, p_picklist[word_idx]) && guess_bone_segregation(bn) == p_segregation) {
583
hit_list.push_back(bn);
584
}
585
idx = skeleton->get_bone_parent(idx);
586
}
587
588
if (hit_list.size() > 0) {
589
shortest = hit_list[0];
590
for (const String &hit : hit_list) {
591
if (hit.length() <= shortest.length()) {
592
shortest = hit; // Prioritize parent.
593
}
594
}
595
}
596
}
597
598
if (shortest != "") {
599
break;
600
}
601
}
602
603
if (shortest == "") {
604
return -1;
605
}
606
607
return skeleton->find_bone(shortest);
608
}
609
610
BoneMapper::BoneSegregation BoneMapper::guess_bone_segregation(const String &p_bone_name) {
611
String fixed_bn = p_bone_name.to_snake_case();
612
613
LocalVector<String> left_words;
614
left_words.push_back("(?<![a-zA-Z])left");
615
left_words.push_back("(?<![a-zA-Z0-9])l(?![a-zA-Z0-9])");
616
617
LocalVector<String> right_words;
618
right_words.push_back("(?<![a-zA-Z])right");
619
right_words.push_back("(?<![a-zA-Z0-9])r(?![a-zA-Z0-9])");
620
621
for (uint32_t i = 0; i < left_words.size(); i++) {
622
RegEx re_l = RegEx(left_words[i]);
623
if (re_l.search(fixed_bn).is_valid()) {
624
return BONE_SEGREGATION_LEFT;
625
}
626
RegEx re_r = RegEx(right_words[i]);
627
if (re_r.search(fixed_bn).is_valid()) {
628
return BONE_SEGREGATION_RIGHT;
629
}
630
}
631
632
return BONE_SEGREGATION_NONE;
633
}
634
635
void BoneMapper::_run_auto_mapping() {
636
auto_mapping_process(bone_map);
637
recreate_items();
638
}
639
640
void BoneMapper::auto_mapping_process(Ref<BoneMap> &p_bone_map) {
641
WARN_PRINT("Run auto mapping.");
642
643
int bone_idx = -1;
644
Vector<String> picklist; // Use Vector<String> because match words have priority.
645
Vector<int> search_path;
646
647
// 1. Guess Hips
648
picklist.push_back("hip");
649
picklist.push_back("pelvis");
650
picklist.push_back("waist");
651
picklist.push_back("torso");
652
picklist.push_back("spine");
653
int hips = search_bone_by_name(skeleton, picklist);
654
if (hips == -1) {
655
WARN_PRINT("Auto Mapping couldn't guess Hips. Abort auto mapping.");
656
return; // If there is no Hips, we cannot guess bone after then.
657
} else {
658
p_bone_map->_set_skeleton_bone_name("Hips", skeleton->get_bone_name(hips));
659
}
660
picklist.clear();
661
662
// 2. Guess Root
663
bone_idx = skeleton->get_bone_parent(hips);
664
while (bone_idx >= 0) {
665
search_path.push_back(bone_idx);
666
bone_idx = skeleton->get_bone_parent(bone_idx);
667
}
668
if (search_path.is_empty()) {
669
bone_idx = -1;
670
} else if (search_path.size() == 1) {
671
bone_idx = search_path[0]; // It is only one bone which can be root.
672
} else {
673
bool found = false;
674
for (int i = 0; i < search_path.size(); i++) {
675
RegEx re = RegEx("root");
676
if (re.search(skeleton->get_bone_name(search_path[i]).to_lower()).is_valid()) {
677
bone_idx = search_path[i]; // Name match is preferred.
678
found = true;
679
break;
680
}
681
}
682
if (!found) {
683
for (int i = 0; i < search_path.size(); i++) {
684
if (skeleton->get_bone_global_rest(search_path[i]).origin.is_zero_approx()) {
685
bone_idx = search_path[i]; // The bone existing at the origin is appropriate as a root.
686
found = true;
687
break;
688
}
689
}
690
}
691
if (!found) {
692
bone_idx = search_path[search_path.size() - 1]; // Ambiguous, but most parental bone selected.
693
}
694
}
695
if (bone_idx == -1) {
696
WARN_PRINT("Auto Mapping couldn't guess Root."); // Root is not required, so continue.
697
} else {
698
p_bone_map->_set_skeleton_bone_name("Root", skeleton->get_bone_name(bone_idx));
699
}
700
bone_idx = -1;
701
search_path.clear();
702
703
// 3. Guess Foots
704
picklist.push_back("foot");
705
picklist.push_back("ankle");
706
int left_foot = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips);
707
if (left_foot == -1) {
708
WARN_PRINT("Auto Mapping couldn't guess LeftFoot.");
709
} else {
710
p_bone_map->_set_skeleton_bone_name("LeftFoot", skeleton->get_bone_name(left_foot));
711
}
712
int right_foot = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips);
713
if (right_foot == -1) {
714
WARN_PRINT("Auto Mapping couldn't guess RightFoot.");
715
} else {
716
p_bone_map->_set_skeleton_bone_name("RightFoot", skeleton->get_bone_name(right_foot));
717
}
718
picklist.clear();
719
720
// 3-1. Guess LowerLegs
721
picklist.push_back("(low|under).*leg");
722
picklist.push_back("knee");
723
picklist.push_back("shin");
724
picklist.push_back("calf");
725
picklist.push_back("leg");
726
int left_lower_leg = -1;
727
if (left_foot != -1) {
728
left_lower_leg = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, left_foot);
729
}
730
if (left_lower_leg == -1) {
731
WARN_PRINT("Auto Mapping couldn't guess LeftLowerLeg.");
732
} else {
733
p_bone_map->_set_skeleton_bone_name("LeftLowerLeg", skeleton->get_bone_name(left_lower_leg));
734
}
735
int right_lower_leg = -1;
736
if (right_foot != -1) {
737
right_lower_leg = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, right_foot);
738
}
739
if (right_lower_leg == -1) {
740
WARN_PRINT("Auto Mapping couldn't guess RightLowerLeg.");
741
} else {
742
p_bone_map->_set_skeleton_bone_name("RightLowerLeg", skeleton->get_bone_name(right_lower_leg));
743
}
744
picklist.clear();
745
746
// 3-2. Guess UpperLegs
747
picklist.push_back("up.*leg");
748
picklist.push_back("thigh");
749
picklist.push_back("leg");
750
if (left_lower_leg != -1) {
751
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, left_lower_leg);
752
}
753
if (bone_idx == -1) {
754
WARN_PRINT("Auto Mapping couldn't guess LeftUpperLeg.");
755
} else {
756
p_bone_map->_set_skeleton_bone_name("LeftUpperLeg", skeleton->get_bone_name(bone_idx));
757
}
758
bone_idx = -1;
759
if (right_lower_leg != -1) {
760
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, right_lower_leg);
761
}
762
if (bone_idx == -1) {
763
WARN_PRINT("Auto Mapping couldn't guess RightUpperLeg.");
764
} else {
765
p_bone_map->_set_skeleton_bone_name("RightUpperLeg", skeleton->get_bone_name(bone_idx));
766
}
767
bone_idx = -1;
768
picklist.clear();
769
770
// 3-3. Guess Toes
771
picklist.push_back("toe");
772
picklist.push_back("ball");
773
if (left_foot != -1) {
774
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_foot);
775
if (bone_idx == -1) {
776
search_path = skeleton->get_bone_children(left_foot);
777
if (search_path.size() == 1) {
778
bone_idx = search_path[0]; // Maybe only one child of the Foot is Toes.
779
}
780
search_path.clear();
781
}
782
}
783
if (bone_idx == -1) {
784
WARN_PRINT("Auto Mapping couldn't guess LeftToes.");
785
} else {
786
p_bone_map->_set_skeleton_bone_name("LeftToes", skeleton->get_bone_name(bone_idx));
787
}
788
bone_idx = -1;
789
if (right_foot != -1) {
790
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_foot);
791
if (bone_idx == -1) {
792
search_path = skeleton->get_bone_children(right_foot);
793
if (search_path.size() == 1) {
794
bone_idx = search_path[0]; // Maybe only one child of the Foot is Toes.
795
}
796
search_path.clear();
797
}
798
}
799
if (bone_idx == -1) {
800
WARN_PRINT("Auto Mapping couldn't guess RightToes.");
801
} else {
802
p_bone_map->_set_skeleton_bone_name("RightToes", skeleton->get_bone_name(bone_idx));
803
}
804
bone_idx = -1;
805
picklist.clear();
806
807
// 4. Guess Hands
808
picklist.push_back("hand");
809
picklist.push_back("wrist");
810
picklist.push_back("palm");
811
picklist.push_back("fingers");
812
int left_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, -1, 5);
813
if (left_hand_or_palm == -1) {
814
// Ambiguous, but try again for fewer finger models.
815
left_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips);
816
}
817
int left_hand = left_hand_or_palm; // Check for the presence of a wrist, since bones with five children may be palmar.
818
while (left_hand != -1) {
819
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, left_hand);
820
if (bone_idx == -1) {
821
break;
822
}
823
left_hand = bone_idx;
824
}
825
if (left_hand == -1) {
826
WARN_PRINT("Auto Mapping couldn't guess LeftHand.");
827
} else {
828
p_bone_map->_set_skeleton_bone_name("LeftHand", skeleton->get_bone_name(left_hand));
829
}
830
bone_idx = -1;
831
int right_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, -1, 5);
832
if (right_hand_or_palm == -1) {
833
// Ambiguous, but try again for fewer finger models.
834
right_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips);
835
}
836
int right_hand = right_hand_or_palm;
837
while (right_hand != -1) {
838
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, right_hand);
839
if (bone_idx == -1) {
840
break;
841
}
842
right_hand = bone_idx;
843
}
844
if (right_hand == -1) {
845
WARN_PRINT("Auto Mapping couldn't guess RightHand.");
846
} else {
847
p_bone_map->_set_skeleton_bone_name("RightHand", skeleton->get_bone_name(right_hand));
848
}
849
bone_idx = -1;
850
picklist.clear();
851
852
// 4-1. Guess Finger
853
int tips_index = -1;
854
bool thumb_tips_size = false;
855
bool named_finger_is_found = false;
856
LocalVector<String> fingers;
857
fingers.push_back("thumb|pollex");
858
fingers.push_back("index|fore");
859
fingers.push_back("middle");
860
fingers.push_back("ring");
861
fingers.push_back("little|pinkie|pinky");
862
if (left_hand_or_palm != -1) {
863
LocalVector<LocalVector<String>> left_fingers_map;
864
left_fingers_map.resize(5);
865
left_fingers_map[0].push_back("LeftThumbMetacarpal");
866
left_fingers_map[0].push_back("LeftThumbProximal");
867
left_fingers_map[0].push_back("LeftThumbDistal");
868
left_fingers_map[1].push_back("LeftIndexProximal");
869
left_fingers_map[1].push_back("LeftIndexIntermediate");
870
left_fingers_map[1].push_back("LeftIndexDistal");
871
left_fingers_map[2].push_back("LeftMiddleProximal");
872
left_fingers_map[2].push_back("LeftMiddleIntermediate");
873
left_fingers_map[2].push_back("LeftMiddleDistal");
874
left_fingers_map[3].push_back("LeftRingProximal");
875
left_fingers_map[3].push_back("LeftRingIntermediate");
876
left_fingers_map[3].push_back("LeftRingDistal");
877
left_fingers_map[4].push_back("LeftLittleProximal");
878
left_fingers_map[4].push_back("LeftLittleIntermediate");
879
left_fingers_map[4].push_back("LeftLittleDistal");
880
for (int i = 0; i < 5; i++) {
881
picklist.push_back(fingers[i]);
882
int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_hand_or_palm, -1, 0);
883
if (finger != -1) {
884
while (finger != left_hand_or_palm && finger >= 0) {
885
search_path.push_back(finger);
886
finger = skeleton->get_bone_parent(finger);
887
}
888
// Tips detection by name matching with "distal" from root.
889
for (int j = search_path.size() - 1; j >= 0; j--) {
890
if (RegEx("distal").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
891
tips_index = j - 1;
892
break;
893
}
894
}
895
// Tips detection by name matching with "tip|leaf" from end.
896
if (tips_index < 0) {
897
for (int j = 0; j < search_path.size(); j++) {
898
if (RegEx("tip|leaf").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
899
tips_index = j;
900
break;
901
}
902
}
903
}
904
// Tips detection by thumb children size.
905
if (tips_index < 0) {
906
if (i == 0) {
907
thumb_tips_size = MAX(0, search_path.size() - 3);
908
}
909
tips_index = thumb_tips_size - 1;
910
}
911
// Remove tips.
912
for (int j = 0; j <= tips_index; j++) {
913
search_path.remove_at(0);
914
}
915
search_path.reverse();
916
if (search_path.size() == 1) {
917
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
918
named_finger_is_found = true;
919
} else if (search_path.size() == 2) {
920
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
921
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
922
named_finger_is_found = true;
923
} else if (search_path.size() >= 3) {
924
search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
925
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
926
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
927
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
928
named_finger_is_found = true;
929
}
930
}
931
picklist.clear();
932
search_path.clear();
933
}
934
935
// It is a bit corner case, but possibly the finger names are sequentially numbered...
936
if (!named_finger_is_found) {
937
picklist.push_back("finger");
938
RegEx finger_re = RegEx("finger");
939
search_path = skeleton->get_bone_children(left_hand_or_palm);
940
Vector<String> finger_names;
941
for (int i = 0; i < search_path.size(); i++) {
942
String bn = skeleton->get_bone_name(search_path[i]);
943
if (finger_re.search(bn.to_lower()).is_valid()) {
944
finger_names.push_back(bn);
945
}
946
}
947
finger_names.sort(); // Order by lexicographic, normal use cases never have more than 10 fingers in one hand.
948
search_path.clear();
949
for (int i = 0; i < finger_names.size(); i++) {
950
if (i >= 5) {
951
break;
952
}
953
int finger_root = skeleton->find_bone(finger_names[i]);
954
int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, finger_root, -1, 0);
955
if (finger != -1) {
956
while (finger != finger_root && finger >= 0) {
957
search_path.push_back(finger);
958
finger = skeleton->get_bone_parent(finger);
959
}
960
}
961
search_path.push_back(finger_root);
962
// Tips detection by thumb children size.
963
if (i == 0) {
964
thumb_tips_size = MAX(0, search_path.size() - 3);
965
}
966
tips_index = thumb_tips_size - 1;
967
for (int j = 0; j <= tips_index; j++) {
968
search_path.remove_at(0);
969
}
970
search_path.reverse();
971
if (search_path.size() == 1) {
972
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
973
} else if (search_path.size() == 2) {
974
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
975
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
976
} else if (search_path.size() >= 3) {
977
search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
978
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
979
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
980
p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
981
}
982
search_path.clear();
983
}
984
picklist.clear();
985
}
986
}
987
988
tips_index = -1;
989
thumb_tips_size = false;
990
named_finger_is_found = false;
991
if (right_hand_or_palm != -1) {
992
LocalVector<LocalVector<String>> right_fingers_map;
993
right_fingers_map.resize(5);
994
right_fingers_map[0].push_back("RightThumbMetacarpal");
995
right_fingers_map[0].push_back("RightThumbProximal");
996
right_fingers_map[0].push_back("RightThumbDistal");
997
right_fingers_map[1].push_back("RightIndexProximal");
998
right_fingers_map[1].push_back("RightIndexIntermediate");
999
right_fingers_map[1].push_back("RightIndexDistal");
1000
right_fingers_map[2].push_back("RightMiddleProximal");
1001
right_fingers_map[2].push_back("RightMiddleIntermediate");
1002
right_fingers_map[2].push_back("RightMiddleDistal");
1003
right_fingers_map[3].push_back("RightRingProximal");
1004
right_fingers_map[3].push_back("RightRingIntermediate");
1005
right_fingers_map[3].push_back("RightRingDistal");
1006
right_fingers_map[4].push_back("RightLittleProximal");
1007
right_fingers_map[4].push_back("RightLittleIntermediate");
1008
right_fingers_map[4].push_back("RightLittleDistal");
1009
for (int i = 0; i < 5; i++) {
1010
picklist.push_back(fingers[i]);
1011
int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_hand_or_palm, -1, 0);
1012
if (finger != -1) {
1013
while (finger != right_hand_or_palm && finger >= 0) {
1014
search_path.push_back(finger);
1015
finger = skeleton->get_bone_parent(finger);
1016
}
1017
// Tips detection by name matching with "distal" from root.
1018
for (int j = search_path.size() - 1; j >= 0; j--) {
1019
if (RegEx("distal").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
1020
tips_index = j - 1;
1021
break;
1022
}
1023
}
1024
// Tips detection by name matching with "tip|leaf" from end.
1025
if (tips_index < 0) {
1026
for (int j = 0; j < search_path.size(); j++) {
1027
if (RegEx("tip|leaf").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
1028
tips_index = j;
1029
break;
1030
}
1031
}
1032
}
1033
// Tips detection by thumb children size.
1034
if (tips_index < 0) {
1035
if (i == 0) {
1036
thumb_tips_size = MAX(0, search_path.size() - 3);
1037
}
1038
tips_index = thumb_tips_size - 1;
1039
}
1040
// Remove tips.
1041
for (int j = 0; j <= tips_index; j++) {
1042
search_path.remove_at(0);
1043
}
1044
search_path.reverse();
1045
if (search_path.size() == 1) {
1046
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
1047
named_finger_is_found = true;
1048
} else if (search_path.size() == 2) {
1049
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
1050
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
1051
named_finger_is_found = true;
1052
} else if (search_path.size() >= 3) {
1053
search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
1054
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
1055
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
1056
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
1057
named_finger_is_found = true;
1058
}
1059
}
1060
picklist.clear();
1061
search_path.clear();
1062
}
1063
1064
// It is a bit corner case, but possibly the finger names are sequentially numbered...
1065
if (!named_finger_is_found) {
1066
picklist.push_back("finger");
1067
RegEx finger_re = RegEx("finger");
1068
search_path = skeleton->get_bone_children(right_hand_or_palm);
1069
Vector<String> finger_names;
1070
for (int i = 0; i < search_path.size(); i++) {
1071
String bn = skeleton->get_bone_name(search_path[i]);
1072
if (finger_re.search(bn.to_lower()).is_valid()) {
1073
finger_names.push_back(bn);
1074
}
1075
}
1076
finger_names.sort(); // Order by lexicographic, normal use cases never have more than 10 fingers in one hand.
1077
search_path.clear();
1078
for (int i = 0; i < finger_names.size(); i++) {
1079
if (i >= 5) {
1080
break;
1081
}
1082
int finger_root = skeleton->find_bone(finger_names[i]);
1083
int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, finger_root, -1, 0);
1084
if (finger != -1) {
1085
while (finger != finger_root && finger >= 0) {
1086
search_path.push_back(finger);
1087
finger = skeleton->get_bone_parent(finger);
1088
}
1089
}
1090
search_path.push_back(finger_root);
1091
// Tips detection by thumb children size.
1092
if (i == 0) {
1093
thumb_tips_size = MAX(0, search_path.size() - 3);
1094
}
1095
tips_index = thumb_tips_size - 1;
1096
for (int j = 0; j <= tips_index; j++) {
1097
search_path.remove_at(0);
1098
}
1099
search_path.reverse();
1100
if (search_path.size() == 1) {
1101
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
1102
} else if (search_path.size() == 2) {
1103
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
1104
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
1105
} else if (search_path.size() >= 3) {
1106
search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
1107
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
1108
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
1109
p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
1110
}
1111
search_path.clear();
1112
}
1113
picklist.clear();
1114
}
1115
}
1116
1117
// 5. Guess Arms
1118
picklist.push_back("shoulder");
1119
picklist.push_back("clavicle");
1120
picklist.push_back("collar");
1121
int left_shoulder = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips);
1122
if (left_shoulder == -1) {
1123
WARN_PRINT("Auto Mapping couldn't guess LeftShoulder.");
1124
} else {
1125
p_bone_map->_set_skeleton_bone_name("LeftShoulder", skeleton->get_bone_name(left_shoulder));
1126
}
1127
int right_shoulder = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips);
1128
if (right_shoulder == -1) {
1129
WARN_PRINT("Auto Mapping couldn't guess RightShoulder.");
1130
} else {
1131
p_bone_map->_set_skeleton_bone_name("RightShoulder", skeleton->get_bone_name(right_shoulder));
1132
}
1133
picklist.clear();
1134
1135
// 5-1. Guess LowerArms
1136
picklist.push_back("(low|fore).*arm");
1137
picklist.push_back("elbow");
1138
picklist.push_back("arm");
1139
int left_lower_arm = -1;
1140
if (left_shoulder != -1 && left_hand_or_palm != -1) {
1141
left_lower_arm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_shoulder, left_hand_or_palm);
1142
}
1143
if (left_lower_arm == -1) {
1144
WARN_PRINT("Auto Mapping couldn't guess LeftLowerArm.");
1145
} else {
1146
p_bone_map->_set_skeleton_bone_name("LeftLowerArm", skeleton->get_bone_name(left_lower_arm));
1147
}
1148
int right_lower_arm = -1;
1149
if (right_shoulder != -1 && right_hand_or_palm != -1) {
1150
right_lower_arm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_shoulder, right_hand_or_palm);
1151
}
1152
if (right_lower_arm == -1) {
1153
WARN_PRINT("Auto Mapping couldn't guess RightLowerArm.");
1154
} else {
1155
p_bone_map->_set_skeleton_bone_name("RightLowerArm", skeleton->get_bone_name(right_lower_arm));
1156
}
1157
picklist.clear();
1158
1159
// 5-2. Guess UpperArms
1160
picklist.push_back("up.*arm");
1161
picklist.push_back("arm");
1162
if (left_shoulder != -1 && left_lower_arm != -1) {
1163
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_shoulder, left_lower_arm);
1164
}
1165
if (bone_idx == -1) {
1166
WARN_PRINT("Auto Mapping couldn't guess LeftUpperArm.");
1167
} else {
1168
p_bone_map->_set_skeleton_bone_name("LeftUpperArm", skeleton->get_bone_name(bone_idx));
1169
}
1170
bone_idx = -1;
1171
if (right_shoulder != -1 && right_lower_arm != -1) {
1172
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_shoulder, right_lower_arm);
1173
}
1174
if (bone_idx == -1) {
1175
WARN_PRINT("Auto Mapping couldn't guess RightUpperArm.");
1176
} else {
1177
p_bone_map->_set_skeleton_bone_name("RightUpperArm", skeleton->get_bone_name(bone_idx));
1178
}
1179
bone_idx = -1;
1180
picklist.clear();
1181
1182
// 6. Guess Neck
1183
picklist.push_back("neck");
1184
picklist.push_back("head"); // For no neck model.
1185
picklist.push_back("face"); // Same above.
1186
int neck = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_NONE, hips);
1187
picklist.clear();
1188
if (neck == -1) {
1189
// If it can't expect by name, search child spine of where the right and left shoulders (or hands) cross.
1190
int ls_idx = left_shoulder != -1 ? left_shoulder : (left_hand_or_palm != -1 ? left_hand_or_palm : -1);
1191
int rs_idx = right_shoulder != -1 ? right_shoulder : (right_hand_or_palm != -1 ? right_hand_or_palm : -1);
1192
if (ls_idx != -1 && rs_idx != -1) {
1193
bool detect = false;
1194
while (ls_idx != hips && ls_idx >= 0 && rs_idx != hips && rs_idx >= 0) {
1195
ls_idx = skeleton->get_bone_parent(ls_idx);
1196
rs_idx = skeleton->get_bone_parent(rs_idx);
1197
if (ls_idx == rs_idx) {
1198
detect = true;
1199
break;
1200
}
1201
}
1202
if (detect) {
1203
Vector<int> children = skeleton->get_bone_children(ls_idx);
1204
children.erase(ls_idx);
1205
children.erase(rs_idx);
1206
String word = "spine"; // It would be better to limit the search with "spine" because it could be mistaken with breast, wing and etc...
1207
for (int i = 0; i < children.size(); i++) {
1208
bone_idx = children[i];
1209
if (is_match_with_bone_name(skeleton->get_bone_name(bone_idx), word)) {
1210
neck = bone_idx;
1211
break;
1212
};
1213
}
1214
bone_idx = -1;
1215
}
1216
}
1217
}
1218
1219
// 7. Guess Head
1220
picklist.push_back("head");
1221
picklist.push_back("face");
1222
int head = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_NONE, neck);
1223
if (head == -1) {
1224
if (neck != -1) {
1225
search_path = skeleton->get_bone_children(neck);
1226
if (search_path.size() == 1) {
1227
head = search_path[0]; // Maybe only one child of the Neck is Head.
1228
}
1229
}
1230
}
1231
if (head == -1) {
1232
if (neck != -1) {
1233
head = neck; // The head animation should have more movement.
1234
neck = -1;
1235
p_bone_map->_set_skeleton_bone_name("Head", skeleton->get_bone_name(head));
1236
} else {
1237
WARN_PRINT("Auto Mapping couldn't guess Neck or Head."); // Continued for guessing on the other bones. But abort when guessing spines step.
1238
}
1239
} else {
1240
p_bone_map->_set_skeleton_bone_name("Neck", skeleton->get_bone_name(neck));
1241
p_bone_map->_set_skeleton_bone_name("Head", skeleton->get_bone_name(head));
1242
}
1243
picklist.clear();
1244
search_path.clear();
1245
1246
int neck_or_head = neck != -1 ? neck : (head != -1 ? head : -1);
1247
if (neck_or_head != -1) {
1248
// 7-1. Guess Eyes
1249
picklist.push_back("eye(?!.*(brow|lash|lid))");
1250
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, neck_or_head);
1251
if (bone_idx == -1) {
1252
WARN_PRINT("Auto Mapping couldn't guess LeftEye.");
1253
} else {
1254
p_bone_map->_set_skeleton_bone_name("LeftEye", skeleton->get_bone_name(bone_idx));
1255
}
1256
1257
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, neck_or_head);
1258
if (bone_idx == -1) {
1259
WARN_PRINT("Auto Mapping couldn't guess RightEye.");
1260
} else {
1261
p_bone_map->_set_skeleton_bone_name("RightEye", skeleton->get_bone_name(bone_idx));
1262
}
1263
picklist.clear();
1264
1265
// 7-2. Guess Jaw
1266
picklist.push_back("jaw");
1267
bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_NONE, neck_or_head);
1268
if (bone_idx == -1) {
1269
WARN_PRINT("Auto Mapping couldn't guess Jaw.");
1270
} else {
1271
p_bone_map->_set_skeleton_bone_name("Jaw", skeleton->get_bone_name(bone_idx));
1272
}
1273
bone_idx = -1;
1274
picklist.clear();
1275
}
1276
1277
// 8. Guess UpperChest or Chest
1278
if (neck_or_head == -1) {
1279
return; // Abort.
1280
}
1281
int chest_or_upper_chest = skeleton->get_bone_parent(neck_or_head);
1282
bool is_appropriate = true;
1283
if (left_shoulder != -1) {
1284
bone_idx = skeleton->get_bone_parent(left_shoulder);
1285
bool detect = false;
1286
while (bone_idx != hips && bone_idx >= 0) {
1287
if (bone_idx == chest_or_upper_chest) {
1288
detect = true;
1289
break;
1290
}
1291
bone_idx = skeleton->get_bone_parent(bone_idx);
1292
}
1293
if (!detect) {
1294
is_appropriate = false;
1295
}
1296
bone_idx = -1;
1297
}
1298
if (right_shoulder != -1) {
1299
bone_idx = skeleton->get_bone_parent(right_shoulder);
1300
bool detect = false;
1301
while (bone_idx != hips && bone_idx >= 0) {
1302
if (bone_idx == chest_or_upper_chest) {
1303
detect = true;
1304
break;
1305
}
1306
bone_idx = skeleton->get_bone_parent(bone_idx);
1307
}
1308
if (!detect) {
1309
is_appropriate = false;
1310
}
1311
bone_idx = -1;
1312
}
1313
if (!is_appropriate) {
1314
if (skeleton->get_bone_parent(left_shoulder) == skeleton->get_bone_parent(right_shoulder)) {
1315
chest_or_upper_chest = skeleton->get_bone_parent(left_shoulder);
1316
} else {
1317
chest_or_upper_chest = -1;
1318
}
1319
}
1320
if (chest_or_upper_chest == -1) {
1321
WARN_PRINT("Auto Mapping couldn't guess Chest or UpperChest. Abort auto mapping.");
1322
return; // Will be not able to guess Spines.
1323
}
1324
1325
// 9. Guess Spines
1326
bone_idx = skeleton->get_bone_parent(chest_or_upper_chest);
1327
while (bone_idx != hips && bone_idx >= 0) {
1328
search_path.push_back(bone_idx);
1329
bone_idx = skeleton->get_bone_parent(bone_idx);
1330
}
1331
search_path.reverse();
1332
if (search_path.is_empty()) {
1333
p_bone_map->_set_skeleton_bone_name("Spine", skeleton->get_bone_name(chest_or_upper_chest)); // Maybe chibi model...?
1334
} else if (search_path.size() == 1) {
1335
p_bone_map->_set_skeleton_bone_name("Spine", skeleton->get_bone_name(search_path[0]));
1336
p_bone_map->_set_skeleton_bone_name("Chest", skeleton->get_bone_name(chest_or_upper_chest));
1337
} else if (search_path.size() >= 2) {
1338
p_bone_map->_set_skeleton_bone_name("Spine", skeleton->get_bone_name(search_path[0]));
1339
p_bone_map->_set_skeleton_bone_name("Chest", skeleton->get_bone_name(search_path[search_path.size() - 1])); // Probably UppeChest's parent is appropriate.
1340
p_bone_map->_set_skeleton_bone_name("UpperChest", skeleton->get_bone_name(chest_or_upper_chest));
1341
}
1342
bone_idx = -1;
1343
search_path.clear();
1344
1345
WARN_PRINT("Finish auto mapping.");
1346
}
1347
1348
void BoneMapper::_value_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
1349
set(p_property, p_value);
1350
recreate_editor();
1351
}
1352
1353
void BoneMapper::_profile_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
1354
bone_map->set(p_property, p_value);
1355
1356
// Run auto mapping when setting SkeletonProfileHumanoid by GUI Editor.
1357
Ref<SkeletonProfile> profile = bone_map->get_profile();
1358
if (profile.is_valid()) {
1359
SkeletonProfileHumanoid *hmn = Object::cast_to<SkeletonProfileHumanoid>(profile.ptr());
1360
if (hmn) {
1361
_run_auto_mapping();
1362
}
1363
}
1364
}
1365
1366
void BoneMapper::_bind_methods() {
1367
ClassDB::bind_method(D_METHOD("set_current_group_idx", "current_group_idx"), &BoneMapper::set_current_group_idx);
1368
ClassDB::bind_method(D_METHOD("get_current_group_idx"), &BoneMapper::get_current_group_idx);
1369
ClassDB::bind_method(D_METHOD("set_current_bone_idx", "current_bone_idx"), &BoneMapper::set_current_bone_idx);
1370
ClassDB::bind_method(D_METHOD("get_current_bone_idx"), &BoneMapper::get_current_bone_idx);
1371
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_group_idx"), "set_current_group_idx", "get_current_group_idx");
1372
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_bone_idx"), "set_current_bone_idx", "get_current_bone_idx");
1373
}
1374
1375
void BoneMapper::_notification(int p_what) {
1376
switch (p_what) {
1377
case NOTIFICATION_ENTER_TREE: {
1378
create_editor();
1379
bone_map->connect("bone_map_updated", callable_mp(this, &BoneMapper::_update_state));
1380
bone_map->connect("profile_updated", callable_mp(this, &BoneMapper::recreate_items));
1381
} break;
1382
case NOTIFICATION_EXIT_TREE: {
1383
clear_items();
1384
if (bone_map.is_valid()) {
1385
if (bone_map->is_connected("bone_map_updated", callable_mp(this, &BoneMapper::_update_state))) {
1386
bone_map->disconnect("bone_map_updated", callable_mp(this, &BoneMapper::_update_state));
1387
}
1388
if (bone_map->is_connected("profile_updated", callable_mp(this, &BoneMapper::recreate_items))) {
1389
bone_map->disconnect("profile_updated", callable_mp(this, &BoneMapper::recreate_items));
1390
}
1391
}
1392
}
1393
}
1394
}
1395
1396
BoneMapper::BoneMapper(Skeleton3D *p_skeleton, Ref<BoneMap> &p_bone_map) {
1397
skeleton = p_skeleton;
1398
bone_map = p_bone_map;
1399
}
1400
1401
void BoneMapEditor::create_editors() {
1402
if (!skeleton) {
1403
return;
1404
}
1405
bone_mapper = memnew(BoneMapper(skeleton, bone_map));
1406
add_child(bone_mapper);
1407
}
1408
1409
void BoneMapEditor::fetch_objects() {
1410
skeleton = nullptr;
1411
// Hackey... but it may be the easiest way to get a selected object from "ImporterScene".
1412
SceneImportSettingsDialog *si = SceneImportSettingsDialog::get_singleton();
1413
if (!si) {
1414
return;
1415
}
1416
if (!si->is_visible()) {
1417
return;
1418
}
1419
Node *selected = si->get_selected_node();
1420
if (selected) {
1421
Skeleton3D *sk = Object::cast_to<Skeleton3D>(selected);
1422
if (!sk) {
1423
return;
1424
}
1425
skeleton = sk;
1426
} else {
1427
// Editor should not exist.
1428
skeleton = nullptr;
1429
}
1430
}
1431
1432
void BoneMapEditor::_notification(int p_what) {
1433
switch (p_what) {
1434
case NOTIFICATION_ENTER_TREE: {
1435
fetch_objects();
1436
create_editors();
1437
} break;
1438
case NOTIFICATION_EXIT_TREE: {
1439
skeleton = nullptr;
1440
} break;
1441
}
1442
}
1443
1444
BoneMapEditor::BoneMapEditor(Ref<BoneMap> &p_bone_map) {
1445
bone_map = p_bone_map;
1446
}
1447
1448
bool EditorInspectorPluginBoneMap::can_handle(Object *p_object) {
1449
return Object::cast_to<BoneMap>(p_object) != nullptr;
1450
}
1451
1452
void EditorInspectorPluginBoneMap::parse_begin(Object *p_object) {
1453
BoneMap *bm = Object::cast_to<BoneMap>(p_object);
1454
if (!bm) {
1455
return;
1456
}
1457
Ref<BoneMap> r(bm);
1458
editor = memnew(BoneMapEditor(r));
1459
add_custom_control(editor);
1460
}
1461
1462
BoneMapEditorPlugin::BoneMapEditorPlugin() {
1463
Ref<EditorInspectorPluginBoneMap> inspector_plugin;
1464
inspector_plugin.instantiate();
1465
add_inspector_plugin(inspector_plugin);
1466
1467
Ref<PostImportPluginSkeletonTrackOrganizer> post_import_plugin_track_organizer;
1468
post_import_plugin_track_organizer.instantiate();
1469
add_scene_post_import_plugin(post_import_plugin_track_organizer);
1470
1471
Ref<PostImportPluginSkeletonRenamer> post_import_plugin_renamer;
1472
post_import_plugin_renamer.instantiate();
1473
add_scene_post_import_plugin(post_import_plugin_renamer);
1474
1475
Ref<PostImportPluginSkeletonRestFixer> post_import_plugin_rest_fixer;
1476
post_import_plugin_rest_fixer.instantiate();
1477
add_scene_post_import_plugin(post_import_plugin_rest_fixer);
1478
}
1479
1480