[maya]maya_usdでstageのレイヤーにprimxformを作る、そのprimxformにAdd USD Referenceを追加する。

Create a primxform in the stage layer with maya_usd, and add Add USD Reference to that primxform.

print("add_primxform_for_stage_layer")

import maya.cmds as cmds
from maya import cmds
import mayaUsd.ufe
from pxr import Usd, UsdGeom
import re
import os
#cmds.workspace( directory=workspacepath)でいいかとおもっていたけどちがかった件。
def evalMelSetProject(FullDirPath):
	print("-----------------------------------maya.mel.eval evalMelSetProject(evalstr)----------------------------------------")# 20161121
	evalstr='setProject("'+FullDirPath+'");' #OK
	print("evalstr="+evalstr);
	maya.mel.eval(evalstr) #OK
	print("----------------------------------maya.mel.eval evalMelSetProject(evalstr) End----------------------------------")
	

def get_stage(stagePath):
    import mayaUsd.ufe
    #stagePath="|stage1|stageShape1"
    #stage = mayaUsd.ufe.getStage("|stage1|stageShape1")
    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]
    print("selectedZero= "+selectedZero)
    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"):
        stageNumStr=selectedZero[6:]
        layerPath="|stage"+stageNumStr+"|stageShape"+stageNumStr
        print("layerPath= "+layerPath)
        cmds.select(layerPath)
        usd_stage=get_stage(layerPath)
    else:
        print("not stage!!!!")
    return usd_stage,layerPath


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  "+str(new_name))
    return new_name  

print("----start-----")
def add_prim_xform_for_stage_layer(add_ref_USD_file_path):
    #---------------------SetProject-------------------------
    filePath = cmds.file(q=1,sceneName=1)
    print("filePath= "+filePath)
    dirPath = os.path.dirname(filePath)
    dirPath=re.sub(r'\\', '/', dirPath)
    #dirPath=os.path.abspath(dirPath)
    print("dirPath= "+dirPath)
    FullDirPath=dirPath
    evalMelSetProject(FullDirPath)
    #-------------------------------------------------------
    
    stage,layerPath = get_selected_maya_usd_stage()
    print("stage= "+str(stage))
    print("layerPath= "+str(layerPath))
    # レイヤーを取得
    #layer = stage.GetEditTarget().GetLayer()
    layer = stage.GetRootLayer()
    print("layer= "+str(layer))
    
    # Primを作成
    prim_count=1
    prim_path = "/Xform"+str(prim_count)
    newPrimStagePath=layerPath+","+prim_path
    print("newPrimStagePath= "+newPrimStagePath)
    
    #layerPrimList = cmds.listRelatives(layerPath,children=1)
    #layerPrimList = cmds.listConnections(layerPath,destination=1)
    #print("layerPrimList= "+str(layerPrimList))

    # レイヤー内のすべての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())
   
    # 追加するUSDファイルのパス
    referencePath = dirPath+"/Usd/S_Stg_Cys_StartArea_Grd.usda"
    referencePath = add_ref_USD_file_path
    print("referencePath= "+referencePath)
    # USDリファレンスを追加
    prim.GetPrim().GetReferences().AddReference(referencePath)
    
    # ステージを保存
    #
    #setAttr -type "string" |stage6|stageShape6.filePath "D:/SandBox/USD_Maya2023/16_newyear/stageShape6-1GGoNk.usd";
    #cmds.setAttr -type "string" |stage6|stageShape6.filePath "D:/SandBox/USD_Maya2023/16_newyear/stageShape6-1GGoNk.usd";
    attName=layerPath+".filePath"
    layout_attpath=dirPath+"/Usd/Stg_Cys_003Floor01_example.usd"
    layout_attpath=dirPath+"/Layout_00100.usd"
    print("layout_attpath= "+layout_attpath)
    cmds.setAttr( attName, layout_attpath,type="string")
    #stage.GetRootLayer().subLayerPaths.append(dirPath+"Usd/Stg_Cys_003Floor01_example.usd")
    #stage.GetRootLayer().Save()
    """"""
    
#add_prim_xform_for_stage_layer()

g_lastSetPath=""

def select_exportDir(self):
    
    filePath = cmds.fileDialog2( cap = 'リファレンスするディレクトリの選択', okc = 'リファレンスするディレクトリを設定',dialogStyle = 2, fileMode = 3,startingDirectory=get_lastSetPath())
    if filePath == None:
        return False
    else:
        set_refDirPath(filePath[0]+"/")
        
