bone_map_editor_plugin.cpp 52 KB

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