Skip to content

Referenceをつくる

ipynbFile UsdReference2__Referenceをつくる.ipynb

In [16]:

1
from pxr import Usd,Sdf

In [10]:

1
2
stage = Usd.Stage.CreateInMemory()
prim = stage.DefinePrim("/sphereTest")

In [11]:

1
2
# ファイルを指定してリファレンスを追加
prim.GetReferences().AddReference(f"{os.getcwd()}/usd/sphere.usda")

Success

1
True

In [20]:

1
2
3
layer = Sdf.Layer.FindOrOpen("usd/sphere.usda")
# 中身
print(layer.ExportToString())

Success

1
2
3
4
5
6
7
8
#usda 1.0
(
    defaultPrim = "sphere"
)

def Sphere "sphere"
{
}

In [13]:

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

Success

1
2
3
4
5
6
7
#usda 1.0

def "sphereTest" (
    prepend references = @c:\reincarnation_tech\notebooks\USD\CompArc/usd/sphere.usda@
)
{
}

In [12]:

1
2
# Flattenした結果
print(stage.Flatten().ExportToString())

Success

1
2
3
4
5
6
7
8
9
#usda 1.0
(
    doc = """Generated from Composed Stage of root layer 
"""
)

def Sphere "sphereTest"
{
}

In [21]:

1
primB = stage.DefinePrim('/referenceCube')

In [25]:

1
2
# DefaultPrimが指定されていないような場合
print(Sdf.Layer.FindOrOpen(f"{os.getcwd()}/usd/cube.usda").ExportToString())

Success

1
2
3
4
5
6
7
8
#usda 1.0

def Xform "root"
{
    def Cube "sampleCube"
    {
    }
}

In [22]:

1
2
# AddReferenceで、どのPrimをリファレンスするのか指定する
primB.GetReferences().AddReference(f"{os.getcwd()}/usd/cube.usda",Sdf.Path("/root"))

Success

1
True

In [23]:

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

Success

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#usda 1.0

def "sphereTest" (
    prepend references = @c:\reincarnation_tech\notebooks\USD\CompArc/usd/sphere.usda@
)
{
}

def "referenceCube" (
    prepend references = @c:\reincarnation_tech\notebooks\USD\CompArc/usd/cube.usda@</root>
)
{
}

In [27]:

1
2
3
4
5
refPrim = stage.DefinePrim("/refPrim")
refPrim.CreateAttribute('sampleValue',Sdf.ValueTypeNames.String).Set("hogehoge")
primC = stage.DefinePrim("/addRef")
# 現在のStage以下のPrimをリファレンス
primC.GetReferences().AddInternalReference(refPrim.GetPath())

Success

1
True

In [29]:

1
print(stage.Flatten().ExportToString())

Success

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#usda 1.0
(
    doc = """Generated from Composed Stage of root layer 
"""
)

def Sphere "sphereTest"
{
}

def "referenceCube"
{
}

def "refPrim"
{
    custom string sampleValue = "hogehoge"
}

def "addRef"
{
    custom string sampleValue = "hogehoge"
}