# Set parameters as texture or color in NodeMaterial via code

**URL:** <https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946>\
**Category:** Questions\
**Tags:** material, custom-node-material\
**Created:** [January 4, 2024, 3:45pm UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946 "2024-01-04T15:45:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lauris47](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@lauris47](https://forum.babylonjs.com/u/lauris47)\
**Post date:** [January 4, 2024, 3:45pm UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946/1 "2024-01-04T15:45:18Z")

</div>

Hello,

I want to change NodeMaterial texture or any value.  
As I understood, it has to be done via NodeMaterial Block

I seen many examples where it is done like this:

```auto
block.texture = ...
block.value = ...

```

However, I cannot acces these values in my code, it says:

```auto
Property 'texture' does not exist on type 'NodeMaterialBlock'

```

Here is my code:

```auto
                    BABYLON.NodeMaterial.ParseFromFileAsync("lightmappedgeneric", "/master_materials/nodeMaterial.json", scene).then((nodeMaterial) => {
                        lightmappedgeneric = nodeMaterial;
                    });

                    var cloneMat: BABYLON.NodeMaterial = lightmappedgeneric.clone("CloneOfMaster");
                    var block = cloneMat.getBlockByPredicate((b) => b.name === "diffuseTex");
                    if(block)
                    {
                        block.texture = orgTex;
                    }

```

It works here:

> [@Setting Node Material Textures Dyanmically](https://forum.babylonjs.com/t/setting-node-material-textures-dyanmically/13268):
>
> This is probably an easy question, but let’s say I have a Node Material snippet I load as a JSON file. const nodeMaterial = new BABYLON.NodeMaterial("mat", scene); nodeMaterial.loadAsync("http://127.0.0.1:5000/pbr.json").then(() =\> { console.log(nodeMaterial.getBlockByName('Bump texture')); }); What I want to be able to do is update the bump texture to a dynamic URL. Is there an example of this somewhere?

> [@Interaction with a shader from the scene?](https://forum.babylonjs.com/t/interaction-with-a-shader-from-the-scene/9194):
>
> Node Material Editor has customizable inputs. But I can’t understand how to control them from the scene? Unfortunately I did not find the answer in the existing video tutorials and forum topics. I assumed that they can be used as attributes of a material, after a dot. But that doesn’t work like that. I would really appreciate it if anyone could share an example. Thanks!

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 4, 2024, 5:02pm UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946/2 "2024-01-04T17:02:50Z")

</div>

ParseFromFileAsync is an async function so you need to await it or move the using code in the callback.

---

<div class="post-metadata">

**Author:** ![lauris47](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@lauris47](https://forum.babylonjs.com/u/lauris47)\
**Post date:** [January 4, 2024, 5:26pm UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946/3 "2024-01-04T17:26:47Z")

</div>

Thanks. I don’t think that is the problem.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/c/5cd078cb0dc0676d56f557edc7bd6d6757e39fa5.png)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 4, 2024, 5:30pm UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946/4 "2024-01-04T17:30:43Z")

</div>

You need to also cast the block to the correct block type in typescript to be sure the texture property is present as well as the value in the other cases.

---

<div class="post-metadata">

**Author:** ![lauris47](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@lauris47](https://forum.babylonjs.com/u/lauris47)\
**Post date:** [January 9, 2024, 3:51pm UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946/5 "2024-01-09T15:51:36Z")

</div>

Thank you, that worked.

```auto
var property = nodeMaterial.getBlockByName("DiffuseTexture");
var textBlock = property as BABYLON.TextureBlock;

```

---

<div class="post-metadata">

**Author:** ![lauris47](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@lauris47](https://forum.babylonjs.com/u/lauris47)\
**Post date:** [January 18, 2024, 9:14am UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946/6 "2024-01-18T09:14:51Z")

</div>

What about color, vector and float blocks, how do I access and modify them?  
I tried this:

```auto
let blockInShader = cloneMat.getInputBlockByPredicate((b) => b.name === matchedBlockName);
let colorBlock = blockInShader as BABYLON.Color4;

```

Second line complains:

> Conversion of type ‘InputBlock’ to type ‘Color4’ may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to ‘unknown’ first.  
> Type ‘InputBlock’ is missing the following properties from type ‘Color4’: r, g, b, a, and 24 more  
> Then I add as suggested:  
> `let colorBlock = blockInShader as unknown as BABYLON.Color4;`

But I don’t think it is correct. Later I set the color via:

```auto
colorBlock = new BABYLON.Color4(1,0,0,0);

```

It does not work, but no errors either.

---

<div class="post-metadata">

**Author:** ![lauris47](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@lauris47](https://forum.babylonjs.com/u/lauris47)\
**Post date:** [January 18, 2024, 9:23am UTC](https://forum.babylonjs.com/t/set-parameters-as-texture-or-color-in-nodematerial-via-code/46946/7 "2024-01-18T09:23:12Z")

</div>

Found out:

```auto
let blockInShader = cloneMat.getInputBlockByPredicate((b) => b.name === matchedBlockName);
if (blockInShader) {
   blockInShader.value = new BABYLON.Color3(1, 0, 0); 
}

```
