Creating a material and changing its color

I created a cube primitive and now I want to create a material and bind it. It seems that creating a material from stage.DefinePrim is a bit of a hassle as one needs to create a shader and connect things. So I did it through the kit commands instead.

mtl_created = []
omni.kit.commands.execute("CreateAndBindMdlMaterialFromLibrary",
            mdl_name="OmniPBR.mdl",
            mtl_name="OmniPBR",
            mtl_created_list = mtl_created)
mtl_path = mtl_created[0]
omni.kit.commands.execute(
            "BindMaterial",
            prim_path=cube.GetPath(),
            material_path=mtl_path
        )

How do I change its diffuse color though? I tried using the attribute from the shader but it cant be found.

shader = self._stage.GetPrimAtPath(mtl_path + "/Shader")
color_attr = shader.GetAttribute("inputs:diffuse_color_constant")
color_attr.Set(Gf.Vec3f(0.6, 0.13, 0.13))

I also tried using the command but thats not working and even it were working it needs to know what the color was previously for some reason.

 omni.kit.commands.execute(
            "ChangeProperty",
            prop_path=mtl_path + "/Shader.inputs:diffuse_color_constant",
            value = (0.76, 0.15, 0.15),
            prev = (0.2, 0.2, 0.2)
        )
1 Like

Hi parrotdora,

please check this part of the documentation, it might help you out.

Kindly,
Liila

Given the documentation you provided, what is the corresponding code (python) then to simply change the color of a primitive object?

This document will be helpful for that: Scene Setup — Omniverse IsaacSim latest documentation