How to modify via UITrait.hx the Image Texture in Shader Nodes?

Is it possible to modify the filename of the Image Texture from Iron for this node ?

(for example, using armory script2d example,

—>>you set the filename via the ui like in this picture;

–>> We can find the “project.addAssets(“assets/checker_rough.png”);” line inside the khafile.js

–>> Thus in your UITrait.hx you have a h.text that contains the filename of your image (see code below );

     ...
        if (ui.window(Id.handle(), 20, 20, 230, 600, true)) {
        // Make panel in this window
        if (ui.panel(Id.handle({selected: true}), "montest")) {
            ui.indent();
            
            // ouverture fichier
            var h = Id.handle();
            ui.row([1/2, 1/2]);
            ui.button("Cancel");
            if (ui.button("Load")) {
                
             // le h.text contient notre fichier image 
	     trace(h.text);

Thus the question is how can we then use this h.text to set the filename in the Shader Node inside the UITrait.hx where we have got the h.text ?

There is no nice way yet, but will fix that soon with material parameters feature (#451).

Going the low-level way, we could do something like this:

// Load image asynchronously
var imagePath = "path/to/some/image.png";
iron.data.Data.getImage(imagePath, function(image:kha.Image) {
	// Image is now loaded
	// Get active scene
	var scene = iron.Scene.active;
	// Get mesh object named "Cube"
	var cube = scene.getMesh("Cube");
	// Get first material
	var mat = cube.materials[0];
	// Overwrite the first texture linked to this material
	mat.contexts[0].textures[0]	= image;
});
1 Like