@@ -3431,11 +3431,43 @@ void EditorSceneFormatImporterESCN::get_extensions(List<String> *r_extensions) c
34313431 r_extensions->push_back (" escn" );
34323432}
34333433
3434+ int get_text_format_version (String p_path) {
3435+ Error error;
3436+ Ref<FileAccess> f = FileAccess::open (p_path, FileAccess::READ , &error);
3437+ ERR_FAIL_COND_V_MSG (error != OK || f.is_null (), -1 , " Cannot open file '" + p_path + " '." );
3438+ String line = f->get_line ().strip_edges ();
3439+ // skip empty lines and comments
3440+ while (line.is_empty () || line.begins_with (" ;" )) {
3441+ line = f->get_line ().strip_edges ();
3442+ if (f->eof_reached ()) {
3443+ break ;
3444+ }
3445+ }
3446+ int format_index = line.find (" format" );
3447+ ERR_FAIL_COND_V_MSG (format_index == -1 , -1 , " No format specifier in file '" + p_path + " '." );
3448+ String format_str = line.substr (format_index).get_slicec (' =' , 1 ).strip_edges ();
3449+ ERR_FAIL_COND_V_MSG (!format_str.substr (0 , 1 ).is_numeric (), -1 , " Invalid format in file '" + p_path + " '." );
3450+ int format = format_str.to_int ();
3451+ return format;
3452+ }
3453+
34343454Node *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) {
34353455 Error error;
3456+ int format = get_text_format_version (p_path);
3457+ ERR_FAIL_COND_V (format == -1 , nullptr );
34363458 Ref<PackedScene> ps = ResourceFormatLoaderText::singleton->load (p_path, p_path, &error);
34373459 ERR_FAIL_COND_V_MSG (ps.is_null (), nullptr , " Cannot load scene as text resource from path '" + p_path + " '." );
34383460 Node *scene = ps->instantiate ();
3461+ ERR_FAIL_COND_V (!scene, nullptr );
3462+ if (format == 2 ) {
3463+ TypedArray<Node> skel_nodes = scene->find_children (" *" , " AnimationPlayer" );
3464+ for (int32_t node_i = 0 ; node_i < skel_nodes.size (); node_i++) {
3465+ // Force re-compute animation tracks.
3466+ AnimationPlayer *player = cast_to<AnimationPlayer>(skel_nodes[node_i]);
3467+ ERR_CONTINUE (!player);
3468+ player->advance (0 );
3469+ }
3470+ }
34393471 TypedArray<Node> nodes = scene->find_children (" *" , " MeshInstance3D" );
34403472 for (int32_t node_i = 0 ; node_i < nodes.size (); node_i++) {
34413473 MeshInstance3D *mesh_3d = cast_to<MeshInstance3D>(nodes[node_i]);
@@ -3444,9 +3476,22 @@ Node *EditorSceneFormatImporterESCN::import_scene(const String &p_path, uint32_t
34443476 // Ignore the aabb, it will be recomputed.
34453477 ImporterMeshInstance3D *importer_mesh_3d = memnew (ImporterMeshInstance3D);
34463478 importer_mesh_3d->set_name (mesh_3d->get_name ());
3447- importer_mesh_3d->set_transform (mesh_3d->get_relative_transform (mesh_3d->get_parent ()));
3448- importer_mesh_3d->set_skin (mesh_3d->get_skin ());
3479+ Node *parent = mesh_3d->get_parent ();
3480+ Transform3D rel_transform = mesh_3d->get_relative_transform (parent);
3481+ if (rel_transform == Transform3D () && parent && parent != mesh_3d) {
3482+ // If we're here, we probably got a "data.parent is null" error
3483+ // Node3D.data.parent hasn't been set yet but Node.data.parent has, so we need to get the transform manually
3484+ Node3D *parent_3d = mesh_3d->get_parent_node_3d ();
3485+ if (parent == parent_3d) {
3486+ rel_transform = mesh_3d->get_transform ();
3487+ } else if (parent_3d) {
3488+ rel_transform = parent_3d->get_relative_transform (parent) * mesh_3d->get_transform ();
3489+ } // Otherwise, parent isn't a Node3D.
3490+ }
3491+ importer_mesh_3d->set_transform (rel_transform);
3492+ Ref<Skin> skin = mesh_3d->get_skin ();
34493493 importer_mesh_3d->set_skeleton_path (mesh_3d->get_skeleton_path ());
3494+ importer_mesh_3d->set_skin (skin);
34503495 Ref<ArrayMesh> array_mesh_3d_mesh = mesh_3d->get_mesh ();
34513496 if (array_mesh_3d_mesh.is_valid ()) {
34523497 // For the MeshInstance3D nodes, we need to convert the ArrayMesh to an ImporterMesh specially.
@@ -3487,7 +3532,5 @@ Node *EditorSceneFormatImporterESCN::import_scene(const String &p_path, uint32_t
34873532 }
34883533 }
34893534
3490- ERR_FAIL_NULL_V (scene, nullptr );
3491-
34923535 return scene;
34933536}
0 commit comments