bone_map_editor_plugin.cpp 52 KB

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