How to access/change VisualShader Uniform variables from within a script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Brinux

I would love to know how to access/change a visual shader material’s uniform parameters from within a GD script.
Scenario: I create a visual shader material for a given mesh, and I want to tweak the material from my GD script for a given game event.

I assume I go to

$sphere.mesh.surface_get_material(0).  #....something goes here

… and after that I have no idea how to access the “shader_param/AlphaKnob” or whatever other uniform variable I’ve put in there. I can easily change in in the editor, I just can’t figure out how to access it from code (script).

Can someone give me the syntax, or better, an example of how I would get at this uniform variable?

Thanks in advance :slight_smile:

Specific example: I have a visual shader with a ScalarUniform connected to the Alpha output. I named it “AlphaKnob”. AlphaKnob is now exposed in the editor, and I can change it in the editor and that works great.
Now I want to change “AlphaKnob” from within a GDscript.
The “float over” text says it’s called “shader_param/AlphaKnob”.
What am I typing into my GDscript to change the value of AlphaKnob to 0.5, or anything else ?

:bust_in_silhouette: Reply From: imjp94

Basically there’s two ways to do it:

So for the record, that worked. Here’s the commands:

so if the texture is on the NODE, you would use:

$sphere.get_surface_material(0).set("shader_param/AlphaKnob", 0.5)

and if the texture is on the MESH, (like it is when it comes over from Blender via gLTF2), you would use:

$sphere.mesh.surface_get_material(0).set("shader_param/AlphaKnob", 0.5)

Also note that the commands are different depending on whether the texture is on the mesh or on the node. The “0” in both cases refers to the material in that slot, so if you have materials 0,1,2 etc., you’re referencing the correct one (but if you only have one material, it’s 0.

Brinux | 2020-03-31 15:40