Need to nudge the scene to get the material to render after updating the material through the API

I am attempting to use the API to set the properties of a PBR material. It works, but I need to “nudge” the scene to get the changes to the material to render in the viewport. Here is an example. Let’s say I want to change the base color of a material:
api.scene.set({from: api.instanceId, id: '9add860b-3579-4419-9f93-ffdb076ac0ab', plug: 'Material', property: 'baseColor'}, {r:0.9999, g:0, b: 0})
That material will not appear as red until I hide or show some object in the scene. Hence, my nudge description of the behavior.
Is there an API function to force the update?
Thanks in advance,
Jem

Hey @jem -

I just set up a simple example with a cube to see if I could reproduce the issue. I used your code (and swapped in my own IDs) and found that using the baseColor parameter would return a ‘No matching query’ warning.

Looking at the full object after running scene.getAll() I saw that there was a defaultColor, and using that rather than baseColor in my script worked as I expected.

Does your scenegraph show baseColor or defaultColor property in the object?

I see what you are saying about the defaultColor property. I think that I need to set up a simpler test scene for myself. Other scripts in my scene may be causing the confusion. I will report back. Thanks for the advice!

1 Like

Keep me posted! Happy to help however I can.

Hi Will. Sorry for the delay. Had to work a trade show last week.
I had the most challenging time reproducing your suggestion. Eventually, I got it to work, but I realized we were not achieving the same result.
The defaultColor property you manipulated is a property of the PolyMesh. The baseColor property that I was trying to manipulate is a property of a material. defaultColor is only used if a PolyMesh has no material assigned to it. My Box had a material assigned, so I didn’t see an effect.
I need to use PolyMeshes with materials assigned, so I will rephrase my original question:
“If I have a PolyMesh with a material, how may I use the API to change the material’s properties?”
Thanks again,
Jem

Hey @jem thanks for your patience here, your reply slipped by and thanks for the further explanation. I was able to replicate - I have a feeling it’s happening because the material is fetched & loaded initially and when you set the color of the referenced material, there is no ‘hot reloading’ so-to-say of the material so it waits for something to rerender it.

Do you have any objection to making the referenced material configurable? You can add a color-type attribute to the setup and manipulate it a bit more easily with the Configurator API.

Hi @Will ,
I was going down that exact path as you made your last post.
I am biased towards the “one script to rule them all” approach because the other 3D solutions I have used in the past behaved that way. But I don’t have to do it that way.
I just set up a simple example where the main Box asset has a script. The Box script parses incoming parameters from the website and does whatever it needs to do to the Box asset, but it does not try to manipulate the material asset directly. Rather, it sets a global attribute value using the configurator.setConfiguration() API. This attribute change event is picked up by the script or logic in the material asset, and the material updates accordingly. This seems to work well. There is no real downside to this. It just didn’t occur to me.
The only mild criticism that I might have is this technique spreads the logic out over a few files or screens making comprehending the project as a whole slightly more challenging. Of course, I could make the opposite argument and say that it makes projects a bit more modular.
Thank you for looking into it.
-Jem

1 Like