bone_map_editor_plugin.cpp 48 KB

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