Add a prim to the layer of the stage selected with maya_usd. It has the same name but is added with a different name.
print("add_primxform_for_stage_layer")
import maya.cmds as cmds
from maya import cmds
import mayaUsd.ufe
from pxr import Usd, UsdGeom
def get_stage(stagePath):
import mayaUsd.ufe
stage = mayaUsd.ufe.getStage(stagePath)
print("stage= "+str(stage))
# レイヤーを取得
layer = stage.GetRootLayer()
print("layer= "+str(layer))
return stage
def get_selected_maya_usd_stage():
selectedlist=cmds.ls(sl=True,long=1)
print("selectedlist= "+str(selectedlist))
selectedZero=selectedlist[0]
selectedZeroType=cmds.objectType( selectedZero )
print("selectedZeroType= "+str(selectedZeroType))
usd_stage=""
stagePath=""
if(str(selectedZeroType)=="mayaUsdProxyShape"):
layerPath=selectedZero
usd_stage=get_stage(layerPath)
elif(str(selectedZeroType)=="transform"):
layerPath="|stage"+stageNumStr+"|stageShape"+stageNumStr
cmds.select(layerPath)
usd_stage=get_stage(layerPath)
else:
print("not stage!!!!")
return usd_stage,layerPath
def Exist_Prim(prim_path):
from maya import cmds
from mayaUsd import ufe, lib
from pxr import Sdf
# Primが存在するかどうかを確認
#primPath = Sdf.Path('/path/to/your/prim')
primPath = Sdf.Path(prim_path)
primExist = stage.GetPrimAtPath(primPath)
if primExist:
print('Primが存在します')
else:
print('Primが存在しません')
return primExist
def usd_increment_name(name,existing_names):
new_name = name
i = 1
# in演算子 new_name が existing_names に存在する場合はTrueを、そうでない場合はFalseを返します。
while new_name in existing_names:
new_name = name+str(i)
print("存在します。"+str(new_name))
i += 1
else:
print("while out")
return new_name
print("----start-----")
def add_prim_xform_for_stage_layer():
stage,layerPath = get_selected_maya_usd_stage()
# レイヤーを取得
layer = stage.GetRootLayer()
print("layer= "+str(layer))
# Primを作成
prim_count=1
prim_path = "/Xform"+str(prim_count)
newPrimStagePath=layerPath+","+prim_path
print("newPrimStagePath= "+newPrimStagePath)
Exist_Prim(newPrimStagePath)
# レイヤー内のすべてのPrimを取得
all_prims = stage.Traverse()
print("all_prims= "+str(all_prims))
# Primのリストを出力
all_primName_list=[]
for prim in all_prims:
print(prim.GetPath())
all_primName_list.append(str(prim.GetPath()))
print("all_primName_list= "+str(all_primName_list))
newPrim_path=usd_increment_name("/Xform",all_primName_list)
print("newPrim_path= "+newPrim_path)
# Primを作成
prim = UsdGeom.Xform.Define(stage, newPrim_path)
# レイヤーにPrimを追加
#layer.GetPrimAtPath(newPrim_path)
print("Primが追加されました:", prim.GetPath())
add_prim_xform_for_stage_layer()