post_import_plugin_skeleton_renamer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**************************************************************************/
  2. /* post_import_plugin_skeleton_renamer.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 "post_import_plugin_skeleton_renamer.h"
  31. #include "editor/import/scene_import_settings.h"
  32. #include "scene/3d/bone_attachment_3d.h"
  33. #include "scene/3d/importer_mesh_instance_3d.h"
  34. #include "scene/3d/skeleton_3d.h"
  35. #include "scene/animation/animation_player.h"
  36. #include "scene/resources/bone_map.h"
  37. void PostImportPluginSkeletonRenamer::get_internal_import_options(InternalImportCategory p_category, List<ResourceImporter::ImportOption> *r_options) {
  38. if (p_category == INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE) {
  39. r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/bone_renamer/rename_bones"), true));
  40. r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/bone_renamer/unique_node/make_unique"), true));
  41. r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::STRING, "retarget/bone_renamer/unique_node/skeleton_name"), "GeneralSkeleton"));
  42. }
  43. }
  44. void PostImportPluginSkeletonRenamer::_internal_process(InternalImportCategory p_category, Node *p_base_scene, Node *p_node, Ref<Resource> p_resource, const Dictionary &p_options, HashMap<String, String> p_rename_map) {
  45. // Prepare objects.
  46. Object *map = p_options["retarget/bone_map"].get_validated_object();
  47. if (!map || !bool(p_options["retarget/bone_renamer/rename_bones"])) {
  48. return;
  49. }
  50. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_node);
  51. // Rename bones in Skeleton3D.
  52. {
  53. int len = skeleton->get_bone_count();
  54. for (int i = 0; i < len; i++) {
  55. StringName bn = p_rename_map[skeleton->get_bone_name(i)];
  56. if (bn) {
  57. skeleton->set_bone_name(i, bn);
  58. }
  59. }
  60. }
  61. // Rename bones in Skin.
  62. {
  63. TypedArray<Node> nodes = p_base_scene->find_children("*", "ImporterMeshInstance3D");
  64. while (nodes.size()) {
  65. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(nodes.pop_back());
  66. Ref<Skin> skin = mi->get_skin();
  67. if (skin.is_valid()) {
  68. Node *node = mi->get_node(mi->get_skeleton_path());
  69. if (node) {
  70. Skeleton3D *mesh_skeleton = Object::cast_to<Skeleton3D>(node);
  71. if (mesh_skeleton && node == skeleton) {
  72. int len = skin->get_bind_count();
  73. for (int i = 0; i < len; i++) {
  74. StringName bn = p_rename_map[skin->get_bind_name(i)];
  75. if (bn) {
  76. skin->set_bind_name(i, bn);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. // Rename bones in AnimationPlayer.
  85. {
  86. TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
  87. while (nodes.size()) {
  88. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
  89. List<StringName> anims;
  90. ap->get_animation_list(&anims);
  91. for (const StringName &name : anims) {
  92. Ref<Animation> anim = ap->get_animation(name);
  93. int len = anim->get_track_count();
  94. for (int i = 0; i < len; i++) {
  95. if (anim->track_get_path(i).get_subname_count() != 1 || !(anim->track_get_type(i) == Animation::TYPE_POSITION_3D || anim->track_get_type(i) == Animation::TYPE_ROTATION_3D || anim->track_get_type(i) == Animation::TYPE_SCALE_3D)) {
  96. continue;
  97. }
  98. String track_path = String(anim->track_get_path(i).get_concatenated_names());
  99. Node *node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
  100. if (node) {
  101. Skeleton3D *track_skeleton = Object::cast_to<Skeleton3D>(node);
  102. if (track_skeleton && track_skeleton == skeleton) {
  103. StringName bn = p_rename_map[anim->track_get_path(i).get_subname(0)];
  104. if (bn) {
  105. anim->track_set_path(i, track_path + ":" + bn);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. // Rename bones in all Nodes by calling method.
  114. {
  115. Dictionary rename_map_dict;
  116. for (HashMap<String, String>::Iterator E = p_rename_map.begin(); E; ++E) {
  117. rename_map_dict[E->key] = E->value;
  118. }
  119. TypedArray<Node> nodes = p_base_scene->find_children("*", "BoneAttachment3D");
  120. while (nodes.size()) {
  121. BoneAttachment3D *attachment = Object::cast_to<BoneAttachment3D>(nodes.pop_back());
  122. if (attachment) {
  123. attachment->notify_skeleton_bones_renamed(p_base_scene, skeleton, rename_map_dict);
  124. }
  125. }
  126. }
  127. }
  128. void PostImportPluginSkeletonRenamer::internal_process(InternalImportCategory p_category, Node *p_base_scene, Node *p_node, Ref<Resource> p_resource, const Dictionary &p_options) {
  129. if (p_category == INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE) {
  130. // Prepare objects.
  131. Object *map = p_options["retarget/bone_map"].get_validated_object();
  132. if (!map || !bool(p_options["retarget/bone_renamer/rename_bones"])) {
  133. return;
  134. }
  135. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_node);
  136. BoneMap *bone_map = Object::cast_to<BoneMap>(map);
  137. int len = skeleton->get_bone_count();
  138. // First, prepare main rename map.
  139. HashMap<String, String> main_rename_map;
  140. for (int i = 0; i < len; i++) {
  141. String bone_name = skeleton->get_bone_name(i);
  142. String target_name = bone_map->find_profile_bone_name(bone_name);
  143. if (target_name.is_empty()) {
  144. continue;
  145. }
  146. main_rename_map.insert(bone_name, target_name);
  147. }
  148. // Preprocess of renaming bones to avoid to conflict with original bone name.
  149. HashMap<String, String> pre_rename_map; // HashMap<skeleton bone name, target(profile) bone name>
  150. {
  151. Vector<String> solved_name_stack;
  152. for (int i = 0; i < len; i++) {
  153. String bone_name = skeleton->get_bone_name(i);
  154. String target_name = bone_map->find_profile_bone_name(bone_name);
  155. if (target_name.is_empty() || bone_name == target_name || skeleton->find_bone(target_name) == -1) {
  156. continue; // No conflicting.
  157. }
  158. // Solve conflicting.
  159. Ref<SkeletonProfile> profile = bone_map->get_profile();
  160. String solved_name = target_name;
  161. for (int j = 2; skeleton->find_bone(solved_name) >= 0 || profile->find_bone(solved_name) >= 0 || solved_name_stack.has(solved_name); j++) {
  162. solved_name = target_name + itos(j);
  163. }
  164. solved_name_stack.push_back(solved_name);
  165. pre_rename_map.insert(target_name, solved_name);
  166. }
  167. _internal_process(p_category, p_base_scene, p_node, p_resource, p_options, pre_rename_map);
  168. }
  169. // Main process of renaming bones.
  170. {
  171. // Apply pre-renaming result to prepared main rename map.
  172. Vector<String> remove_queue;
  173. for (HashMap<String, String>::Iterator E = main_rename_map.begin(); E; ++E) {
  174. if (pre_rename_map.has(E->key)) {
  175. remove_queue.push_back(E->key);
  176. }
  177. }
  178. for (int i = 0; i < remove_queue.size(); i++) {
  179. main_rename_map.insert(pre_rename_map[remove_queue[i]], main_rename_map[remove_queue[i]]);
  180. main_rename_map.erase(remove_queue[i]);
  181. }
  182. _internal_process(p_category, p_base_scene, p_node, p_resource, p_options, main_rename_map);
  183. }
  184. // Make unique skeleton.
  185. if (bool(p_options["retarget/bone_renamer/unique_node/make_unique"])) {
  186. String unique_name = String(p_options["retarget/bone_renamer/unique_node/skeleton_name"]);
  187. ERR_FAIL_COND_MSG(unique_name.is_empty(), "Skeleton unique name cannot be empty.");
  188. TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
  189. while (nodes.size()) {
  190. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
  191. List<StringName> anims;
  192. ap->get_animation_list(&anims);
  193. for (const StringName &name : anims) {
  194. Ref<Animation> anim = ap->get_animation(name);
  195. int track_len = anim->get_track_count();
  196. for (int i = 0; i < track_len; i++) {
  197. String track_path = String(anim->track_get_path(i).get_concatenated_names());
  198. Node *orig_node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
  199. Node *node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
  200. while (node) {
  201. Skeleton3D *track_skeleton = Object::cast_to<Skeleton3D>(node);
  202. if (track_skeleton && track_skeleton == skeleton) {
  203. if (node == orig_node) {
  204. if (anim->track_get_path(i).get_subname_count() > 0) {
  205. anim->track_set_path(i, UNIQUE_NODE_PREFIX + unique_name + String(":") + anim->track_get_path(i).get_concatenated_subnames());
  206. } else {
  207. anim->track_set_path(i, UNIQUE_NODE_PREFIX + unique_name);
  208. }
  209. } else {
  210. if (anim->track_get_path(i).get_subname_count() > 0) {
  211. anim->track_set_path(i, UNIQUE_NODE_PREFIX + unique_name + "/" + node->get_path_to(orig_node) + String(":") + anim->track_get_path(i).get_concatenated_subnames());
  212. } else {
  213. anim->track_set_path(i, UNIQUE_NODE_PREFIX + unique_name + "/" + node->get_path_to(orig_node));
  214. }
  215. }
  216. break;
  217. }
  218. node = node->get_parent();
  219. }
  220. }
  221. }
  222. }
  223. skeleton->set_name(unique_name);
  224. skeleton->set_unique_name_in_owner(true);
  225. }
  226. }
  227. }
  228. PostImportPluginSkeletonRenamer::PostImportPluginSkeletonRenamer() {
  229. }