コンテンツにスキップ

Variantの挙動を調べる_1

ipynbFile variantset01__Variantの挙動を調べる_1.ipynb

In [123]:

1
from pxr import Usd,Sdf

In [124]:

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

In [125]:

1
2
3
4
5
vset = prim.GetVariantSets().AddVariantSet('hogehoge')
vset.AddVariant('A')
vset.AddVariant('B')
vset.AddVariant('C')
print(vset)

Success

1
<pxr.Usd.VariantSet object at 0x000002C0014160B8>

In [126]:

1
2
# VariantSetを取得
prim.GetVariantSets().GetNames()

Success

1
['hogehoge']

In [127]:

1
2
3
4
5
6
vset = prim.GetVariantSets().GetVariantSet("hogehoge")
vset.SetVariantSelection('A')
# 今選択されているものを表示
print(vset.GetVariantSelection())
# VariatnSetがあるPrimを取得
print(vset.GetPrim())

Success

1
2
A
Usd.Prim(</VariantSet>)

In [128]:

1
2
3
4
vset.SetVariantSelection('A')
with vset.GetVariantEditContext():
    # VariantSet「A」を選んでいる時には VariantSet/hoge というPrimができあがる
    childPrim = stage.DefinePrim(prim.GetPath().AppendChild("hoge"))

In [129]:

1
2
3
4
vset.SetVariantSelection('B')
with vset.GetVariantEditContext():
    childPrim = stage.DefinePrim(prim.GetPath().AppendChild("hogeB"))
    childPrim.GetReferences().AddReference(r"D:\work\usd_py36\usd\layerB.usda")

In [130]:

1
2
3
vset.SetVariantSelection('C')
with vset.GetVariantEditContext():
    prim.CreateAttribute("TEST",Sdf.ValueTypeNames.String).Set("HOGE")

In [131]:

1
print(stage.GetRootLayer().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
24
25
26
27
28
29
30
#usda 1.0

def "VariantSet" (
    variants = {
        string hogehoge = "C"
    }
    prepend variantSets = "hogehoge"
)
{
    variantSet "hogehoge" = {
        "A" {
            def "hoge"
            {
            }

        }
        "B" {
            def "hogeB" (
                prepend references = @D:\work\usd_py36\usd\layerB.usda@
            )
            {
            }

        }
        "C" {
            custom string TEST = "HOGE"

        }
    }
}

In [132]:

1
2
vset.SetVariantSelection('A')
print(stage.Flatten().ExportToString())

Success

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

def "VariantSet"
{
    def "hoge"
    {
    }
}

In [133]:

1
2
vset.SetVariantSelection('B')
print(stage.Flatten().ExportToString())

Success

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

def "VariantSet"
{
    def "hogeB"
    {
    }
}

In [134]:

1
2
vset.SetVariantSelection('C')
print(stage.Flatten().ExportToString())

Success

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

def "VariantSet"
{
    custom string TEST = "HOGE"
}

In [135]:

1
stage.GetRootLayer().Export("D:/test.usda")

Success

1
True

In [136]:

1
prim.GetPrimStack()

Success

1
2
[Sdf.Find('anon:000002C06682DA70:tmp.usda', '/VariantSet'),
 Sdf.Find('anon:000002C06682DA70:tmp.usda', '/VariantSet{hogehoge=C}')]

In [138]:

1
2
3
vset.SetVariantSelection('B')
a = stage.GetPrimAtPath("/VariantSet/hogeB")
print(a.GetPrimStack())

Success

1
[Sdf.Find('anon:000002C06682DA70:tmp.usda', '/VariantSet{hogehoge=B}hogeB')]

In [139]:

1
spec = a.GetPrimStack()[0] #PrimSpecを取得できる

In [140]:

1
2
3
# PrimSpecからReferenceのUSDAは取得できるっぽい
for ref in spec.referenceList.prependedItems:
    print(ref.assetPath)

Success

1
D:\work\usd_py36\usd\layerB.usda