Debian Codegraph @

editor/import/3d/post_import_plugin_skeleton_rest_fixer.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* post_import_plugin_skeleton_rest_fixer.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_rest_fixer.h"
32
33 #include "scene/3d/bone_attachment_3d.h"
34 #include "scene/3d/importer_mesh_instance_3d.h"
35 #include "scene/3d/retarget_modifier_3d.h"
36 #include "scene/3d/skeleton_3d.h"
37 #include "scene/animation/animation_player.h"
38 #include "scene/resources/bone_map.h"
39
40 void PostImportPluginSkeletonRestFixer::get_internal_import_options(InternalImportCategory p_category, List<ResourceImporter::ImportOption> *r_options) {
41 if (p_category == INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE) {
42 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/rest_fixer/apply_node_transforms"), true));
43 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/rest_fixer/normalize_position_tracks"), true));
44 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/rest_fixer/reset_all_bone_poses_after_import"), true));
45
46 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, "retarget/rest_fixer/retarget_method", PROPERTY_HINT_ENUM, "None,Overwrite Axis,Use Retarget Modifier", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 1));
47 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/rest_fixer/keep_global_rest_on_leftovers"), true));
48 String skeleton_bones_must_be_renamed_warning = String(
49 "The skeleton modifier option uses SkeletonProfile as a list of bone names and retargets by name matching. Without renaming, retargeting by modifier will not work and the track path of the animation will be broken and it will be not playbacked correctly."); // TODO: translate.
50 r_options->push_back(ResourceImporter::ImportOption(
51 PropertyInfo(
52 Variant::STRING, U"retarget/rest_fixer/\u26A0_validation_warning/skeleton_bones_must_be_renamed",
53 PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY),
54 Variant(skeleton_bones_must_be_renamed_warning)));
55 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/rest_fixer/use_global_pose"), true));
56 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::STRING, "retarget/rest_fixer/original_skeleton_name"), "OriginalSkeleton"));
57 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "retarget/rest_fixer/fix_silhouette/enable", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
58 // TODO: PostImportPlugin need to be implemented such as validate_option(PropertyInfo &property, const Dictionary &p_options).
59 // get_internal_option_visibility() is not sufficient because it can only retrieve options implemented in the core and can only read option values.
60 // r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::ARRAY, "retarget/rest_fixer/filter", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::STRING_NAME, PROPERTY_HINT_ENUM, "Hips,Spine,Chest")), Array()));
61 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::ARRAY, "retarget/rest_fixer/fix_silhouette/filter", PROPERTY_HINT_ARRAY_TYPE, vformat("%s:", Variant::STRING_NAME)), Array()));
62 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::FLOAT, "retarget/rest_fixer/fix_silhouette/threshold"), 15));
63 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::FLOAT, "retarget/rest_fixer/fix_silhouette/base_height_adjustment", PROPERTY_HINT_RANGE, "-1,1,0.01"), 0.0));
64 }
65 }
66
67 Variant PostImportPluginSkeletonRestFixer::get_internal_option_visibility(InternalImportCategory p_category, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
68 if (p_category == INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE) {
69 if (p_option.begins_with("retarget/rest_fixer/fix_silhouette/")) {
70 if (!bool(p_options["retarget/rest_fixer/fix_silhouette/enable"])) {
71 if (!p_option.ends_with("enable")) {
72 return false;
73 }
74 }
75 } else if (p_option == "retarget/rest_fixer/keep_global_rest_on_leftovers") {
76 return int(p_options["retarget/rest_fixer/retarget_method"]) == 1;
77 } else if (p_option == "retarget/rest_fixer/original_skeleton_name" || p_option == "retarget/rest_fixer/use_global_pose") {
78 return int(p_options["retarget/rest_fixer/retarget_method"]) == 2;
79 } else if (p_option.begins_with("retarget/") && p_option.ends_with("skeleton_bones_must_be_renamed")) {
80 return int(p_options["retarget/rest_fixer/retarget_method"]) == 2 && bool(p_options["retarget/bone_renamer/rename_bones"]) == false;
81 }
82 }
83 return true;
84 }
85
86 void PostImportPluginSkeletonRestFixer::internal_process(InternalImportCategory p_category, Node *p_base_scene, Node *p_node, Ref<Resource> p_resource, const Dictionary &p_options) {
87 if (p_category == INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE) {
88 // Prepare objects.
89 Object *map = p_options["retarget/bone_map"].get_validated_object();
90 if (!map) {
91 return;
92 }
93 BoneMap *bone_map = Object::cast_to<BoneMap>(map);
94 Ref<SkeletonProfile> profile = bone_map->get_profile();
95 if (profile.is_null()) {
96 return;
97 }
98 Skeleton3D *src_skeleton = Object::cast_to<Skeleton3D>(p_node);
99 if (!src_skeleton) {
100 return;
101 }
102
103 bool is_renamed = bool(p_options["retarget/bone_renamer/rename_bones"]);
104 Array filter = p_options["retarget/rest_fixer/fix_silhouette/filter"];
105 bool is_rest_changed = false;
106
107 // Build profile skeleton.
108 Skeleton3D *prof_skeleton = memnew(Skeleton3D);
109 {
110 int prof_bone_len = profile->get_bone_size();
111 // Add single bones.
112 for (int i = 0; i < prof_bone_len; i++) {
113 prof_skeleton->add_bone(profile->get_bone_name(i));
114 prof_skeleton->set_bone_rest(i, profile->get_reference_pose(i));
115 }
116 // Set parents.
117 for (int i = 0; i < prof_bone_len; i++) {
118 int parent = profile->find_bone(profile->get_bone_parent(i));
119 if (parent >= 0) {
120 prof_skeleton->set_bone_parent(i, parent);
121 }
122 }
123 }
124
125 // Get global transform.
126 Transform3D global_transform;
127 if (bool(p_options["retarget/rest_fixer/apply_node_transforms"])) {
128 Node *pr = src_skeleton;
129 while (pr) {
130 Node3D *pr3d = Object::cast_to<Node3D>(pr);
131 if (pr3d) {
132 global_transform = pr3d->get_transform() * global_transform;
133 pr3d->set_transform(Transform3D());
134 }
135 pr = pr->get_parent();
136 }
137 global_transform.origin = Vector3(); // Translation by a Node is not a bone animation, so the retargeted model should be at the origin.
138 }
139
140 // Apply node transforms.
141 if (bool(p_options["retarget/rest_fixer/apply_node_transforms"])) {
142 Vector3 scl = global_transform.basis.get_scale_global();
143
144 Vector<int> bones_to_process = src_skeleton->get_parentless_bones();
145 for (int i = 0; i < bones_to_process.size(); i++) {
146 src_skeleton->set_bone_rest(bones_to_process[i], global_transform.orthonormalized() * src_skeleton->get_bone_rest(bones_to_process[i]));
147
148 src_skeleton->set_bone_pose_position(bones_to_process[i], global_transform.orthonormalized().xform(src_skeleton->get_bone_pose_position(bones_to_process[i])));
149 src_skeleton->set_bone_pose_rotation(bones_to_process[i], global_transform.basis.get_rotation_quaternion() * src_skeleton->get_bone_pose_rotation(bones_to_process[i]));
150 src_skeleton->set_bone_pose_scale(bones_to_process[i], (global_transform.orthonormalized().basis * Basis().scaled(src_skeleton->get_bone_pose_scale((bones_to_process[i])))).get_scale());
151 }
152
153 while (bones_to_process.size() > 0) {
154 int src_idx = bones_to_process[0];
155 bones_to_process.erase(src_idx);
156 Vector<int> src_children = src_skeleton->get_bone_children(src_idx);
157 for (int i = 0; i < src_children.size(); i++) {
158 bones_to_process.push_back(src_children[i]);
159 }
160 src_skeleton->set_bone_rest(src_idx, Transform3D(src_skeleton->get_bone_rest(src_idx).basis, src_skeleton->get_bone_rest(src_idx).origin * scl));
161 src_skeleton->set_bone_pose_position(src_idx, src_skeleton->get_bone_pose_position(src_idx) * scl);
162 }
163
164 // Fix animation by changing node transform.
165 bones_to_process = src_skeleton->get_parentless_bones();
166 {
167 TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
168 while (nodes.size()) {
169 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
170 List<StringName> anims;
171 ap->get_animation_list(&anims);
172 for (const StringName &name : anims) {
173 Ref<Animation> anim = ap->get_animation(name);
174 int track_len = anim->get_track_count();
175 for (int i = 0; i < track_len; i++) {
176 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)) {
177 continue;
178 }
179
180 if (anim->track_is_compressed(i)) {
181 continue; // Shouldn't occur in internal_process().
182 }
183
184 String track_path = String(anim->track_get_path(i).get_concatenated_names());
185 Node *node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
186 ERR_CONTINUE(!node);
187
188 Skeleton3D *track_skeleton = Object::cast_to<Skeleton3D>(node);
189 if (!track_skeleton || track_skeleton != src_skeleton) {
190 continue;
191 }
192
193 StringName bn = anim->track_get_path(i).get_subname(0);
194 if (!bn) {
195 continue;
196 }
197
198 int bone_idx = src_skeleton->find_bone(bn);
199 int key_len = anim->track_get_key_count(i);
200 if (anim->track_get_type(i) == Animation::TYPE_POSITION_3D) {
201 if (bones_to_process.has(bone_idx)) {
202 for (int j = 0; j < key_len; j++) {
203 Vector3 ps = static_cast<Vector3>(anim->track_get_key_value(i, j));
204 anim->track_set_key_value(i, j, global_transform.basis.xform(ps) + global_transform.origin);
205 }
206 } else {
207 for (int j = 0; j < key_len; j++) {
208 Vector3 ps = static_cast<Vector3>(anim->track_get_key_value(i, j));
209 anim->track_set_key_value(i, j, ps * scl);
210 }
211 }
212 } else if (bones_to_process.has(bone_idx)) {
213 if (anim->track_get_type(i) == Animation::TYPE_ROTATION_3D) {
214 for (int j = 0; j < key_len; j++) {
215 Quaternion qt = static_cast<Quaternion>(anim->track_get_key_value(i, j));
216 anim->track_set_key_value(i, j, global_transform.basis.get_rotation_quaternion() * qt);
217 }
218 } else {
219 for (int j = 0; j < key_len; j++) {
220 Basis sc = Basis().scaled(static_cast<Vector3>(anim->track_get_key_value(i, j)));
221 anim->track_set_key_value(i, j, (global_transform.orthonormalized().basis * sc).get_scale());
222 }
223 }
224 }
225 }
226 }
227 }
228 }
229
230 is_rest_changed = true;
231 }
232
233 // Complement Rotation track for compatibility between different rests.
234 {
235 TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
236 while (nodes.size()) {
237 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
238 List<StringName> anims;
239 ap->get_animation_list(&anims);
240 for (const StringName &name : anims) {
241 if (String(name).contains_char('/')) {
242 continue; // Avoid animation library which may be created by importer dynamically.
243 }
244
245 Ref<Animation> anim = ap->get_animation(name);
246 int track_len = anim->get_track_count();
247
248 // Detect does the animation have skeleton's TRS track.
249 String track_path;
250 bool found_skeleton = false;
251 for (int i = 0; i < track_len; i++) {
252 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)) {
253 continue;
254 }
255 track_path = String(anim->track_get_path(i).get_concatenated_names());
256 Node *node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
257 if (node) {
258 Skeleton3D *track_skeleton = Object::cast_to<Skeleton3D>(node);
259 if (track_skeleton && track_skeleton == src_skeleton) {
260 found_skeleton = true;
261 break;
262 }
263 }
264 }
265
266 if (!found_skeleton) {
267 continue;
268 }
269
270 // Search and insert rot track if it doesn't exist.
271 for (int prof_idx = 0; prof_idx < prof_skeleton->get_bone_count(); prof_idx++) {
272 String bone_name = is_renamed ? prof_skeleton->get_bone_name(prof_idx) : String(bone_map->get_skeleton_bone_name(prof_skeleton->get_bone_name(prof_idx)));
273 if (bone_name.is_empty()) {
274 continue;
275 }
276 int src_idx = src_skeleton->find_bone(bone_name);
277 if (src_idx == -1) {
278 continue;
279 }
280 String insert_path = track_path + ":" + bone_name;
281 int rot_track = anim->find_track(insert_path, Animation::TYPE_ROTATION_3D);
282 if (rot_track == -1) {
283 int track = anim->add_track(Animation::TYPE_ROTATION_3D);
284 anim->track_set_path(track, insert_path);
285 anim->track_set_imported(track, true);
286 anim->rotation_track_insert_key(track, 0, src_skeleton->get_bone_rest(src_idx).basis.get_rotation_quaternion());
287 }
288 }
289 }
290 }
291 }
292
293 // Fix silhouette.
294 Vector<Transform3D> silhouette_diff; // Transform values to be ignored when overwrite axis.
295 silhouette_diff.resize(src_skeleton->get_bone_count());
296 Transform3D *silhouette_diff_w = silhouette_diff.ptrw();
297 LocalVector<Transform3D> pre_silhouette_skeleton_global_rest;
298 for (int i = 0; i < src_skeleton->get_bone_count(); i++) {
299 pre_silhouette_skeleton_global_rest.push_back(src_skeleton->get_bone_global_rest(i));
300 }
301 if (bool(p_options["retarget/rest_fixer/fix_silhouette/enable"])) {
302 Vector<int> bones_to_process = prof_skeleton->get_parentless_bones();
303 while (bones_to_process.size() > 0) {
304 int prof_idx = bones_to_process[0];
305 bones_to_process.erase(prof_idx);
306 Vector<int> prof_children = prof_skeleton->get_bone_children(prof_idx);
307 for (int i = 0; i < prof_children.size(); i++) {
308 bones_to_process.push_back(prof_children[i]);
309 }
310
311 // Calc virtual/looking direction with origins.
312 bool is_filtered = false;
313 for (int i = 0; i < filter.size(); i++) {
314 if (String(filter[i]) == prof_skeleton->get_bone_name(prof_idx)) {
315 is_filtered = true;
316 break;
317 }
318 }
319 if (is_filtered) {
320 continue;
321 }
322
323 int src_idx = src_skeleton->find_bone(is_renamed ? prof_skeleton->get_bone_name(prof_idx) : String(bone_map->get_skeleton_bone_name(prof_skeleton->get_bone_name(prof_idx))));
324 if (src_idx < 0 || profile->get_tail_direction(prof_idx) == SkeletonProfile::TAIL_DIRECTION_END) {
325 continue;
326 }
327 Vector3 prof_tail;
328 Vector3 src_tail;
329 if (profile->get_tail_direction(prof_idx) == SkeletonProfile::TAIL_DIRECTION_AVERAGE_CHILDREN) {
330 PackedInt32Array prof_bone_children = prof_skeleton->get_bone_children(prof_idx);
331 int children_size = prof_bone_children.size();
332 if (children_size == 0) {
333 continue;
334 }
335 bool exist_all_children = true;
336 for (int i = 0; i < children_size; i++) {
337 int prof_child_idx = prof_bone_children[i];
338 int src_child_idx = src_skeleton->find_bone(is_renamed ? prof_skeleton->get_bone_name(prof_child_idx) : String(bone_map->get_skeleton_bone_name(prof_skeleton->get_bone_name(prof_child_idx))));
339 if (src_child_idx < 0) {
340 exist_all_children = false;
341 break;
342 }
343 prof_tail = prof_tail + prof_skeleton->get_bone_global_rest(prof_child_idx).origin;
344 src_tail = src_tail + src_skeleton->get_bone_global_rest(src_child_idx).origin;
345 }
346 if (!exist_all_children) {
347 continue;
348 }
349 prof_tail = prof_tail / children_size;
350 src_tail = src_tail / children_size;
351 }
352 if (profile->get_tail_direction(prof_idx) == SkeletonProfile::TAIL_DIRECTION_SPECIFIC_CHILD) {
353 int prof_tail_idx = prof_skeleton->find_bone(profile->get_bone_tail(prof_idx));
354 if (prof_tail_idx < 0) {
355 continue;
356 }
357 int src_tail_idx = src_skeleton->find_bone(is_renamed ? prof_skeleton->get_bone_name(prof_tail_idx) : String(bone_map->get_skeleton_bone_name(prof_skeleton->get_bone_name(prof_tail_idx))));
358 if (src_tail_idx < 0) {
359 continue;
360 }
361 prof_tail = prof_skeleton->get_bone_global_rest(prof_tail_idx).origin;
362 src_tail = src_skeleton->get_bone_global_rest(src_tail_idx).origin;
363 }
364
365 Vector3 prof_head = prof_skeleton->get_bone_global_rest(prof_idx).origin;
366 Vector3 src_head = src_skeleton->get_bone_global_rest(src_idx).origin;
367
368 Vector3 prof_dir = prof_tail - prof_head;
369 Vector3 src_dir = src_tail - src_head;
370 if (Math::is_zero_approx(prof_dir.length_squared()) || Math::is_zero_approx(src_dir.length_squared())) {
371 continue;
372 }
373 prof_dir.normalize();
374 src_dir.normalize();
375
376 // Rotate rest.
377 if (Math::abs(Math::rad_to_deg(src_dir.angle_to(prof_dir))) > float(p_options["retarget/rest_fixer/fix_silhouette/threshold"])) {
378 Basis diff_b = Basis(Quaternion(src_dir, prof_dir));
379 // Apply rotation difference as global transform to skeleton.
380 Basis src_pg;
381 int src_parent = src_skeleton->get_bone_parent(src_idx);
382 if (src_parent >= 0) {
383 src_pg = src_skeleton->get_bone_global_rest(src_parent).basis;
384 }
385 Transform3D fixed_rest = Transform3D(src_pg.inverse() * diff_b * src_pg * src_skeleton->get_bone_rest(src_idx).basis, src_skeleton->get_bone_rest(src_idx).origin);
386 src_skeleton->set_bone_rest(src_idx, fixed_rest);
387 }
388 }
389
390 // Adjust scale base bone height.
391 float base_adjustment = float(p_options["retarget/rest_fixer/fix_silhouette/base_height_adjustment"]);
392 if (!Math::is_zero_approx(base_adjustment)) {
393 StringName scale_base_bone_name = profile->get_scale_base_bone();
394 int src_bone_idx = src_skeleton->find_bone(scale_base_bone_name);
395 if (src_bone_idx >= 0) {
396 Vector3 up_vector = Vector3(0, base_adjustment, 0);
397
398 int src_parent = src_skeleton->get_bone_parent(src_bone_idx);
399 if (src_parent >= 0) {
400 Quaternion global_diff = src_skeleton->get_bone_global_rest(src_parent).basis.get_rotation_quaternion();
401 up_vector = global_diff.xform_inv(up_vector);
402 }
403
404 Transform3D src_rest = src_skeleton->get_bone_rest(src_bone_idx);
405 src_skeleton->set_bone_rest(src_bone_idx, Transform3D(src_rest.basis, src_rest.origin + up_vector));
406
407 TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
408 while (nodes.size()) {
409 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
410 List<StringName> anims;
411 ap->get_animation_list(&anims);
412 for (const StringName &name : anims) {
413 Ref<Animation> anim = ap->get_animation(name);
414 int track_len = anim->get_track_count();
415 for (int i = 0; i < track_len; i++) {
416 if (anim->track_get_path(i).get_subname_count() != 1 || anim->track_get_type(i) != Animation::TYPE_POSITION_3D) {
417 continue;
418 }
419
420 if (anim->track_is_compressed(i)) {
421 continue; // Shouldn't occur in internal_process().
422 }
423
424 String track_path = String(anim->track_get_path(i).get_concatenated_names());
425 Node *node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
426 ERR_CONTINUE(!node);
427
428 Skeleton3D *track_skeleton = Object::cast_to<Skeleton3D>(node);
429 if (!track_skeleton || track_skeleton != src_skeleton) {
430 continue;
431 }
432
433 StringName bn = anim->track_get_path(i).get_concatenated_subnames();
434 if (bn != scale_base_bone_name) {
435 continue;
436 }
437
438 int key_len = anim->track_get_key_count(i);
439 for (int j = 0; j < key_len; j++) {
440 Vector3 pos = static_cast<Vector3>(anim->track_get_key_value(i, j));
441 pos += up_vector;
442 anim->track_set_key_value(i, j, pos);
443 }
444 }
445 }
446 }
447 }
448 }
449
450 // For skin modification in overwrite rest.
451 for (int i = 0; i < src_skeleton->get_bone_count(); i++) {
452 silhouette_diff_w[i] = pre_silhouette_skeleton_global_rest[i] * src_skeleton->get_bone_global_rest(i).affine_inverse();
453 }
454
455 is_rest_changed = true;
456 }
457
458 // Set motion scale to Skeleton if normalize position tracks.
459 if (bool(p_options["retarget/rest_fixer/normalize_position_tracks"])) {
460 int src_bone_idx = src_skeleton->find_bone(profile->get_scale_base_bone());
461 if (src_bone_idx >= 0) {
462 real_t motion_scale = std::abs(src_skeleton->get_bone_global_rest(src_bone_idx).origin.y);
463 if (motion_scale > 0) {
464 src_skeleton->set_motion_scale(motion_scale);
465 }
466 }
467 }
468
469 bool is_using_modifier = int(p_options["retarget/rest_fixer/retarget_method"]) == 2;
470 bool is_using_global_pose = bool(p_options["retarget/rest_fixer/use_global_pose"]);
471 Skeleton3D *orig_skeleton = nullptr;
472 Skeleton3D *profile_skeleton = nullptr;
473
474 // Retarget in some way.
475 if (int(p_options["retarget/rest_fixer/retarget_method"]) > 0) {
476 LocalVector<Transform3D> old_skeleton_rest;
477 LocalVector<Transform3D> old_skeleton_global_rest;
478 for (int i = 0; i < src_skeleton->get_bone_count(); i++) {
479 old_skeleton_rest.push_back(src_skeleton->get_bone_rest(i));
480 old_skeleton_global_rest.push_back(src_skeleton->get_bone_global_rest(i));
481 }
482
483 // Build structure for modifier.
484 if (is_using_modifier) {
485 orig_skeleton = src_skeleton;
486
487 // Duplicate src_skeleton to modify animation tracks, it will memdelele after that animation track modification.
488 src_skeleton = memnew(Skeleton3D);
489 for (int i = 0; i < orig_skeleton->get_bone_count(); i++) {
490 src_skeleton->add_bone(orig_skeleton->get_bone_name(i));
491 src_skeleton->set_bone_rest(i, orig_skeleton->get_bone_rest(i));
492 src_skeleton->set_bone_pose(i, orig_skeleton->get_bone_pose(i));
493 }
494 for (int i = 0; i < orig_skeleton->get_bone_count(); i++) {
495 src_skeleton->set_bone_parent(i, orig_skeleton->get_bone_parent(i));
496 }
497 src_skeleton->set_motion_scale(orig_skeleton->get_motion_scale());
498
499 // Rename orig_skeleton (previous src_skeleton), since it is not animated by animation track with GeneralSkeleton.
500 String original_skeleton_name = String(p_options["retarget/rest_fixer/original_skeleton_name"]);
501 String skel_name = orig_skeleton->get_name();
502 ERR_FAIL_COND_MSG(original_skeleton_name.is_empty(), "Original skeleton name cannot be empty.");
503 ERR_FAIL_COND_MSG(original_skeleton_name == skel_name, "Original skeleton name must be different from unique skeleton name.");
504
505 // Rename profile skeleton to be general skeleton.
506 profile_skeleton = memnew(Skeleton3D);
507 bool is_unique = orig_skeleton->is_unique_name_in_owner();
508 if (is_unique) {
509 orig_skeleton->set_unique_name_in_owner(false);
510 }
511 orig_skeleton->set_name(original_skeleton_name);
512 profile_skeleton->set_name(skel_name);
513 if (is_unique) {
514 profile_skeleton->set_unique_name_in_owner(true);
515 }
516 // Build profile skeleton bones.
517 int len = profile->get_bone_size();
518 for (int i = 0; i < len; i++) {
519 profile_skeleton->add_bone(profile->get_bone_name(i));
520 profile_skeleton->set_bone_rest(i, profile->get_reference_pose(i));
521 }
522 for (int i = 0; i < len; i++) {
523 int target_parent = profile_skeleton->find_bone(profile->get_bone_parent(i));
524 if (target_parent >= 0) {
525 profile_skeleton->set_bone_parent(i, target_parent);
526 }
527 }
528 for (int i = 0; i < len; i++) {
529 Vector3 origin;
530 int found = orig_skeleton->find_bone(profile->get_bone_name(i));
531 String parent_name = profile->get_bone_parent(i);
532 if (found >= 0) {
533 origin = orig_skeleton->get_bone_global_rest(found).origin;
534 if (profile->get_bone_name(i) != profile->get_root_bone()) {
535 int src_parent = -1;
536 while (src_parent < 0 && !parent_name.is_empty()) {
537 src_parent = orig_skeleton->find_bone(parent_name);
538 parent_name = profile->get_bone_parent(profile->find_bone(parent_name));
539 }
540 if (src_parent >= 0) {
541 Transform3D parent_grest = orig_skeleton->get_bone_global_rest(src_parent);
542 origin = origin - parent_grest.origin;
543 }
544 }
545 }
546 int target_parent = profile_skeleton->find_bone(profile->get_bone_parent(i));
547 if (target_parent >= 0) {
548 origin = profile_skeleton->get_bone_global_rest(target_parent).basis.get_rotation_quaternion().xform_inv(origin);
549 }
550 profile_skeleton->set_bone_rest(i, Transform3D(profile_skeleton->get_bone_rest(i).basis, origin));
551 }
552 profile_skeleton->set_motion_scale(orig_skeleton->get_motion_scale());
553 profile_skeleton->reset_bone_poses();
554 // Make structure with modifier.
555 Node *owner = p_node->get_owner();
556
557 Node *pr = orig_skeleton->get_parent();
558 pr->add_child(profile_skeleton);
559 profile_skeleton->set_owner(owner);
560
561 RetargetModifier3D *mod = memnew(RetargetModifier3D);
562 profile_skeleton->add_child(mod);
563 mod->set_owner(owner);
564 mod->set_name("RetargetModifier3D");
565
566 orig_skeleton->set_owner(nullptr);
567 orig_skeleton->reparent(mod, false);
568 orig_skeleton->set_owner(owner);
569 orig_skeleton->set_unique_name_in_owner(true);
570
571 mod->set_use_global_pose(is_using_global_pose);
572 mod->set_profile(profile);
573
574 // Fix skeleton name in animation.
575 // Mapped skeleton is animated by %GenerarSkeleton:RenamedBoneName.
576 // Unmapped skeleton is animated by %OriginalSkeleton:OriginalBoneName.
577 if (is_using_modifier) {
578 TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
579 String general_skeleton_pathname = UNIQUE_NODE_PREFIX + profile_skeleton->get_name();
580 while (nodes.size()) {
581 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
582 List<StringName> anims;
583 ap->get_animation_list(&anims);
584 for (const StringName &name : anims) {
585 Ref<Animation> anim = ap->get_animation(name);
586 int track_len = anim->get_track_count();
587 for (int i = 0; i < track_len; i++) {
588 if (anim->track_get_path(i).get_name_count() == 0) {
589 return;
590 }
591 if (anim->track_get_path(i).get_name(0) == general_skeleton_pathname) {
592 bool replace = false;
593 if (anim->track_get_path(i).get_subname_count() > 0) {
594 int found = profile_skeleton->find_bone(anim->track_get_path(i).get_concatenated_subnames());
595 if (found < 0) {
596 replace = true;
597 }
598 } else {
599 replace = true;
600 }
601 if (replace) {
602 String path_string = UNIQUE_NODE_PREFIX + original_skeleton_name;
603 if (anim->track_get_path(i).get_name_count() > 1) {
604 Vector<StringName> names = anim->track_get_path(i).get_names();
605 names.remove_at(0);
606 for (int j = 0; j < names.size(); j++) {
607 path_string += "/" + names[i].operator String();
608 }
609 }
610 if (anim->track_get_path(i).get_subname_count() > 0) {
611 path_string = path_string + String(":") + anim->track_get_path(i).get_concatenated_subnames();
612 }
613 anim->track_set_path(i, path_string);
614 anim->track_set_imported(i, true);
615 }
616 }
617 }
618 }
619 }
620 }
621 }
622
623 bool keep_global_rest_leftovers = bool(p_options["retarget/rest_fixer/keep_global_rest_on_leftovers"]);
624
625 // Scan hierarchy and populate a whitelist of unmapped bones without mapped descendants.
626 // When both is_using_modifier and is_using_global_pose are enabled, this array is used for detecting warning.
627 Vector<int> keep_bone_rest;
628 if (is_using_modifier || keep_global_rest_leftovers) {
629 Vector<int> bones_to_process = src_skeleton->get_parentless_bones();
630 while (bones_to_process.size() > 0) {
631 int src_idx = bones_to_process[0];
632 bones_to_process.erase(src_idx);
633 Vector<int> src_children = src_skeleton->get_bone_children(src_idx);
634 for (const int &src_child : src_children) {
635 bones_to_process.push_back(src_child);
636 }
637
638 StringName src_bone_name = is_renamed ? StringName(src_skeleton->get_bone_name(src_idx)) : bone_map->find_profile_bone_name(src_skeleton->get_bone_name(src_idx));
639 if (src_bone_name != StringName() && !profile->has_bone(src_bone_name)) {
640 // Scan descendants for mapped bones.
641 bool found_mapped = false;
642
643 Vector<int> descendants_to_process = src_skeleton->get_bone_children(src_idx);
644 while (descendants_to_process.size() > 0) {
645 int desc_idx = descendants_to_process[0];
646 descendants_to_process.erase(desc_idx);
647 Vector<int> desc_children = src_skeleton->get_bone_children(desc_idx);
648 for (const int &desc_child : desc_children) {
649 descendants_to_process.push_back(desc_child);
650 }
651
652 StringName desc_bone_name = is_renamed ? StringName(src_skeleton->get_bone_name(desc_idx)) : bone_map->find_profile_bone_name(src_skeleton->get_bone_name(desc_idx));
653 if (desc_bone_name != StringName() && profile->has_bone(desc_bone_name)) {
654 found_mapped = true;
655 break;
656 }
657 }
658
659 if (!found_mapped) {
660 keep_bone_rest.push_back(src_idx); // No mapped descendants. Add to whitelist.
661 }
662 }
663 }
664 }
665
666 Vector<Basis> diffs;
667 diffs.resize(src_skeleton->get_bone_count());
668 Basis *diffs_w = diffs.ptrw();
669
670 Vector<int> bones_to_process = src_skeleton->get_parentless_bones();
671 while (bones_to_process.size() > 0) {
672 int src_idx = bones_to_process[0];
673 bones_to_process.erase(src_idx);
674 Vector<int> src_children = src_skeleton->get_bone_children(src_idx);
675 for (int i = 0; i < src_children.size(); i++) {
676 bones_to_process.push_back(src_children[i]);
677 }
678
679 Basis tgt_rot;
680 StringName src_bone_name = is_renamed ? StringName(src_skeleton->get_bone_name(src_idx)) : bone_map->find_profile_bone_name(src_skeleton->get_bone_name(src_idx));
681 if (src_bone_name != StringName()) {
682 Basis src_pg;
683 int src_parent_idx = src_skeleton->get_bone_parent(src_idx);
684 if (src_parent_idx >= 0) {
685 src_pg = src_skeleton->get_bone_global_rest(src_parent_idx).basis;
686 }
687 int prof_idx = profile->find_bone(src_bone_name);
688 if (prof_idx >= 0) {
689 // Mapped bone uses reference pose.
690 // It is fine to change rest here even though is_using_modifier is enabled, since next process is aborted with unmapped bones.
691 tgt_rot = src_pg.inverse() * prof_skeleton->get_bone_global_rest(prof_idx).basis;
692 } else if (keep_global_rest_leftovers && keep_bone_rest.has(src_idx)) {
693 // Non-Mapped bones without mapped children keeps global rest.
694 tgt_rot = src_pg.inverse() * old_skeleton_global_rest[src_idx].basis;
695 }
696 }
697
698 if (src_skeleton->get_bone_parent(src_idx) >= 0) {
699 diffs_w[src_idx] = tgt_rot.inverse() * diffs[src_skeleton->get_bone_parent(src_idx)] * src_skeleton->get_bone_rest(src_idx).basis;
700 } else {
701 diffs_w[src_idx] = tgt_rot.inverse() * src_skeleton->get_bone_rest(src_idx).basis;
702 }
703
704 Basis diff;
705 if (src_skeleton->get_bone_parent(src_idx) >= 0) {
706 diff = diffs[src_skeleton->get_bone_parent(src_idx)];
707 }
708 src_skeleton->set_bone_rest(src_idx, Transform3D(tgt_rot, diff.xform(src_skeleton->get_bone_rest(src_idx).origin)));
709 }
710
711 // Fix animation by changing rest.
712 bool warning_detected = false;
713 {
714 TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
715 while (nodes.size()) {
716 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
717 ERR_CONTINUE(!ap);
718 List<StringName> anims;
719 ap->get_animation_list(&anims);
720 for (const StringName &name : anims) {
721 Ref<Animation> anim = ap->get_animation(name);
722 int track_len = anim->get_track_count();
723 for (int i = 0; i < track_len; i++) {
724 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)) {
725 continue;
726 }
727
728 if (anim->track_is_compressed(i)) {
729 continue; // Shouldn't occur in internal_process().
730 }
731
732 String track_path = String(anim->track_get_path(i).get_concatenated_names());
733 Node *node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
734 ERR_CONTINUE(!node);
735
736 Skeleton3D *track_skeleton = Object::cast_to<Skeleton3D>(node);
737 if (!track_skeleton ||
738 (is_using_modifier && track_skeleton != profile_skeleton && track_skeleton != orig_skeleton) ||
739 (!is_using_modifier && track_skeleton != src_skeleton)) {
740 continue;
741 }
742
743 StringName bn = anim->track_get_path(i).get_subname(0);
744 if (!bn) {
745 continue;
746 }
747
748 int bone_idx = src_skeleton->find_bone(bn);
749
750 if (is_using_modifier) {
751 int prof_idx = profile->find_bone(bn);
752 if (prof_idx < 0) {
753 continue; // If is_using_modifier, the original skeleton rest is not changed.
754 } else if (keep_bone_rest.has(bone_idx)) {
755 warning_detected = true;
756 }
757 }
758
759 Transform3D old_rest = old_skeleton_rest[bone_idx];
760 Transform3D new_rest = src_skeleton->get_bone_rest(bone_idx);
761 Transform3D old_pg;
762 Transform3D new_pg;
763 int parent_idx = src_skeleton->get_bone_parent(bone_idx);
764 if (parent_idx >= 0) {
765 old_pg = old_skeleton_global_rest[parent_idx];
766 new_pg = src_skeleton->get_bone_global_rest(parent_idx);
767 }
768
769 int key_len = anim->track_get_key_count(i);
770 if (anim->track_get_type(i) == Animation::TYPE_ROTATION_3D) {
771 Quaternion old_rest_q = old_rest.basis.get_rotation_quaternion();
772 Quaternion new_rest_q = new_rest.basis.get_rotation_quaternion();
773 Quaternion old_pg_q = old_pg.basis.get_rotation_quaternion();
774 Quaternion new_pg_q = new_pg.basis.get_rotation_quaternion();
775 for (int j = 0; j < key_len; j++) {
776 Quaternion qt = static_cast<Quaternion>(anim->track_get_key_value(i, j));
777 anim->track_set_key_value(i, j, new_pg_q.inverse() * old_pg_q * qt * old_rest_q.inverse() * old_pg_q.inverse() * new_pg_q * new_rest_q);
778 }
779 } else if (anim->track_get_type(i) == Animation::TYPE_SCALE_3D) {
780 Basis old_rest_b_inv = old_rest.basis.inverse();
781 Basis new_rest_b = new_rest.basis;
782 Basis old_pg_b = old_pg.basis;
783 Basis new_pg_b = new_pg.basis;
784 Basis old_pg_b_inv = old_pg.basis.inverse();
785 Basis new_pg_b_inv = new_pg.basis.inverse();
786 for (int j = 0; j < key_len; j++) {
787 Basis sc = Basis().scaled(static_cast<Vector3>(anim->track_get_key_value(i, j)));
788 anim->track_set_key_value(i, j, (new_pg_b_inv * old_pg_b * sc * old_rest_b_inv * old_pg_b_inv * new_pg_b * new_rest_b).get_scale());
789 }
790 } else {
791 Vector3 old_rest_o = old_rest.origin;
792 Vector3 new_rest_o = new_rest.origin;
793 Basis old_pg_b = old_pg.basis;
794 Basis new_pg_b = new_pg.basis;
795 for (int j = 0; j < key_len; j++) {
796 Vector3 ps = static_cast<Vector3>(anim->track_get_key_value(i, j));
797 anim->track_set_key_value(i, j, new_pg_b.xform_inv(old_pg_b.xform(ps - old_rest_o)) + new_rest_o);
798 }
799 }
800 }
801 }
802 }
803 }
804 if (is_using_global_pose && warning_detected) {
805 // TODO:
806 // Theoretically, if A and its conversion are calculated correctly taking into account the difference in the number of bones,
807 // there is no need to disable use_global_pose, but this is probably a fairly niche case.
808 WARN_PRINT_ED("Animated extra bone between mapped bones detected, consider disabling Use Global Pose option to prevent that the pose origin be overridden by the RetargetModifier3D.");
809 }
810
811 if (p_options.has("retarget/rest_fixer/reset_all_bone_poses_after_import") && !bool(p_options["retarget/rest_fixer/reset_all_bone_poses_after_import"])) {
812 // If Reset All Bone Poses After Import is disabled, preserve the original bone pose, adjusted for the new bone rolls.
813 for (int bone_idx = 0; bone_idx < src_skeleton->get_bone_count(); bone_idx++) {
814 Transform3D old_rest = old_skeleton_rest[bone_idx];
815 Transform3D new_rest = src_skeleton->get_bone_rest(bone_idx);
816 Transform3D old_pg;
817 Transform3D new_pg;
818 int parent_idx = src_skeleton->get_bone_parent(bone_idx);
819 if (parent_idx >= 0) {
820 old_pg = old_skeleton_global_rest[parent_idx];
821 new_pg = src_skeleton->get_bone_global_rest(parent_idx);
822 }
823
824 Quaternion old_pg_q = old_pg.basis.get_rotation_quaternion();
825 Quaternion new_pg_q = new_pg.basis.get_rotation_quaternion();
826 Quaternion qt = src_skeleton->get_bone_pose_rotation(bone_idx);
827 src_skeleton->set_bone_pose_rotation(bone_idx, new_pg_q.inverse() * old_pg_q * qt * old_rest.basis.get_rotation_quaternion().inverse() * old_pg_q.inverse() * new_pg_q * new_rest.basis.get_rotation_quaternion());
828
829 Basis sc = Basis().scaled(src_skeleton->get_bone_pose_scale(bone_idx));
830 src_skeleton->set_bone_pose_scale(bone_idx, (new_pg.basis.inverse() * old_pg.basis * sc * old_rest.basis.inverse() * old_pg.basis.inverse() * new_pg.basis * new_rest.basis).get_scale());
831 Vector3 ps = src_skeleton->get_bone_pose_position(bone_idx);
832 src_skeleton->set_bone_pose_position(bone_idx, new_pg_q.xform_inv(old_pg_q.xform(ps - old_rest.origin)) + new_rest.origin);
833 }
834 }
835
836 if (is_using_modifier) {
837 memdelete(src_skeleton);
838 src_skeleton = profile_skeleton;
839 }
840
841 is_rest_changed = true;
842 }
843
844 // Scale position tracks by motion scale if normalize position tracks.
845 if (bool(p_options["retarget/rest_fixer/normalize_position_tracks"])) {
846 TypedArray<Node> nodes = p_base_scene->find_children("*", "AnimationPlayer");
847 while (nodes.size()) {
848 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(nodes.pop_back());
849 List<StringName> anims;
850 ap->get_animation_list(&anims);
851 for (const StringName &name : anims) {
852 Ref<Animation> anim = ap->get_animation(name);
853 int track_len = anim->get_track_count();
854 for (int i = 0; i < track_len; i++) {
855 if (anim->track_get_path(i).get_subname_count() != 1 || anim->track_get_type(i) != Animation::TYPE_POSITION_3D) {
856 continue;
857 }
858
859 if (anim->track_is_compressed(i)) {
860 continue; // Shouldn't occur in internal_process().
861 }
862
863 String track_path = String(anim->track_get_path(i).get_concatenated_names());
864 Node *node = (ap->get_node(ap->get_root_node()))->get_node(NodePath(track_path));
865 ERR_CONTINUE(!node);
866
867 Skeleton3D *track_skeleton = Object::cast_to<Skeleton3D>(node);
868 if (!track_skeleton ||
869 (is_using_modifier && track_skeleton != profile_skeleton && track_skeleton != orig_skeleton) ||
870 (!is_using_modifier && track_skeleton != src_skeleton)) {
871 continue;
872 }
873
874 real_t mlt = 1 / src_skeleton->get_motion_scale();
875 int key_len = anim->track_get_key_count(i);
876 for (int j = 0; j < key_len; j++) {
877 Vector3 pos = static_cast<Vector3>(anim->track_get_key_value(i, j));
878 anim->track_set_key_value(i, j, pos * mlt);
879 }
880 }
881 }
882 }
883 }
884
885 if (!is_using_modifier && is_rest_changed) {
886 // Fix skin.
887 {
888 HashSet<Ref<Skin>> mutated_skins;
889 TypedArray<Node> nodes = p_base_scene->find_children("*", "ImporterMeshInstance3D");
890 while (nodes.size()) {
891 ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(nodes.pop_back());
892 ERR_CONTINUE(!mi);
893
894 Ref<Skin> skin = mi->get_skin();
895 if (skin.is_null()) {
896 continue;
897 }
898 if (mutated_skins.has(skin)) {
899 continue;
900 }
901 mutated_skins.insert(skin);
902
903 Node *node = mi->get_node(mi->get_skeleton_path());
904 ERR_CONTINUE(!node);
905
906 Skeleton3D *mesh_skeleton = Object::cast_to<Skeleton3D>(node);
907 if (!mesh_skeleton || mesh_skeleton != src_skeleton) {
908 continue;
909 }
910
911 int skin_len = skin->get_bind_count();
912 for (int i = 0; i < skin_len; i++) {
913 StringName bn = skin->get_bind_name(i);
914 int bone_idx = src_skeleton->find_bone(bn);
915 if (bone_idx >= 0) {
916 Transform3D adjust_transform = src_skeleton->get_bone_global_rest(bone_idx).affine_inverse() * silhouette_diff[bone_idx].affine_inverse() * pre_silhouette_skeleton_global_rest[bone_idx];
917 adjust_transform.scale(global_transform.basis.get_scale_global());
918 skin->set_bind_pose(i, adjust_transform * skin->get_bind_pose(i));
919 }
920 }
921 }
922 nodes = src_skeleton->get_children();
923 while (nodes.size()) {
924 BoneAttachment3D *attachment = Object::cast_to<BoneAttachment3D>(nodes.pop_back());
925 if (attachment == nullptr) {
926 continue;
927 }
928 int bone_idx = attachment->get_bone_idx();
929 if (bone_idx == -1) {
930 bone_idx = src_skeleton->find_bone(attachment->get_bone_name());
931 }
932 ERR_CONTINUE(bone_idx < 0 || bone_idx >= src_skeleton->get_bone_count());
933 Transform3D adjust_transform = src_skeleton->get_bone_global_rest(bone_idx).affine_inverse() * silhouette_diff[bone_idx].affine_inverse() * pre_silhouette_skeleton_global_rest[bone_idx];
934 adjust_transform.scale(global_transform.basis.get_scale_global());
935
936 TypedArray<Node> child_nodes = attachment->get_children();
937 while (child_nodes.size()) {
938 Node3D *child = Object::cast_to<Node3D>(child_nodes.pop_back());
939 if (child == nullptr) {
940 continue;
941 }
942 child->set_transform(adjust_transform * child->get_transform());
943 }
944 }
945 }
946
947 if (!p_options.has("retarget/rest_fixer/reset_all_bone_poses_after_import") || bool(p_options["retarget/rest_fixer/reset_all_bone_poses_after_import"])) {
948 // Init skeleton pose to new rest.
949 for (int i = 0; i < src_skeleton->get_bone_count(); i++) {
950 Transform3D fixed_rest = src_skeleton->get_bone_rest(i);
951 src_skeleton->set_bone_pose_position(i, fixed_rest.origin);
952 src_skeleton->set_bone_pose_rotation(i, fixed_rest.basis.get_rotation_quaternion());
953 src_skeleton->set_bone_pose_scale(i, fixed_rest.basis.get_scale());
954 }
955 if (orig_skeleton) {
956 for (int i = 0; i < orig_skeleton->get_bone_count(); i++) {
957 Transform3D fixed_rest = orig_skeleton->get_bone_rest(i);
958 orig_skeleton->set_bone_pose_position(i, fixed_rest.origin);
959 orig_skeleton->set_bone_pose_rotation(i, fixed_rest.basis.get_rotation_quaternion());
960 orig_skeleton->set_bone_pose_scale(i, fixed_rest.basis.get_scale());
961 }
962 }
963 }
964 }
965
966 memdelete(prof_skeleton);
967 }
968 }