After introducing how to run scripts in the initial post, we would like to explain the usage of materials.
In general, a material contains the parameters of the Look and of the Stellar representation. Depending on the currently selected render mode, only the relevant parameters are displayed in the UI. Via Python, you can always access both parameter sets. Let us have a deeper look at both representations.
Stellar materials
Let’s assume we have an open scene and want to add a Stellar material to the model look library:
At this point in our script, we have access to the actual Stellar RenderMaterial. There are no dedicated methods for accessing all of the contained parameters. Instead, a JSON object can be retrieved, which holds the parameters of the material:
Now you can e.g. change the albedo color:
After having applied all changes to the JSON object, just update the material description:
Now we can see the correctly applied albedo color in the UI. Please consider that we have set the RGB color in linear colorspace. This is why need to switch to this colorspace in the UI as well to see the same values:
The code line, where we set the color value, still looks like magic. How will I know how the properties in the JSON data structure are named? The solution is quite simple: You can print the material description to a file and open it in a JSON editor:
As a result, you get the following output:
Now you can easily navigate through the JSON representation and identify the right keys.
Look materials
Working with look materials does not significantly differ from the workflow with Stellar materials. First, we need to get the actual RenderMaterial of the Look:
Afterwards, we access the parameters via the JSON object, which we initialize with material description:
We changed the diffuse color by passing the hsv values of the color. The hue needs to be specified in the range from 0 to 360, and saturation and value must be in the range from 0 to 1. Hurray, we are done!
Use case: Resetting all texture slots of Look materials
Let’s conclude the introduction with a typical use case: We have migrated a Look scene to Stellar by setting up the Stellar materials, assigning proper colors, textures etc. Now, we want to share the scene with a colleague. Hence, we trigger “Save for distribution” to ensure that we only pass on the model and the used textures. Afterwards, we figured out that the look representations still hold textures, which are not needed anymore. How can I easily reset all Look texture slots of my materials, before I run “Save for Distribution”?
Let’s write a script! First, we need to traverse all materials in the Model library of the active scene and call a helper function “removeLookTextureUsages” for every material:
Let’s inspect this helper function: It accesses, like in the previous examples, the JSON description of the material. All subdictionaries for the texture samplers are passed to the resetTexture function. This clears the file name and sets the “IsActive” property of the sampler to False.
That’s it!
Final notes
The complete scripts are attached to this post as well. They can be directly executed with DELTAGEN 2022x. To run them with older versions of DELTAGEN, they need to be slightly modified, because the APIs have been unified and simplified in DELTAGEN 2022x.
PYTHON SCRIPTING
