@@ -3426,11 +3426,43 @@ void EditorSceneFormatImporterESCN::get_extensions(List<String> *r_extensions) c
34263426 r_extensions->push_back (" escn" );
34273427}
34283428
3429+ int get_text_format_version (String p_path) {
3430+ Error error;
3431+ Ref<FileAccess> f = FileAccess::open (p_path, FileAccess::READ , &error);
3432+ ERR_FAIL_COND_V_MSG (error != OK || f.is_null (), -1 , " Cannot open file '" + p_path + " '." );
3433+ String line = f->get_line ().strip_edges ();
3434+ // skip empty lines and comments
3435+ while (line.is_empty () || line.begins_with (" ;" )) {
3436+ line = f->get_line ().strip_edges ();
3437+ if (f->eof_reached ()) {
3438+ break ;
3439+ }
3440+ }
3441+ int format_index = line.find (" format" );
3442+ ERR_FAIL_COND_V_MSG (format_index == -1 , -1 , " No format specifier in file '" + p_path + " '." );
3443+ String format_str = line.substr (format_index).get_slicec (' =' , 1 ).strip_edges ();
3444+ ERR_FAIL_COND_V_MSG (!format_str.substr (0 , 1 ).is_numeric (), -1 , " Invalid format in file '" + p_path + " '." );
3445+ int format = format_str.to_int ();
3446+ return format;
3447+ }
3448+
34293449Node *EditorSceneFormatImporterESCN::import_scene (const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err) {
34303450 Error error;
3451+ int format = get_text_format_version (p_path);
3452+ ERR_FAIL_COND_V (format == -1 , nullptr );
34313453 Ref<PackedScene> ps = ResourceFormatLoaderText::singleton->load (p_path, p_path, &error);
34323454 ERR_FAIL_COND_V_MSG (ps.is_null (), nullptr , " Cannot load scene as text resource from path '" + p_path + " '." );
34333455 Node *scene = ps->instantiate ();
3456+ ERR_FAIL_COND_V (!scene, nullptr );
3457+ if (format == 2 ) {
3458+ TypedArray<Node> skel_nodes = scene->find_children (" *" , " AnimationPlayer" );
3459+ for (int32_t node_i = 0 ; node_i < skel_nodes.size (); node_i++) {
3460+ // Force re-compute animation tracks.
3461+ AnimationPlayer *player = cast_to<AnimationPlayer>(skel_nodes[node_i]);
3462+ ERR_CONTINUE (!player);
3463+ player->advance (0 );
3464+ }
3465+ }
34343466 TypedArray<Node> nodes = scene->find_children (" *" , " MeshInstance3D" );
34353467 for (int32_t node_i = 0 ; node_i < nodes.size (); node_i++) {
34363468 MeshInstance3D *mesh_3d = cast_to<MeshInstance3D>(nodes[node_i]);
@@ -3439,9 +3471,22 @@ Node *EditorSceneFormatImporterESCN::import_scene(const String &p_path, uint32_t
34393471 // Ignore the aabb, it will be recomputed.
34403472 ImporterMeshInstance3D *importer_mesh_3d = memnew (ImporterMeshInstance3D);
34413473 importer_mesh_3d->set_name (mesh_3d->get_name ());
3442- importer_mesh_3d->set_transform (mesh_3d->get_relative_transform (mesh_3d->get_parent ()));
3443- importer_mesh_3d->set_skin (mesh_3d->get_skin ());
3474+ Node *parent = mesh_3d->get_parent ();
3475+ Transform3D rel_transform = mesh_3d->get_relative_transform (parent);
3476+ if (rel_transform == Transform3D () && parent && parent != mesh_3d) {
3477+ // If we're here, we probably got a "data.parent is null" error
3478+ // Node3D.data.parent hasn't been set yet but Node.data.parent has, so we need to get the transform manually
3479+ Node3D *parent_3d = mesh_3d->get_parent_node_3d ();
3480+ if (parent == parent_3d) {
3481+ rel_transform = mesh_3d->get_transform ();
3482+ } else if (parent_3d) {
3483+ rel_transform = parent_3d->get_relative_transform (parent) * mesh_3d->get_transform ();
3484+ } // Otherwise, parent isn't a Node3D.
3485+ }
3486+ importer_mesh_3d->set_transform (rel_transform);
3487+ Ref<Skin> skin = mesh_3d->get_skin ();
34443488 importer_mesh_3d->set_skeleton_path (mesh_3d->get_skeleton_path ());
3489+ importer_mesh_3d->set_skin (skin);
34453490 Ref<ArrayMesh> array_mesh_3d_mesh = mesh_3d->get_mesh ();
34463491 if (array_mesh_3d_mesh.is_valid ()) {
34473492 // For the MeshInstance3D nodes, we need to convert the ArrayMesh to an ImporterMesh specially.
@@ -3482,7 +3527,5 @@ Node *EditorSceneFormatImporterESCN::import_scene(const String &p_path, uint32_t
34823527 }
34833528 }
34843529
3485- ERR_FAIL_NULL_V (scene, nullptr );
3486-
34873530 return scene;
34883531}
0 commit comments