post_import_plugin_skeleton_renamer.cpp 11 KB

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