コンテンツにスキップ

USDのインスタンス

ipynbFile usd_instance__USDのインスタンス.ipynb

In [1]:

1
from pxr import Usd,UsdGeom,Sdf

In [2]:

1
stage = Usd.Stage.Open("D:/Kitchen_set/Kitchen_set_instanced.usd")

In [44]:

1
2
3
4
5
for prim in stage.Traverse():
    if prim.IsInstance():
        # instanceable なPrimを列挙する
        # Primから、InstanceのPrototype(ベース)のPrimを取得する
        protoPrim = prim.GetPrototype()

In [4]:

1
2
3
4
# instanceable が指定されたPrimは、
# StageにPrototypePrimとして登録される。
prototypes = stage.GetPrototypes()
print(prototypes[0].IsPrototype())

Success

1
True

In [7]:

1
2
3
4
5
canA = stage.GetPrimAtPath('/Kitchen_set/Props_grp/West_grp/TinCanA_1')
canB = stage.GetPrimAtPath('/Kitchen_set/Props_grp/West_grp/TinCanA_2')
# 同じモデルなので、Prototypeは共通
print(canA.GetPrototype())
print(canB.GetPrototype())

Success

1
2
Usd.Prim(</__Prototype_2>)
Usd.Prim(</__Prototype_2>)

In [2]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
stage = Usd.Stage.CreateInMemory()

primA = stage.DefinePrim("/sampleA")
primA.GetReferences().AddReference('./cube.usd')
primA.SetInstanceable(True)
primB = stage.DefinePrim("/sampleB")
primB.SetInstanceable(True)
primB.GetReferences().AddReference('./cube.usd')

primC = stage.DefinePrim("/sampleC")
primC.GetReferences().AddReference('./cube.usd')

# Instanceの場合はChildはみつからない
print(primB.GetChildren())
# 普通なら見つかる
print(primC.GetChildren())

Success

1
2
[]
[Usd.Prim(</sampleC/cube>)]

In [6]:

1
2
3
4
prototype = primA.GetPrototype()

for instance in prototype.GetInstances():
    print(instance)

Success

1
2
Usd.Prim(</sampleA>)
Usd.Prim(</sampleB>)

In [47]:

1
stage.GetRootLayer().Export("./instance.usd")

Success

1
True

In [38]:

1
2
for i in primB.GetPrototype().GetChildren():
    print(i)

Success

1
Usd.Prim(</__Prototype_1/cube>)

In [49]:

1
2
prototypes = stage.GetPrototypes()
prototype = prototypes[0]

In [50]:

1
prototype.GetInstances()

Success

1
[Usd.Prim(</sampleA>), Usd.Prim(</sampleB>)]

In [61]:

1
2
3
4
# InstanceのPrimも GetPrimAtPathで取得が可能
proxy = stage.GetPrimAtPath("/sampleA/cube")
# Proxyの場合、値の追加や変更を使用するとエラーになる
proxy.CreateAttribute('sample',Sdf.ValueTypeNames.String).Set('hoge')

Error


ErrorException Traceback (most recent call last)

in
2 proxy = stage.GetPrimAtPath("/sampleA/cube")
3 # Proxyの場合、値の追加や変更を使用するとエラーになる
----> 4 proxy.CreateAttribute('sample',Sdf.ValueTypeNames.String).Set('hoge')

ErrorException:
Error in 'pxrInternal_v0_21__pxrReserved__::UsdStage::_ValidateEditPrim' at line 1297 in file D:\work\GithubRepo\USD\pxr\usd\usd\stage.cpp : 'Cannot create property spec at path ; authoring to an instance proxy is not allowed.'

In [66]:

1
2
3
# ProxyからPrototype側でのPathを取得
protoPrim = proxy.GetPrimInPrototype()
protoPrim.CreateAttribute('sample',Sdf.ValueTypeNames.String).Set('hoge')

Error


ErrorException Traceback (most recent call last)

in
1 # ProxyからPrototype側でのPathを取得
2 protoPrim = proxy.GetPrimInPrototype()
----> 3 protoPrim.CreateAttribute('sample',Sdf.ValueTypeNames.String).Set('hoge')

ErrorException:
Error in 'pxrInternal_v0_21__pxrReserved__::UsdStage::_ValidateEditPrim' at line 1290 in file D:\work\GithubRepo\USD\pxr\usd\usd\stage.cpp : 'Cannot create property spec at path ; authoring to an instancing prototype is not allowed.'