bone_map_editor_plugin.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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_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::_bind_methods() {
  214. }
  215. void BonePicker::_notification(int p_what) {
  216. switch (p_what) {
  217. case NOTIFICATION_ENTER_TREE: {
  218. create_editors();
  219. } break;
  220. }
  221. }
  222. BonePicker::BonePicker(Skeleton3D *p_skeleton) {
  223. skeleton = p_skeleton;
  224. }
  225. BonePicker::~BonePicker() {
  226. }
  227. void BoneMapper::create_editor() {
  228. // Create Bone picker.
  229. picker = memnew(BonePicker(skeleton));
  230. picker->connect("confirmed", callable_mp(this, &BoneMapper::_apply_picker_selection));
  231. add_child(picker, false, INTERNAL_MODE_FRONT);
  232. profile_selector = memnew(EditorPropertyResource);
  233. profile_selector->setup(bone_map.ptr(), "profile", "SkeletonProfile");
  234. profile_selector->set_label("Profile");
  235. profile_selector->set_selectable(false);
  236. profile_selector->set_object_and_property(bone_map.ptr(), "profile");
  237. profile_selector->update_property();
  238. profile_selector->connect("property_changed", callable_mp(this, &BoneMapper::_profile_changed));
  239. add_child(profile_selector);
  240. add_child(memnew(HSeparator));
  241. HBoxContainer *group_hbox = memnew(HBoxContainer);
  242. add_child(group_hbox);
  243. profile_group_selector = memnew(EditorPropertyEnum);
  244. profile_group_selector->set_label("Group");
  245. profile_group_selector->set_selectable(false);
  246. profile_group_selector->set_h_size_flags(SIZE_EXPAND_FILL);
  247. profile_group_selector->set_object_and_property(this, "current_group_idx");
  248. profile_group_selector->update_property();
  249. profile_group_selector->connect("property_changed", callable_mp(this, &BoneMapper::_value_changed));
  250. group_hbox->add_child(profile_group_selector);
  251. clear_mapping_button = memnew(Button);
  252. clear_mapping_button->set_icon(get_editor_theme_icon(SNAME("Clear")));
  253. clear_mapping_button->set_tooltip_text(TTR("Clear mappings in current group."));
  254. clear_mapping_button->connect(SceneStringName(pressed), callable_mp(this, &BoneMapper::_clear_mapping_current_group));
  255. group_hbox->add_child(clear_mapping_button);
  256. bone_mapper_field = memnew(AspectRatioContainer);
  257. bone_mapper_field->set_stretch_mode(AspectRatioContainer::STRETCH_FIT);
  258. bone_mapper_field->set_custom_minimum_size(Vector2(0, 256.0) * EDSCALE);
  259. bone_mapper_field->set_h_size_flags(Control::SIZE_FILL);
  260. add_child(bone_mapper_field);
  261. profile_bg = memnew(ColorRect);
  262. profile_bg->set_color(Color(0, 0, 0, 1));
  263. profile_bg->set_h_size_flags(Control::SIZE_FILL);
  264. profile_bg->set_v_size_flags(Control::SIZE_FILL);
  265. bone_mapper_field->add_child(profile_bg);
  266. profile_texture = memnew(TextureRect);
  267. profile_texture->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  268. profile_texture->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  269. profile_texture->set_h_size_flags(Control::SIZE_FILL);
  270. profile_texture->set_v_size_flags(Control::SIZE_FILL);
  271. bone_mapper_field->add_child(profile_texture);
  272. mapper_item_vbox = memnew(VBoxContainer);
  273. add_child(mapper_item_vbox);
  274. recreate_items();
  275. }
  276. void BoneMapper::update_group_idx() {
  277. if (!bone_map->get_profile().is_valid()) {
  278. return;
  279. }
  280. PackedStringArray group_names;
  281. int len = bone_map->get_profile()->get_group_size();
  282. for (int i = 0; i < len; i++) {
  283. group_names.push_back(bone_map->get_profile()->get_group_name(i));
  284. }
  285. if (current_group_idx >= len) {
  286. current_group_idx = 0;
  287. }
  288. if (len > 0) {
  289. profile_group_selector->setup(group_names);
  290. profile_group_selector->update_property();
  291. profile_group_selector->set_read_only(false);
  292. }
  293. }
  294. void BoneMapper::_pick_bone(const StringName &p_bone_name) {
  295. picker_key_name = p_bone_name;
  296. picker->popup_bones_tree(Size2(500, 500) * EDSCALE);
  297. }
  298. void BoneMapper::_apply_picker_selection() {
  299. if (!picker->has_selected_bone()) {
  300. return;
  301. }
  302. bone_map->set_skeleton_bone_name(picker_key_name, picker->get_selected_bone());
  303. }
  304. void BoneMapper::set_current_group_idx(int p_group_idx) {
  305. current_group_idx = p_group_idx;
  306. recreate_editor();
  307. }
  308. int BoneMapper::get_current_group_idx() const {
  309. return current_group_idx;
  310. }
  311. void BoneMapper::set_current_bone_idx(int p_bone_idx) {
  312. current_bone_idx = p_bone_idx;
  313. recreate_editor();
  314. }
  315. int BoneMapper::get_current_bone_idx() const {
  316. return current_bone_idx;
  317. }
  318. void BoneMapper::recreate_editor() {
  319. // Clear buttons.
  320. int len = bone_mapper_buttons.size();
  321. for (int i = 0; i < len; i++) {
  322. profile_texture->remove_child(bone_mapper_buttons[i]);
  323. memdelete(bone_mapper_buttons[i]);
  324. }
  325. bone_mapper_buttons.clear();
  326. // Organize mapper items.
  327. len = bone_mapper_items.size();
  328. for (int i = 0; i < len; i++) {
  329. bone_mapper_items[i]->set_visible(current_bone_idx == i);
  330. }
  331. Ref<SkeletonProfile> profile = bone_map->get_profile();
  332. if (profile.is_valid()) {
  333. SkeletonProfileHumanoid *hmn = Object::cast_to<SkeletonProfileHumanoid>(profile.ptr());
  334. if (hmn) {
  335. StringName hmn_group_name = profile->get_group_name(current_group_idx);
  336. if (hmn_group_name == "Body") {
  337. profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanBody")));
  338. } else if (hmn_group_name == "Face") {
  339. profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanFace")));
  340. } else if (hmn_group_name == "LeftHand") {
  341. profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanLeftHand")));
  342. } else if (hmn_group_name == "RightHand") {
  343. profile_texture->set_texture(get_editor_theme_icon(SNAME("BoneMapHumanRightHand")));
  344. }
  345. } else {
  346. profile_texture->set_texture(profile->get_texture(current_group_idx));
  347. }
  348. } else {
  349. profile_texture->set_texture(Ref<Texture2D>());
  350. }
  351. if (!profile.is_valid()) {
  352. return;
  353. }
  354. for (int i = 0; i < len; i++) {
  355. if (profile->get_group(i) == profile->get_group_name(current_group_idx)) {
  356. BoneMapperButton *mb = memnew(BoneMapperButton(profile->get_bone_name(i), profile->is_required(i), current_bone_idx == i));
  357. mb->connect(SceneStringName(pressed), callable_mp(this, &BoneMapper::set_current_bone_idx).bind(i), CONNECT_DEFERRED);
  358. mb->set_h_grow_direction(GROW_DIRECTION_BOTH);
  359. mb->set_v_grow_direction(GROW_DIRECTION_BOTH);
  360. Vector2 vc = profile->get_handle_offset(i);
  361. bone_mapper_buttons.push_back(mb);
  362. profile_texture->add_child(mb);
  363. mb->set_anchor(SIDE_LEFT, vc.x);
  364. mb->set_anchor(SIDE_RIGHT, vc.x);
  365. mb->set_anchor(SIDE_TOP, vc.y);
  366. mb->set_anchor(SIDE_BOTTOM, vc.y);
  367. }
  368. }
  369. _update_state();
  370. }
  371. void BoneMapper::clear_items() {
  372. // Clear items.
  373. int len = bone_mapper_items.size();
  374. for (int i = 0; i < len; i++) {
  375. bone_mapper_items[i]->disconnect("pick", callable_mp(this, &BoneMapper::_pick_bone));
  376. mapper_item_vbox->remove_child(bone_mapper_items[i]);
  377. memdelete(bone_mapper_items[i]);
  378. }
  379. bone_mapper_items.clear();
  380. }
  381. void BoneMapper::recreate_items() {
  382. clear_items();
  383. // Create items by profile.
  384. Ref<SkeletonProfile> profile = bone_map->get_profile();
  385. if (profile.is_valid()) {
  386. int len = profile->get_bone_size();
  387. for (int i = 0; i < len; i++) {
  388. StringName bn = profile->get_bone_name(i);
  389. bone_mapper_items.append(memnew(BoneMapperItem(bone_map, bn)));
  390. bone_mapper_items[i]->connect("pick", callable_mp(this, &BoneMapper::_pick_bone), CONNECT_DEFERRED);
  391. mapper_item_vbox->add_child(bone_mapper_items[i]);
  392. }
  393. }
  394. update_group_idx();
  395. recreate_editor();
  396. }
  397. void BoneMapper::_update_state() {
  398. int len = bone_mapper_buttons.size();
  399. for (int i = 0; i < len; i++) {
  400. StringName pbn = bone_mapper_buttons[i]->get_profile_bone_name();
  401. StringName sbn = bone_map->get_skeleton_bone_name(pbn);
  402. int bone_idx = skeleton->find_bone(sbn);
  403. if (bone_idx >= 0) {
  404. if (bone_map->get_skeleton_bone_name_count(sbn) == 1) {
  405. Ref<SkeletonProfile> prof = bone_map->get_profile();
  406. StringName parent_name = prof->get_bone_parent(prof->find_bone(pbn));
  407. Vector<int> prof_parent_bones;
  408. while (parent_name != StringName()) {
  409. prof_parent_bones.push_back(skeleton->find_bone(bone_map->get_skeleton_bone_name(parent_name)));
  410. if (prof->find_bone(parent_name) == -1) {
  411. break;
  412. }
  413. parent_name = prof->get_bone_parent(prof->find_bone(parent_name));
  414. }
  415. int parent_id = skeleton->get_bone_parent(bone_idx);
  416. Vector<int> skel_parent_bones;
  417. while (parent_id >= 0) {
  418. skel_parent_bones.push_back(parent_id);
  419. parent_id = skeleton->get_bone_parent(parent_id);
  420. }
  421. bool is_broken = false;
  422. for (int j = 0; j < prof_parent_bones.size(); j++) {
  423. if (prof_parent_bones[j] != -1 && !skel_parent_bones.has(prof_parent_bones[j])) {
  424. is_broken = true;
  425. }
  426. }
  427. if (is_broken) {
  428. bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_ERROR);
  429. } else {
  430. bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_SET);
  431. }
  432. } else {
  433. bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_ERROR);
  434. }
  435. } else {
  436. if (bone_mapper_buttons[i]->is_require()) {
  437. bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_MISSING);
  438. } else {
  439. bone_mapper_buttons[i]->set_state(BoneMapperButton::BONE_MAP_STATE_UNSET);
  440. }
  441. }
  442. }
  443. }
  444. void BoneMapper::_clear_mapping_current_group() {
  445. if (bone_map.is_valid()) {
  446. Ref<SkeletonProfile> profile = bone_map->get_profile();
  447. if (profile.is_valid() && profile->get_group_size() > 0) {
  448. int len = profile->get_bone_size();
  449. for (int i = 0; i < len; i++) {
  450. if (profile->get_group(i) == profile->get_group_name(current_group_idx)) {
  451. bone_map->_set_skeleton_bone_name(profile->get_bone_name(i), StringName());
  452. }
  453. }
  454. recreate_items();
  455. }
  456. }
  457. }
  458. #ifdef MODULE_REGEX_ENABLED
  459. bool BoneMapper::is_match_with_bone_name(const String &p_bone_name, const String &p_word) {
  460. RegEx re = RegEx(p_word);
  461. return !re.search(p_bone_name.to_lower()).is_null();
  462. }
  463. 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) {
  464. // There may be multiple candidates hit by existing the subsidiary bone.
  465. // The one with the shortest name is probably the original.
  466. LocalVector<String> hit_list;
  467. String shortest = "";
  468. for (int word_idx = 0; word_idx < p_picklist.size(); word_idx++) {
  469. if (p_child == -1) {
  470. Vector<int> bones_to_process = p_parent == -1 ? p_skeleton->get_parentless_bones() : p_skeleton->get_bone_children(p_parent);
  471. while (bones_to_process.size() > 0) {
  472. int idx = bones_to_process[0];
  473. bones_to_process.erase(idx);
  474. Vector<int> children = p_skeleton->get_bone_children(idx);
  475. for (int i = 0; i < children.size(); i++) {
  476. bones_to_process.push_back(children[i]);
  477. }
  478. if (p_children_count == 0 && children.size() > 0) {
  479. continue;
  480. }
  481. if (p_children_count > 0 && children.size() < p_children_count) {
  482. continue;
  483. }
  484. String bn = skeleton->get_bone_name(idx);
  485. if (is_match_with_bone_name(bn, p_picklist[word_idx]) && guess_bone_segregation(bn) == p_segregation) {
  486. hit_list.push_back(bn);
  487. }
  488. }
  489. if (hit_list.size() > 0) {
  490. shortest = hit_list[0];
  491. for (const String &hit : hit_list) {
  492. if (hit.length() < shortest.length()) {
  493. shortest = hit; // Prioritize parent.
  494. }
  495. }
  496. }
  497. } else {
  498. int idx = skeleton->get_bone_parent(p_child);
  499. while (idx != p_parent && idx >= 0) {
  500. Vector<int> children = p_skeleton->get_bone_children(idx);
  501. if (p_children_count == 0 && children.size() > 0) {
  502. continue;
  503. }
  504. if (p_children_count > 0 && children.size() < p_children_count) {
  505. continue;
  506. }
  507. String bn = skeleton->get_bone_name(idx);
  508. if (is_match_with_bone_name(bn, p_picklist[word_idx]) && guess_bone_segregation(bn) == p_segregation) {
  509. hit_list.push_back(bn);
  510. }
  511. idx = skeleton->get_bone_parent(idx);
  512. }
  513. if (hit_list.size() > 0) {
  514. shortest = hit_list[0];
  515. for (const String &hit : hit_list) {
  516. if (hit.length() <= shortest.length()) {
  517. shortest = hit; // Prioritize parent.
  518. }
  519. }
  520. }
  521. }
  522. if (shortest != "") {
  523. break;
  524. }
  525. }
  526. if (shortest == "") {
  527. return -1;
  528. }
  529. return skeleton->find_bone(shortest);
  530. }
  531. BoneMapper::BoneSegregation BoneMapper::guess_bone_segregation(const String &p_bone_name) {
  532. String fixed_bn = p_bone_name.to_snake_case();
  533. LocalVector<String> left_words;
  534. left_words.push_back("(?<![a-zA-Z])left");
  535. left_words.push_back("(?<![a-zA-Z0-9])l(?![a-zA-Z0-9])");
  536. LocalVector<String> right_words;
  537. right_words.push_back("(?<![a-zA-Z])right");
  538. right_words.push_back("(?<![a-zA-Z0-9])r(?![a-zA-Z0-9])");
  539. for (uint32_t i = 0; i < left_words.size(); i++) {
  540. RegEx re_l = RegEx(left_words[i]);
  541. if (!re_l.search(fixed_bn).is_null()) {
  542. return BONE_SEGREGATION_LEFT;
  543. }
  544. RegEx re_r = RegEx(right_words[i]);
  545. if (!re_r.search(fixed_bn).is_null()) {
  546. return BONE_SEGREGATION_RIGHT;
  547. }
  548. }
  549. return BONE_SEGREGATION_NONE;
  550. }
  551. void BoneMapper::_run_auto_mapping() {
  552. auto_mapping_process(bone_map);
  553. recreate_items();
  554. }
  555. void BoneMapper::auto_mapping_process(Ref<BoneMap> &p_bone_map) {
  556. WARN_PRINT("Run auto mapping.");
  557. int bone_idx = -1;
  558. Vector<String> picklist; // Use Vector<String> because match words have priority.
  559. Vector<int> search_path;
  560. // 1. Guess Hips
  561. picklist.push_back("hip");
  562. picklist.push_back("pelvis");
  563. picklist.push_back("waist");
  564. picklist.push_back("torso");
  565. picklist.push_back("spine");
  566. int hips = search_bone_by_name(skeleton, picklist);
  567. if (hips == -1) {
  568. WARN_PRINT("Auto Mapping couldn't guess Hips. Abort auto mapping.");
  569. return; // If there is no Hips, we cannot guess bone after then.
  570. } else {
  571. p_bone_map->_set_skeleton_bone_name("Hips", skeleton->get_bone_name(hips));
  572. }
  573. picklist.clear();
  574. // 2. Guess Root
  575. bone_idx = skeleton->get_bone_parent(hips);
  576. while (bone_idx >= 0) {
  577. search_path.push_back(bone_idx);
  578. bone_idx = skeleton->get_bone_parent(bone_idx);
  579. }
  580. if (search_path.size() == 0) {
  581. bone_idx = -1;
  582. } else if (search_path.size() == 1) {
  583. bone_idx = search_path[0]; // It is only one bone which can be root.
  584. } else {
  585. bool found = false;
  586. for (int i = 0; i < search_path.size(); i++) {
  587. RegEx re = RegEx("root");
  588. if (!re.search(skeleton->get_bone_name(search_path[i]).to_lower()).is_null()) {
  589. bone_idx = search_path[i]; // Name match is preferred.
  590. found = true;
  591. break;
  592. }
  593. }
  594. if (!found) {
  595. for (int i = 0; i < search_path.size(); i++) {
  596. if (skeleton->get_bone_global_rest(search_path[i]).origin.is_zero_approx()) {
  597. bone_idx = search_path[i]; // The bone existing at the origin is appropriate as a root.
  598. found = true;
  599. break;
  600. }
  601. }
  602. }
  603. if (!found) {
  604. bone_idx = search_path[search_path.size() - 1]; // Ambiguous, but most parental bone selected.
  605. }
  606. }
  607. if (bone_idx == -1) {
  608. WARN_PRINT("Auto Mapping couldn't guess Root."); // Root is not required, so continue.
  609. } else {
  610. p_bone_map->_set_skeleton_bone_name("Root", skeleton->get_bone_name(bone_idx));
  611. }
  612. bone_idx = -1;
  613. search_path.clear();
  614. // 3. Guess Foots
  615. picklist.push_back("foot");
  616. picklist.push_back("ankle");
  617. int left_foot = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips);
  618. if (left_foot == -1) {
  619. WARN_PRINT("Auto Mapping couldn't guess LeftFoot.");
  620. } else {
  621. p_bone_map->_set_skeleton_bone_name("LeftFoot", skeleton->get_bone_name(left_foot));
  622. }
  623. int right_foot = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips);
  624. if (right_foot == -1) {
  625. WARN_PRINT("Auto Mapping couldn't guess RightFoot.");
  626. } else {
  627. p_bone_map->_set_skeleton_bone_name("RightFoot", skeleton->get_bone_name(right_foot));
  628. }
  629. picklist.clear();
  630. // 3-1. Guess LowerLegs
  631. picklist.push_back("(low|under).*leg");
  632. picklist.push_back("knee");
  633. picklist.push_back("shin");
  634. picklist.push_back("calf");
  635. picklist.push_back("leg");
  636. int left_lower_leg = -1;
  637. if (left_foot != -1) {
  638. left_lower_leg = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, left_foot);
  639. }
  640. if (left_lower_leg == -1) {
  641. WARN_PRINT("Auto Mapping couldn't guess LeftLowerLeg.");
  642. } else {
  643. p_bone_map->_set_skeleton_bone_name("LeftLowerLeg", skeleton->get_bone_name(left_lower_leg));
  644. }
  645. int right_lower_leg = -1;
  646. if (right_foot != -1) {
  647. right_lower_leg = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, right_foot);
  648. }
  649. if (right_lower_leg == -1) {
  650. WARN_PRINT("Auto Mapping couldn't guess RightLowerLeg.");
  651. } else {
  652. p_bone_map->_set_skeleton_bone_name("RightLowerLeg", skeleton->get_bone_name(right_lower_leg));
  653. }
  654. picklist.clear();
  655. // 3-2. Guess UpperLegs
  656. picklist.push_back("up.*leg");
  657. picklist.push_back("thigh");
  658. picklist.push_back("leg");
  659. if (left_lower_leg != -1) {
  660. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, left_lower_leg);
  661. }
  662. if (bone_idx == -1) {
  663. WARN_PRINT("Auto Mapping couldn't guess LeftUpperLeg.");
  664. } else {
  665. p_bone_map->_set_skeleton_bone_name("LeftUpperLeg", skeleton->get_bone_name(bone_idx));
  666. }
  667. bone_idx = -1;
  668. if (right_lower_leg != -1) {
  669. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, right_lower_leg);
  670. }
  671. if (bone_idx == -1) {
  672. WARN_PRINT("Auto Mapping couldn't guess RightUpperLeg.");
  673. } else {
  674. p_bone_map->_set_skeleton_bone_name("RightUpperLeg", skeleton->get_bone_name(bone_idx));
  675. }
  676. bone_idx = -1;
  677. picklist.clear();
  678. // 3-3. Guess Toes
  679. picklist.push_back("toe");
  680. picklist.push_back("ball");
  681. if (left_foot != -1) {
  682. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_foot);
  683. if (bone_idx == -1) {
  684. search_path = skeleton->get_bone_children(left_foot);
  685. if (search_path.size() == 1) {
  686. bone_idx = search_path[0]; // Maybe only one child of the Foot is Toes.
  687. }
  688. search_path.clear();
  689. }
  690. }
  691. if (bone_idx == -1) {
  692. WARN_PRINT("Auto Mapping couldn't guess LeftToes.");
  693. } else {
  694. p_bone_map->_set_skeleton_bone_name("LeftToes", skeleton->get_bone_name(bone_idx));
  695. }
  696. bone_idx = -1;
  697. if (right_foot != -1) {
  698. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_foot);
  699. if (bone_idx == -1) {
  700. search_path = skeleton->get_bone_children(right_foot);
  701. if (search_path.size() == 1) {
  702. bone_idx = search_path[0]; // Maybe only one child of the Foot is Toes.
  703. }
  704. search_path.clear();
  705. }
  706. }
  707. if (bone_idx == -1) {
  708. WARN_PRINT("Auto Mapping couldn't guess RightToes.");
  709. } else {
  710. p_bone_map->_set_skeleton_bone_name("RightToes", skeleton->get_bone_name(bone_idx));
  711. }
  712. bone_idx = -1;
  713. picklist.clear();
  714. // 4. Guess Hands
  715. picklist.push_back("hand");
  716. picklist.push_back("wrist");
  717. picklist.push_back("palm");
  718. picklist.push_back("fingers");
  719. int left_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, -1, 5);
  720. if (left_hand_or_palm == -1) {
  721. // Ambiguous, but try again for fewer finger models.
  722. left_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips);
  723. }
  724. int left_hand = left_hand_or_palm; // Check for the presence of a wrist, since bones with five children may be palmar.
  725. while (left_hand != -1) {
  726. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips, left_hand);
  727. if (bone_idx == -1) {
  728. break;
  729. }
  730. left_hand = bone_idx;
  731. }
  732. if (left_hand == -1) {
  733. WARN_PRINT("Auto Mapping couldn't guess LeftHand.");
  734. } else {
  735. p_bone_map->_set_skeleton_bone_name("LeftHand", skeleton->get_bone_name(left_hand));
  736. }
  737. bone_idx = -1;
  738. int right_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, -1, 5);
  739. if (right_hand_or_palm == -1) {
  740. // Ambiguous, but try again for fewer finger models.
  741. right_hand_or_palm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips);
  742. }
  743. int right_hand = right_hand_or_palm;
  744. while (right_hand != -1) {
  745. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips, right_hand);
  746. if (bone_idx == -1) {
  747. break;
  748. }
  749. right_hand = bone_idx;
  750. }
  751. if (right_hand == -1) {
  752. WARN_PRINT("Auto Mapping couldn't guess RightHand.");
  753. } else {
  754. p_bone_map->_set_skeleton_bone_name("RightHand", skeleton->get_bone_name(right_hand));
  755. }
  756. bone_idx = -1;
  757. picklist.clear();
  758. // 4-1. Guess Finger
  759. int tips_index = -1;
  760. bool thumb_tips_size = 0;
  761. bool named_finger_is_found = false;
  762. LocalVector<String> fingers;
  763. fingers.push_back("thumb|pollex");
  764. fingers.push_back("index|fore");
  765. fingers.push_back("middle");
  766. fingers.push_back("ring");
  767. fingers.push_back("little|pinkie|pinky");
  768. if (left_hand_or_palm != -1) {
  769. LocalVector<LocalVector<String>> left_fingers_map;
  770. left_fingers_map.resize(5);
  771. left_fingers_map[0].push_back("LeftThumbMetacarpal");
  772. left_fingers_map[0].push_back("LeftThumbProximal");
  773. left_fingers_map[0].push_back("LeftThumbDistal");
  774. left_fingers_map[1].push_back("LeftIndexProximal");
  775. left_fingers_map[1].push_back("LeftIndexIntermediate");
  776. left_fingers_map[1].push_back("LeftIndexDistal");
  777. left_fingers_map[2].push_back("LeftMiddleProximal");
  778. left_fingers_map[2].push_back("LeftMiddleIntermediate");
  779. left_fingers_map[2].push_back("LeftMiddleDistal");
  780. left_fingers_map[3].push_back("LeftRingProximal");
  781. left_fingers_map[3].push_back("LeftRingIntermediate");
  782. left_fingers_map[3].push_back("LeftRingDistal");
  783. left_fingers_map[4].push_back("LeftLittleProximal");
  784. left_fingers_map[4].push_back("LeftLittleIntermediate");
  785. left_fingers_map[4].push_back("LeftLittleDistal");
  786. for (int i = 0; i < 5; i++) {
  787. picklist.push_back(fingers[i]);
  788. int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_hand_or_palm, -1, 0);
  789. if (finger != -1) {
  790. while (finger != left_hand_or_palm && finger >= 0) {
  791. search_path.push_back(finger);
  792. finger = skeleton->get_bone_parent(finger);
  793. }
  794. // Tips detection by name matching with "distal" from root.
  795. for (int j = search_path.size() - 1; j >= 0; j--) {
  796. if (RegEx("distal").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
  797. tips_index = j - 1;
  798. break;
  799. }
  800. }
  801. // Tips detection by name matching with "tip|leaf" from end.
  802. if (tips_index < 0) {
  803. for (int j = 0; j < search_path.size(); j++) {
  804. if (RegEx("tip|leaf").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
  805. tips_index = j;
  806. break;
  807. }
  808. }
  809. }
  810. // Tips detection by thumb children size.
  811. if (tips_index < 0) {
  812. if (i == 0) {
  813. thumb_tips_size = MAX(0, search_path.size() - 3);
  814. }
  815. tips_index = thumb_tips_size - 1;
  816. }
  817. // Remove tips.
  818. for (int j = 0; j <= tips_index; j++) {
  819. search_path.remove_at(0);
  820. }
  821. search_path.reverse();
  822. if (search_path.size() == 1) {
  823. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  824. named_finger_is_found = true;
  825. } else if (search_path.size() == 2) {
  826. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  827. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  828. named_finger_is_found = true;
  829. } else if (search_path.size() >= 3) {
  830. search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
  831. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  832. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  833. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
  834. named_finger_is_found = true;
  835. }
  836. }
  837. picklist.clear();
  838. search_path.clear();
  839. }
  840. // It is a bit corner case, but possibly the finger names are sequentially numbered...
  841. if (!named_finger_is_found) {
  842. picklist.push_back("finger");
  843. RegEx finger_re = RegEx("finger");
  844. search_path = skeleton->get_bone_children(left_hand_or_palm);
  845. Vector<String> finger_names;
  846. for (int i = 0; i < search_path.size(); i++) {
  847. String bn = skeleton->get_bone_name(search_path[i]);
  848. if (!finger_re.search(bn.to_lower()).is_null()) {
  849. finger_names.push_back(bn);
  850. }
  851. }
  852. finger_names.sort(); // Order by lexicographic, normal use cases never have more than 10 fingers in one hand.
  853. search_path.clear();
  854. for (int i = 0; i < finger_names.size(); i++) {
  855. if (i >= 5) {
  856. break;
  857. }
  858. int finger_root = skeleton->find_bone(finger_names[i]);
  859. int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, finger_root, -1, 0);
  860. if (finger != -1) {
  861. while (finger != finger_root && finger >= 0) {
  862. search_path.push_back(finger);
  863. finger = skeleton->get_bone_parent(finger);
  864. }
  865. }
  866. search_path.push_back(finger_root);
  867. // Tips detection by thumb children size.
  868. if (i == 0) {
  869. thumb_tips_size = MAX(0, search_path.size() - 3);
  870. }
  871. tips_index = thumb_tips_size - 1;
  872. for (int j = 0; j <= tips_index; j++) {
  873. search_path.remove_at(0);
  874. }
  875. search_path.reverse();
  876. if (search_path.size() == 1) {
  877. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  878. } else if (search_path.size() == 2) {
  879. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  880. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  881. } else if (search_path.size() >= 3) {
  882. search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
  883. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  884. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  885. p_bone_map->_set_skeleton_bone_name(left_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
  886. }
  887. search_path.clear();
  888. }
  889. picklist.clear();
  890. }
  891. }
  892. tips_index = -1;
  893. thumb_tips_size = 0;
  894. named_finger_is_found = false;
  895. if (right_hand_or_palm != -1) {
  896. LocalVector<LocalVector<String>> right_fingers_map;
  897. right_fingers_map.resize(5);
  898. right_fingers_map[0].push_back("RightThumbMetacarpal");
  899. right_fingers_map[0].push_back("RightThumbProximal");
  900. right_fingers_map[0].push_back("RightThumbDistal");
  901. right_fingers_map[1].push_back("RightIndexProximal");
  902. right_fingers_map[1].push_back("RightIndexIntermediate");
  903. right_fingers_map[1].push_back("RightIndexDistal");
  904. right_fingers_map[2].push_back("RightMiddleProximal");
  905. right_fingers_map[2].push_back("RightMiddleIntermediate");
  906. right_fingers_map[2].push_back("RightMiddleDistal");
  907. right_fingers_map[3].push_back("RightRingProximal");
  908. right_fingers_map[3].push_back("RightRingIntermediate");
  909. right_fingers_map[3].push_back("RightRingDistal");
  910. right_fingers_map[4].push_back("RightLittleProximal");
  911. right_fingers_map[4].push_back("RightLittleIntermediate");
  912. right_fingers_map[4].push_back("RightLittleDistal");
  913. for (int i = 0; i < 5; i++) {
  914. picklist.push_back(fingers[i]);
  915. int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_hand_or_palm, -1, 0);
  916. if (finger != -1) {
  917. while (finger != right_hand_or_palm && finger >= 0) {
  918. search_path.push_back(finger);
  919. finger = skeleton->get_bone_parent(finger);
  920. }
  921. // Tips detection by name matching with "distal" from root.
  922. for (int j = search_path.size() - 1; j >= 0; j--) {
  923. if (RegEx("distal").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
  924. tips_index = j - 1;
  925. break;
  926. }
  927. }
  928. // Tips detection by name matching with "tip|leaf" from end.
  929. if (tips_index < 0) {
  930. for (int j = 0; j < search_path.size(); j++) {
  931. if (RegEx("tip|leaf").search(skeleton->get_bone_name(search_path[j]).to_lower()).is_valid()) {
  932. tips_index = j;
  933. break;
  934. }
  935. }
  936. }
  937. // Tips detection by thumb children size.
  938. if (tips_index < 0) {
  939. if (i == 0) {
  940. thumb_tips_size = MAX(0, search_path.size() - 3);
  941. }
  942. tips_index = thumb_tips_size - 1;
  943. }
  944. // Remove tips.
  945. for (int j = 0; j <= tips_index; j++) {
  946. search_path.remove_at(0);
  947. }
  948. search_path.reverse();
  949. if (search_path.size() == 1) {
  950. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  951. named_finger_is_found = true;
  952. } else if (search_path.size() == 2) {
  953. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  954. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  955. named_finger_is_found = true;
  956. } else if (search_path.size() >= 3) {
  957. search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
  958. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  959. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  960. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
  961. named_finger_is_found = true;
  962. }
  963. }
  964. picklist.clear();
  965. search_path.clear();
  966. }
  967. // It is a bit corner case, but possibly the finger names are sequentially numbered...
  968. if (!named_finger_is_found) {
  969. picklist.push_back("finger");
  970. RegEx finger_re = RegEx("finger");
  971. search_path = skeleton->get_bone_children(right_hand_or_palm);
  972. Vector<String> finger_names;
  973. for (int i = 0; i < search_path.size(); i++) {
  974. String bn = skeleton->get_bone_name(search_path[i]);
  975. if (!finger_re.search(bn.to_lower()).is_null()) {
  976. finger_names.push_back(bn);
  977. }
  978. }
  979. finger_names.sort(); // Order by lexicographic, normal use cases never have more than 10 fingers in one hand.
  980. search_path.clear();
  981. for (int i = 0; i < finger_names.size(); i++) {
  982. if (i >= 5) {
  983. break;
  984. }
  985. int finger_root = skeleton->find_bone(finger_names[i]);
  986. int finger = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, finger_root, -1, 0);
  987. if (finger != -1) {
  988. while (finger != finger_root && finger >= 0) {
  989. search_path.push_back(finger);
  990. finger = skeleton->get_bone_parent(finger);
  991. }
  992. }
  993. search_path.push_back(finger_root);
  994. // Tips detection by thumb children size.
  995. if (i == 0) {
  996. thumb_tips_size = MAX(0, search_path.size() - 3);
  997. }
  998. tips_index = thumb_tips_size - 1;
  999. for (int j = 0; j <= tips_index; j++) {
  1000. search_path.remove_at(0);
  1001. }
  1002. search_path.reverse();
  1003. if (search_path.size() == 1) {
  1004. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  1005. } else if (search_path.size() == 2) {
  1006. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  1007. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  1008. } else if (search_path.size() >= 3) {
  1009. search_path = search_path.slice(-3); // Eliminate the possibility of carpal bone.
  1010. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][0], skeleton->get_bone_name(search_path[0]));
  1011. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][1], skeleton->get_bone_name(search_path[1]));
  1012. p_bone_map->_set_skeleton_bone_name(right_fingers_map[i][2], skeleton->get_bone_name(search_path[2]));
  1013. }
  1014. search_path.clear();
  1015. }
  1016. picklist.clear();
  1017. }
  1018. }
  1019. // 5. Guess Arms
  1020. picklist.push_back("shoulder");
  1021. picklist.push_back("clavicle");
  1022. picklist.push_back("collar");
  1023. int left_shoulder = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, hips);
  1024. if (left_shoulder == -1) {
  1025. WARN_PRINT("Auto Mapping couldn't guess LeftShoulder.");
  1026. } else {
  1027. p_bone_map->_set_skeleton_bone_name("LeftShoulder", skeleton->get_bone_name(left_shoulder));
  1028. }
  1029. int right_shoulder = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, hips);
  1030. if (right_shoulder == -1) {
  1031. WARN_PRINT("Auto Mapping couldn't guess RightShoulder.");
  1032. } else {
  1033. p_bone_map->_set_skeleton_bone_name("RightShoulder", skeleton->get_bone_name(right_shoulder));
  1034. }
  1035. picklist.clear();
  1036. // 5-1. Guess LowerArms
  1037. picklist.push_back("(low|fore).*arm");
  1038. picklist.push_back("elbow");
  1039. picklist.push_back("arm");
  1040. int left_lower_arm = -1;
  1041. if (left_shoulder != -1 && left_hand_or_palm != -1) {
  1042. left_lower_arm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_shoulder, left_hand_or_palm);
  1043. }
  1044. if (left_lower_arm == -1) {
  1045. WARN_PRINT("Auto Mapping couldn't guess LeftLowerArm.");
  1046. } else {
  1047. p_bone_map->_set_skeleton_bone_name("LeftLowerArm", skeleton->get_bone_name(left_lower_arm));
  1048. }
  1049. int right_lower_arm = -1;
  1050. if (right_shoulder != -1 && right_hand_or_palm != -1) {
  1051. right_lower_arm = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_shoulder, right_hand_or_palm);
  1052. }
  1053. if (right_lower_arm == -1) {
  1054. WARN_PRINT("Auto Mapping couldn't guess RightLowerArm.");
  1055. } else {
  1056. p_bone_map->_set_skeleton_bone_name("RightLowerArm", skeleton->get_bone_name(right_lower_arm));
  1057. }
  1058. picklist.clear();
  1059. // 5-2. Guess UpperArms
  1060. picklist.push_back("up.*arm");
  1061. picklist.push_back("arm");
  1062. if (left_shoulder != -1 && left_lower_arm != -1) {
  1063. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_LEFT, left_shoulder, left_lower_arm);
  1064. }
  1065. if (bone_idx == -1) {
  1066. WARN_PRINT("Auto Mapping couldn't guess LeftUpperArm.");
  1067. } else {
  1068. p_bone_map->_set_skeleton_bone_name("LeftUpperArm", skeleton->get_bone_name(bone_idx));
  1069. }
  1070. bone_idx = -1;
  1071. if (right_shoulder != -1 && right_lower_arm != -1) {
  1072. bone_idx = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_RIGHT, right_shoulder, right_lower_arm);
  1073. }
  1074. if (bone_idx == -1) {
  1075. WARN_PRINT("Auto Mapping couldn't guess RightUpperArm.");
  1076. } else {
  1077. p_bone_map->_set_skeleton_bone_name("RightUpperArm", skeleton->get_bone_name(bone_idx));
  1078. }
  1079. bone_idx = -1;
  1080. picklist.clear();
  1081. // 6. Guess Neck
  1082. picklist.push_back("neck");
  1083. picklist.push_back("head"); // For no neck model.
  1084. picklist.push_back("face"); // Same above.
  1085. int neck = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_NONE, hips);
  1086. picklist.clear();
  1087. if (neck == -1) {
  1088. // If it can't expect by name, search child spine of where the right and left shoulders (or hands) cross.
  1089. int ls_idx = left_shoulder != -1 ? left_shoulder : (left_hand_or_palm != -1 ? left_hand_or_palm : -1);
  1090. int rs_idx = right_shoulder != -1 ? right_shoulder : (right_hand_or_palm != -1 ? right_hand_or_palm : -1);
  1091. if (ls_idx != -1 && rs_idx != -1) {
  1092. bool detect = false;
  1093. while (ls_idx != hips && ls_idx >= 0 && rs_idx != hips && rs_idx >= 0) {
  1094. ls_idx = skeleton->get_bone_parent(ls_idx);
  1095. rs_idx = skeleton->get_bone_parent(rs_idx);
  1096. if (ls_idx == rs_idx) {
  1097. detect = true;
  1098. break;
  1099. }
  1100. }
  1101. if (detect) {
  1102. Vector<int> children = skeleton->get_bone_children(ls_idx);
  1103. children.erase(ls_idx);
  1104. children.erase(rs_idx);
  1105. String word = "spine"; // It would be better to limit the search with "spine" because it could be mistaken with breast, wing and etc...
  1106. for (int i = 0; i < children.size(); i++) {
  1107. bone_idx = children[i];
  1108. if (is_match_with_bone_name(skeleton->get_bone_name(bone_idx), word)) {
  1109. neck = bone_idx;
  1110. break;
  1111. };
  1112. }
  1113. bone_idx = -1;
  1114. }
  1115. }
  1116. }
  1117. // 7. Guess Head
  1118. picklist.push_back("head");
  1119. picklist.push_back("face");
  1120. int head = search_bone_by_name(skeleton, picklist, BONE_SEGREGATION_NONE, neck);
  1121. if (head == -1) {
  1122. search_path = skeleton->get_bone_children(neck);
  1123. if (search_path.size() == 1) {
  1124. head = search_path[0]; // Maybe only one child of the Neck is Head.
  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. // Register properties in editor settings.
  1348. EDITOR_DEF("editors/bone_mapper/handle_colors/unset", Color(0.3, 0.3, 0.3));
  1349. EDITOR_DEF("editors/bone_mapper/handle_colors/set", Color(0.1, 0.6, 0.25));
  1350. EDITOR_DEF("editors/bone_mapper/handle_colors/missing", Color(0.8, 0.2, 0.8));
  1351. EDITOR_DEF("editors/bone_mapper/handle_colors/error", Color(0.8, 0.2, 0.2));
  1352. Ref<EditorInspectorPluginBoneMap> inspector_plugin;
  1353. inspector_plugin.instantiate();
  1354. add_inspector_plugin(inspector_plugin);
  1355. Ref<PostImportPluginSkeletonTrackOrganizer> post_import_plugin_track_organizer;
  1356. post_import_plugin_track_organizer.instantiate();
  1357. add_scene_post_import_plugin(post_import_plugin_track_organizer);
  1358. Ref<PostImportPluginSkeletonRenamer> post_import_plugin_renamer;
  1359. post_import_plugin_renamer.instantiate();
  1360. add_scene_post_import_plugin(post_import_plugin_renamer);
  1361. Ref<PostImportPluginSkeletonRestFixer> post_import_plugin_rest_fixer;
  1362. post_import_plugin_rest_fixer.instantiate();
  1363. add_scene_post_import_plugin(post_import_plugin_rest_fixer);
  1364. }