コンテンツにスキップ

USDでMaterial_ShadingNetwork

ipynbFile USDMaterial_01__USDでMaterial_ShadingNetwork.ipynb

In [32]:

1
2
import os.path
from pxr import Usd, UsdGeom, Sdf, Gf, UsdShade

In [33]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
stage = Usd.Stage.CreateInMemory()
rootLayer = stage.GetRootLayer()

# Mesh作成
sphere = UsdGeom.Sphere.Define(stage, '/test/sphere')

matPath = Sdf.Path("/Model/Material/MyMat")
mat = UsdShade.Material.Define(stage, matPath)
shader = UsdShade.Shader.Define(stage, matPath.AppendChild('testShader'))


# Shaderのアトリビュート設定
# 色をつけただけの基本のPBRシェーダーを作る
shader.CreateIdAttr('UsdPreviewSurface')
shader.CreateInput('diffuseColor', Sdf.ValueTypeNames.Color3f)
shader.CreateInput('metalic', Sdf.ValueTypeNames.Float).Set(0.9)
shader.CreateInput('roughness', Sdf.ValueTypeNames.Float).Set(0.2)
# カスタムパラメーターもつくれる
shader.CreateInput('test', Sdf.ValueTypeNames.Float).Set(1.0)

# Shaderの結果をMatにつなげる
mat.CreateSurfaceOutput().ConnectToSource(shader, "surface")
# 別のノードを作って、接続する
colorPrim = UsdShade.Shader.Define(stage, '/node/outColor')
colorOut = colorPrim.CreateInput('color', Sdf.ValueTypeNames.Color3f)
colorOut.Set(Gf.Vec3f(1, 0, 0))
shader.GetInput('diffuseColor').ConnectToSource(colorOut)

# Bind
UsdShade.MaterialBindingAPI(sphere.GetPrim()).Bind(mat)

Success

1
True

In [34]:

1
2
3
print(stage.GetRootLayer().ExportToString())

stage.GetRootLayer().Export("D:/matTest.usda")

Success

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#usda 1.0

def "test"
{
    def Sphere "sphere"
    {
        rel material:binding = </Model/Material/MyMat>
    }
}

def "Model"
{
    def "Material"
    {
        def Material "MyMat"
        {
            token outputs:surface.connect = </Model/Material/MyMat/testShader.outputs:surface>

            def Shader "testShader"
            {
                uniform token info:id = "UsdPreviewSurface"
                color3f inputs:diffuseColor.connect = </node/outColor.inputs:color>
                float inputs:metalic = 0.9
                float inputs:roughness = 0.2
                float inputs:test = 1
                token outputs:surface
            }
        }
    }
}

def "node"
{
    def Shader "outColor"
    {
        color3f inputs:color = (1, 0, 0)
    }
}

Success

1
True


こんな感じ?


ShadingNetworkも CreateInput と ConnectToSource で表現できる