Type mismatch for diffuseColor

Based on the example from https://docs.omniverse.nvidia.com/dev-guide/latest/programmer_ref/usd/materials/create-mdl-material.html

I use this code:

from pxr import Sdf, UsdShade
import omni.usd

stage = omni.usd.get_context().get_stage()
mtl_path = Sdf.Path("/World/Looks/PreviewSurface")
mtl = UsdShade.Material.Define(stage, mtl_path)
shader = UsdShade.Shader.Define(stage, mtl_path.AppendPath("Shader"))
shader.CreateIdAttr("UsdPreviewSurface")
shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set([1.0, 0.0, 0.0])
shader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(0.5)
shader.CreateInput("metallic", Sdf.ValueTypeNames.Float).Set(0.0)
mtl.CreateSurfaceOutput().ConnectToSource(shader.ConnectableAPI(), "surface")

and get this error

2024-06-07 15:37:54  [Error] [omni.kit.app.plugin] 	Error in 'pxrInternal_v0_22__pxrReserved__::UsdStage::_SetValueImpl' at line 6189 in file W:\ac88d7d902b57417\USD\pxr\usd\usd\stage.cpp : 'Type mismatch for </World/Looks/PreviewSurface/Shader.inputs:diffuseColor>: expected 'GfVec3f', got 'vector<VtValue,allocator<VtValue> >''```

Hey,

shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set([1.0, 0.0, 0.0]) needs to be
shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set(Gf.Vec3f(1.0, 0.0, 0.0))

Also make sure to include Gf in your imports so it would read: from pxr import Sdf, UsdShade, Gf.

Full script will look like the following:

from pxr import Sdf, UsdShade, Gf
import omni.usd

stage = omni.usd.get_context().get_stage()
mtl_path = Sdf.Path("/World/Looks/PreviewSurface")
mtl = UsdShade.Material.Define(stage, mtl_path)
shader = UsdShade.Shader.Define(stage, mtl_path.AppendPath("Shader"))
shader.CreateIdAttr("UsdPreviewSurface")
shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set(Gf.Vec3f(1.0, 0.0, 0.0))
shader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(0.5)
shader.CreateInput("metallic", Sdf.ValueTypeNames.Float).Set(0.0)
mtl.CreateSurfaceOutput().ConnectToSource(shader.ConnectableAPI(), "surface")

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.