@@ -494,7 +494,13 @@ func create_scene(params):
494
494
else :
495
495
printerr ("Failed to pack scene: " + str (result ))
496
496
printerr ("Error code: " + str (result ))
497
- quit (1 )
497
+ quit (1 ) # Quit for pack error
498
+
499
+ # --- Final Cleanup for create_scene ---
500
+ if scene_root and is_instance_valid (scene_root ):
501
+ if debug_mode : print ("Freeing instantiated scene root node at end of create_scene" )
502
+ scene_root .free ()
503
+ # --- End Final Cleanup ---
498
504
499
505
# Add a node to an existing scene
500
506
func add_node (params ):
@@ -572,26 +578,43 @@ func add_node(params):
572
578
if debug_mode :
573
579
print ("Pack result: " + str (result ) + " (OK=" + str (OK ) + ")" )
574
580
581
+ var save_error = ERR_CANT_CREATE # Initialize with an error state
582
+
575
583
if result == OK :
584
+ absolute_scene_path = ProjectSettings .globalize_path (full_scene_path ) # Ensure absolute path is defined here (Removed var)
576
585
if debug_mode :
577
586
print ("Saving scene to: " + absolute_scene_path )
578
- var save_error = ResourceSaver .save (packed_scene , absolute_scene_path )
587
+ save_error = ResourceSaver .save (packed_scene , absolute_scene_path ) # Use absolute path for saving
579
588
if debug_mode :
580
589
print ("Save result: " + str (save_error ) + " (OK=" + str (OK ) + ")" )
590
+
581
591
if save_error == OK :
592
+ # Simplified success message for clarity
593
+ print ("Node '" + params .node_name + "' of type '" + params .node_type + "' added successfully" )
594
+ # Add a small delay after successful save before cleanup
582
595
if debug_mode :
583
- var file_check_after = FileAccess .file_exists (absolute_scene_path )
584
- print ("File exists check after save: " + str (file_check_after ))
585
- if file_check_after :
586
- print ("Node '" + params .node_name + "' of type '" + params .node_type + "' added successfully" )
587
- else :
588
- printerr ("File reported as saved but does not exist at: " + absolute_scene_path )
589
- else :
590
- print ("Node '" + params .node_name + "' of type '" + params .node_type + "' added successfully" )
596
+ print ("Waiting briefly after save..." )
597
+ OS .delay_msec (100 ) # 100ms delay
591
598
else :
592
599
printerr ("Failed to save scene: " + str (save_error ))
600
+ # No quit here, proceed to cleanup
593
601
else :
594
602
printerr ("Failed to pack scene: " + str (result ))
603
+ # No quit here, proceed to cleanup
604
+
605
+ # --- Start Cleanup ---
606
+ # Removed packed_scene.free() as PackedScene is RefCounted
607
+ # Reinstate scene_root.free() to prevent RID leak
608
+ if scene_root and is_instance_valid (scene_root ):
609
+ if debug_mode :
610
+ print ("Freeing instantiated scene root node after delay" )
611
+ scene_root .free () # Free the instantiated scene root
612
+ # --- End Cleanup ---
613
+
614
+ # Quit only if there was a critical error during packing or saving
615
+ if result != OK or save_error != OK :
616
+ quit (1 ) # Exit with error code if packing or saving failed
617
+ # Otherwise, the script will naturally exit after _init finishes
595
618
596
619
# Load a sprite into a Sprite2D node
597
620
func load_sprite (params ):
@@ -725,6 +748,12 @@ func load_sprite(params):
725
748
else :
726
749
printerr ("Failed to pack scene: " + str (result ))
727
750
751
+ # --- Final Cleanup for load_sprite ---
752
+ if scene_root and is_instance_valid (scene_root ):
753
+ if debug_mode : print ("Freeing instantiated scene root node at end of load_sprite" )
754
+ scene_root .free ()
755
+ # --- End Final Cleanup ---
756
+
728
757
# Export a scene as a MeshLibrary resource
729
758
func export_mesh_library (params ):
730
759
print ("Exporting MeshLibrary from scene: " + params .scene_path )
@@ -904,6 +933,12 @@ func export_mesh_library(params):
904
933
else :
905
934
printerr ("No valid meshes found in the scene" )
906
935
936
+ # --- Final Cleanup for export_mesh_library ---
937
+ if scene_root and is_instance_valid (scene_root ):
938
+ if debug_mode : print ("Freeing instantiated scene root node at end of export_mesh_library" )
939
+ scene_root .free ()
940
+ # --- End Final Cleanup ---
941
+
907
942
# Find files with a specific extension recursively
908
943
func find_files (path , extension ):
909
944
var files = []
@@ -1222,3 +1257,9 @@ func save_scene(params):
1222
1257
printerr ("Failed to save scene: " + str (error ))
1223
1258
else :
1224
1259
printerr ("Failed to pack scene: " + str (result ))
1260
+
1261
+ # --- Final Cleanup for save_scene ---
1262
+ if scene_root and is_instance_valid (scene_root ):
1263
+ if debug_mode : print ("Freeing instantiated scene root node at end of save_scene" )
1264
+ scene_root .free ()
1265
+ # --- End Final Cleanup ---
0 commit comments