Initializing a custom class variable with import script

Godot Version

4.2.1

Question

Hello.

I am trying to fill an Array inside a custom class script with data gathered during import.

I’ve added the export annotation to the variable so I can see in the editor that the size of the array is bigger than zero.

However when _snapToGrid function is called then print(nodes.size()) prints to the output zero on each call.

I have only one object of the new type, so there is no chance I am using the wrong one.

What am I doing wrong? I’m out of ideas.

This is my script with custom class:

class_name Scene
extends Node

@export var nodes : Array

# Called when the node enters the scene tree for the first time.
func _ready():
	pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

func _snapToGrid(vec3):
	print(nodes.size())

The import script:

@tool
extends EditorScenePostImport

func _post_import(scene):
	...
	var verts:Array = []
	...
	scene.set_script(load("res://scripts/scene.gd"))
	...
			scene.nodes.assign(verts)
	...
	return scene

You probably need to initialize the array like @export var nodes : Array = [] and probably set that script to be a @tool script too if you want to run anything from it in the editor.