def set_refDirPath(refDir):
    print("refDir= "+refDir)
    refDir_Arr=refDir.split("/")
    refDir_Arr_last=len(refDir_Arr)
    print("refDir_Arr_last= "+str(refDir_Arr_last))
    DirName=refDir_Arr[refDir_Arr_last-2]
    print("DirName= "+DirName)
    #D:/SandBox/USD_Maya2023/17_subfolder/Stick/Stick.mb
    ref_USD_filePath=refDir+DirName+".usda"
    add_prim_xform_for_stage_layer(ref_USD_filePath)
    
def exportFile():
    filePath = cmds.fileDialog2(fileFilter = '*.json', cap = '書き出し', okc = '書き出し',ds = 2, fm = 0)
    if filePath == None:
        return False
               
def importFile():
    filePath = cmds.fileDialog2(fileFilter = '*.json', cap = '読み込み', okc = '読み込み',ds = 2, fm = 1)
    if filePath == None:
        return False

def get_lastSetPath():
    global g_lastSetPath
    lastSetPath=""
    if(g_lastSetPath==""):
        print("g_lastSetPath=は値なし")
        scenePath=get_scenePath_ref()
        scenePath=os.path.abspath(scenePath)
        lastSetPath=scenePath
        g_lastSetPath=lastSetPath
    else:
        print("g_lastSetPath=は値 g_lastSetPath="+g_lastSetPath)
        lastSetPath=g_lastSetPath
    print("g_lastSetPath= "+g_lastSetPath)
    print("lastSetPath= "+lastSetPath)
    return lastSetPath

def get_scenePath_ref():
    scenefilePath = cmds.file(q=1, sceneName=1)
    mayaPath,mayaFile = os.path.split(scenefilePath)
    #mayaPath = mayaPath + "/Usd/"
    mayaPath=os.path.abspath(mayaPath)
    return mayaPath


text_Field_id=""       
def createWindow():
    scenefilePath = cmds.file(q=1, sceneName=1)

    USD_window = cmds.window("FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window", widthHeight=(400, 200))
    USD_layout = cmds.columnLayout("USD_layout",adjustableColumn=True, parent=USD_window)

    cmds.text (label="primをぶら下げたいステージのレイヤーを選択して、", align='left', parent=USD_layout)
    cmds.text (label="[Add USD Referernce/Payload...]を押してください", align='left', parent=USD_layout)
    cmds.separator(parent=USD_layout)
    
    cmds.button(label="Add USD Referernce/Payload... ", command=select_exportDir, parent=USD_layout)
    
    #cmds.separator(parent=USD_layout)

    #cmds.button(label="Add USD Referernce/Payload... ", command=add_prim_xform_for_stage_layer, parent=USD_layout)

    cmds.showWindow(USD_window)
    return None


def folderOpen(self):
    Exportpath = get_lastExportDirPath()
    Exportpath = Exportpath.replace('/', '\\')
    subprocess.Popen(["explorer", "/root,", Exportpath], shell=True)
    print(USD.mayaPath)

def get_scenePath():
    scenefilePath = cmds.file(q=1, sceneName=1)
    mayaPath,mayaFile = os.path.split(scenefilePath)
    mayaPath = mayaPath + "/Usd/"
    return mayaPath
    
def btn_scene(self):
    scenePath = get_scenePath()
    print("scenePath= "+scenePath)
    set_lastExportDirPath(scenePath)
    
def changeTextFld(*arg):
    #mayaPath = get_scenePath()
    #cmds.textField("pathTxtFld", edit=True, text=get_lastExportDirPath())
    #pathTxtFld_value=cmds.textField("pathTxtFld", r=True, v=True)
    #pathTxtFld_value = cmds.textField("pathTxtFld", q=True, text=True)
    text_Field_id="FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window|USD_layout|pathTxtFld"
    print("text_Field_id= "+text_Field_id)
    pathTxtFld_value = cmds.textField(text_Field_id, q=True, text=True)
    #pathTxtFld_value = pathTxtFld
    print("pathTxtFld_value= "+pathTxtFld_value)
    set_lastExportDirPath(pathTxtFld_value)
    
def clearWindow():
    if cmds.window("FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window",exists=True):
        cmds.deleteUI("FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window")
    return None
    
def FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window_Func():
    if cmds.window("FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window",exists=True):
        cmds.deleteUI("FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window")
    createWindow()



FURC_USD_Layout_Add_PrimXformReference_For_StageLayer_Window_Func()

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です