コンテンツにスキップ

USDでTextureAssign

ipynbFile USDTextureAssign__USDでTextureAssign.ipynb

In [1]:

1
from pxr import Gf, Kind, Sdf, Usd, UsdGeom, UsdShade

In [7]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
stage = Usd.Stage.CreateInMemory()
UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.y)

modelRoot = UsdGeom.Xform.Define(stage, "/TexModel")
Usd.ModelAPI(modelRoot).SetKind(Kind.Tokens.component)
billboard = UsdGeom.Mesh.Define(stage, "/TexModel/card")
billboard.CreatePointsAttr([(-430, -145, 0), (430, -145, 0), (430, 145, 0), (-430, 145, 0)])
billboard.CreateFaceVertexCountsAttr([4])
billboard.CreateFaceVertexIndicesAttr([0,1,2,3])
billboard.CreateExtentAttr([(-430, -145, 0), (430, 145, 0)])
texCoords = billboard.CreatePrimvar("st", 
                                    Sdf.ValueTypeNames.TexCoord2fArray, 
                                    UsdGeom.Tokens.varying)
texCoords.Set([(0, 0), (1, 0), (1,1), (0, 1)])

Success

1
True

In [8]:

1
2
3
4
5
6
7
8
9
material = UsdShade.Material.Define(stage, '/TexModel/boardMat')

pbrShader = UsdShade.Shader.Define(stage, '/TexModel/boardMat/PBRShader')
pbrShader.CreateIdAttr("UsdPreviewSurface")
pbrShader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(0.4)
pbrShader.CreateInput("metallic", Sdf.ValueTypeNames.Float).Set(0.0)
pbrShader.CreateInput("opacity",Sdf.ValueTypeNames.Float).Set(1.0)

material.CreateSurfaceOutput().ConnectToSource(pbrShader, "surface")

Success

1
True

In [9]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
stReader = UsdShade.Shader.Define(stage, '/TexModel/boardMat/PBRShader/stReader')
stReader.CreateIdAttr('UsdPrimvarReader_float2')

diffuseTextureSampler = UsdShade.Shader.Define(stage,'/TexModel/boardMat/PBRShader/diffuseTexture')
diffuseTextureSampler.CreateIdAttr('UsdUVTexture')
diffuseTextureSampler.CreateInput('file', Sdf.ValueTypeNames.Asset).Set("D:/USD_Logo.png")
diffuseTextureSampler.CreateInput("st", Sdf.ValueTypeNames.Float2).ConnectToSource(stReader, 'result')
diffuseTextureSampler.CreateOutput('rgb', Sdf.ValueTypeNames.Float3)
diffuseTextureSampler.CreateOutput('a', Sdf.ValueTypeNames.Float)
pbrShader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).ConnectToSource(diffuseTextureSampler, 'rgb')
pbrShader.CreateInput("opacity", Sdf.ValueTypeNames.Float).ConnectToSource(diffuseTextureSampler, 'a')

Success

1
True

In [11]:

1
2
3
4
stInput = material.CreateInput('frame:stPrimvarName', Sdf.ValueTypeNames.Token)
stInput.Set('st')

stReader.CreateInput('varname',Sdf.ValueTypeNames.Token).ConnectToSource(stInput)

Success

1
True

In [12]:

1
2
3
UsdShade.MaterialBindingAPI(billboard).Bind(material)

stage.Export("D:/addTexture.usda")

Success

1
True