Setting custom Material ID through API's createMaterial

We’re trying to create custom Materials through the Viewer API, which we can then assign to nodes with their material ID.

The problem is that these seem to be randomly generated when the API creates the material, making it difficult to use with assignMaterial. We’ve looked around and tried setting this to a custom value (similar to setting the name, which does work), but material ID doesn’t seem to work in quite the same way.

Is there a specific ID selector within the createMaterial function we’re missing, or is there perhaps a way to assign this material through, say, its name instead of ID?

Any help welcome.

Hi @360Fabriek,
If I understand correctly, you create a fresh material with the API and then want to assign that material to all nodes which have a certain material ID? If that’s the case, it’s easier to modify that material, instead of creating a new one.
You’d find the material by name with the help of getMaterialList, edit the channels of that material and push it back into the scene with setMaterial
Or am I misinterpreting what you’re trying to achieve?

Hey @klaasnienhuis,

We’re trying to assign new materials (not in model, made through API) to a node, while maintaining the possibility to reset to its original material.
If we were to change the existing materials directly through the API, there wouldn’t be a good way to reset them to their original, the way we have set it up now.

i.e. an original EmitColor value of [0, 0, 0] looking a specific way. When altered through the API (to the same value of [0, 0, 0]), the EmitColor ends up solid black, instead of its original color.

This is why we’d prefer (if it were possible) to create extra materials and assign these to Nodes, while being able to reset to the Node’s original material.

Chances are we’re missing something here, and if it’s possible to use the first option (changing existing material values, without ruining reset possibility), we’d gladly go with that as well.

I see.
Since the assignMaterial method works with nodes, you could first analyze the scene to create collections of nodes with specific material IDs. It’s a bit of scene management, but it should be doable. Something like this

get all materials
make a map of the materials with the material ID and material name
get all nodes
split nodes into arrays with the same material ID

Now you can find all nodes with the same material by material name

1 Like