Debian Codegraph @

editor/import/3d/post_import_plugin_skeleton_renamer.cpp

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