How to reference my Imported Mesh in GDscript to change the Material

Godot Version

Godot 4.2.1 Stable

Question

Hey there! I’m currently working on a character customization system in Godot 3D. I wanted to start off by making the skin tone changeable, so we’re trying by making a system that flips through an array of 3 materials via the
surface material override on our meshes.

I setup my customization scene with a background and imported in the scene of my model.
I tried making variables to refer to the meshes of my Base model (which has
been imported in as a Kinematic Body 3D with a collision shape, in case that changes anything here.)

I couldn’t use normal variables, so the engine told me to use @onready var for them all.
However, when I went to test my scene I get an error for every single piece of the mesh, 11 pieces, 11 errors, saying Node not found at the end of each error.
I made it so the scene of my model lets you edit the children and copied the node paths for each piece of the model (the pieces that would be affected by the skin tone change, like the head and other limbs.)

Here is my script :

extends Control

const baseAngel = preload(“res://Meshes/Angels/Base_Angel.glb”)

#Skin
var skinTones: Array = [“res://Meshes/Angels/SkinTones/Misc/Base.tres”,“res://Meshes/Angels/SkinTones/Tan/Tan1.tres”,“res://Meshes/Angels/SkinTones/Dark/Dark1.tres”]
var skinToneIndex: int = 0

#Body
@onready var Head = $Root/Base_Angel/Armature/Skeleton3D/Head
@onready var Ears = $Root/Base_Angel/Armature/Skeleton3D/Ears/Ears
@onready var LeftArm = $Root/Base_Angel/Armature/Skeleton3D/LeftArm
@onready var LeftFoot = $Root/Base_Angel/Armature/Skeleton3D/LeftFoot
@onready var LeftHand = $Root/Base_Angel/Armature/Skeleton3D/LeftHand
@onready var LeftLeg = $Root/Base_Angel/Armature/Skeleton3D/LeftLeg
@onready var RightArm = $Root/Base_Angel/Armature/Skeleton3D/RightArm
@onready var RightFoot = $Root/Base_Angel/Armature/Skeleton3D/RightFoot
@onready var RightHand = $Root/Base_Angel/Armature/Skeleton3D/RightHand
@onready var RightLeg = $Root/Base_Angel/Armature/Skeleton3D/RightLeg
@onready var Wings = $Root/Base_Angel/Armature/Skeleton3D/Wings

Called when the node enters the scene tree for the first time.

func _ready():
update_skin_tone()

func update_skin_tone():
var currentSkinTone = skinTones[skinToneIndex]

#Changing skin tones via Surface Material Overrride 
Head.material_override = currentSkinTone
Ears.material_override = currentSkinTone
LeftArm.material_override = currentSkinTone
LeftFoot.material_override = currentSkinTone
LeftHand.material_override = currentSkinTone
LeftLeg.material_override = currentSkinTone
RightArm.material_override = currentSkinTone 
RightFoot.material_override = currentSkinTone
RightHand.material_override = currentSkinTone
RightLeg.material_override = currentSkinTone

#Function called when player presses the “Skin” Button
func _on_skin_tone_pressed():
skinToneIndex = (skinToneIndex +1) % skinTones.size()

update_skin_tone()

My model, “Base_Angel” is a glb imported and saved as a scene as a Kinematic Body3D and the script is attached to a button node that is made to change the skin tones.

I noticed that all my variables are listed as null as well, as if it’s not being recognized at all…
I’ve tried a few different approaches like having the angel be local to the customization screen, but I really am stumped! For now I gave the Base_Angel scene an “editable children” setting, so I could see the pieces I needed to reference.
I just don’t get how to reference my imported mesh in GDscript for my variables.

Thank you so much for reading, this is my first 3D project as well, so this is new territory!

If you can post your project somewhere, I would be happy to have a look. Maybe I can spot something.

I really appreciate your help–hopefully this link works out alright. Should have all the files to my project. The scene named “NewPal_AC1.tscn” which is in the Alter Creation folder should have all the relevant script and scenes. So it should be Scenes → Game and finally the Alter Creation folder! The base angel scene and it’s glb file should be in the meshes folder along with the skin tone materials which are also split up into folders! If you have any questions let me know thank you again!

I got it and am looking it over :slight_smile:


Got something for you. writing post will take a bit.

  1. Hold ctrl and drag a node into your code and Godot will write the @onready for you.
    (Beware that it will use the same names as your nodes, so you will have to edit the names of the vars so that it fits how you use them later in the code.)
@onready var Ears = $"../../../../Base_Angel/Armature/Skeleton3D/Ears/Ears"
@onready var Head = $"../../../../Base_Angel/Armature/Skeleton3D/Head"
@onready var LeftArm = $"../../../../Base_Angel/Armature/Skeleton3D/LeftArm"
@onready var LeftFoot = $"../../../../Base_Angel/Armature/Skeleton3D/LeftFoot"
@onready var LeftHand = $"../../../../Base_Angel/Armature/Skeleton3D/LeftHand"
@onready var LeftLeg = $"../../../../Base_Angel/Armature/Skeleton3D/LeftLeg"
@onready var RightArm = $"../../../../Base_Angel/Armature/Skeleton3D/RightArm"
@onready var RightFoot = $"../../../../Base_Angel/Armature/Skeleton3D/RightFoot"
@onready var RightHand = $"../../../../Base_Angel/Armature/Skeleton3D/RightHand"
@onready var RightLeg = $"../../../../Base_Angel/Armature/Skeleton3D/RightLeg"
@onready var Wings = $"../../../../Base_Angel/Armature/Skeleton3D/Wings"
  1. Add the extra line to this func:
func update_skin_tone():
	var currentSkinToneFile = skinTones[skinToneIndex]
	var currentSkinTone = load(currentSkinToneFile) # <-- this one
  1. Re-wire your skin button to the right place:
    image
    Edit the signal.


Connect it to the correct script.

  1. Run the scene (F6) and if all is well, the skin button seems to work

:blush:

Wow, thank you so much! I was going crazy trying to figure this out! I’ll have to add your name to the credits once this whole crazy thing is out of development!

Your solution works perfectly!

Hope you have a great day! :grin:

1 Like

Hey, it was actually a pleasure. Enjoy your developing :innocent:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.