コンテンツにスキップ

Variantの挙動を調べる_1

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

[123]:

from pxr import Usd,Sdf

[124]:

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

[125]:

vset = prim.GetVariantSets().AddVariantSet('hogehoge')
vset.AddVariant('A')
vset.AddVariant('B')
vset.AddVariant('C')
print(vset)

Success

<pxr.Usd.VariantSet object at 0x000002C0014160B8>

[126]:

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

Success

['hogehoge']

[127]:

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

Success

A
Usd.Prim(</VariantSet>)

[128]:

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

[129]:

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

[130]:

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

[131]:

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

Success

#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"

        }
    }
}

[132]:

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

Success

#usda 1.0
(
    doc = """Generated from Composed Stage of root layer 
"""
)

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

[133]:

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

Success

#usda 1.0
(
    doc = """Generated from Composed Stage of root layer 
"""
)

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

[134]:

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

Success

#usda 1.0
(
    doc = """Generated from Composed Stage of root layer 
"""
)

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

[135]:

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

Success

True

[136]:

prim.GetPrimStack()

Success

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

[138]:

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

Success

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

[139]:

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

[140]:

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

Success

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

最終更新日: 2020-04-27 15:05:19