bone_map_editor_plugin.cpp 52 KB

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