前言:在Maya中操作USD只需要获取到USD的实例然后用USD的方法去操作即可
相关链接
User Documentation and Tutorials API Documentation MayaUSD GitHub Autodesk Maya2022 - USD for Maya
界面展示
功能介绍
Variant Sets:选择 usd 节点,点击此按钮,会生成在大纲中右键 usd 节点出现的 Variant Sets 相同的菜单选项,可以多选 usd 节点,默认是获取第一个 usd 节点的菜单信息
主要用到的命令
import maya.internal.ufeSupport.utils as ufeUtils
ufeUtils.getNonMayaSelectedItems()
import mayaUsd
mayaUsd.lib.GetPrim("|HelloWorld|HelloWorldShape").GetStage()
完整代码
u"""
批量设置变体工具
"""
import maya.cmds as mc
from functools import partial
import maya.internal.ufeSupport.utils as ufeUtils
import mayaUsd
WINDOW_NAME = "usd_variant_tool"
MENU_NAME = WINDOW_NAME + "_menu"
def usdPathToDict(path_list):
result = {}
for i in path_list:
shape, prim = i.split(",")
if shape not in result:
result[shape] = []
result[shape].append(prim)
return result
def batchSetUsdVariant(prims, variant_set, variant, *args):
u"""
批量设置变体
"""
prim_dict = usdPathToDict(prims)
for shape, prim_list in prim_dict.items():
stage = mayaUsd.lib.GetPrim(shape).GetStage()
for i in prim_list:
prim = stage.GetPrimAtPath(i)
vs = prim.GetVariantSets()
if vs.HasVariantSet(variant_set):
vt = vs.GetVariantSet(variant_set)
if vt.HasAuthoredVariant(variant):
vt.SetVariantSelection(variant)
pass
def usdSetActive(prims, act):
if isinstance(prims, list):
prims = usdPathToDict(prims)
for k, v in prims.items():
stage = mayaUsd.lib.GetPrim(k).GetStage()
for path in v:
try:
prim = stage.GetPrimAtPath(path)
prim.SetActive(act)
except:
pass
pass
def setActivate(*_):
usdSetActive(ufeUtils.getNonMayaSelectedItems(), True)
pass
def setDeactivate(*_):
usdSetActive(ufeUtils.getNonMayaSelectedItems(), False)
def create_variant_menu(*args):
mc.menu(MENU_NAME, e=True, deleteAllItems=True)
sels = ufeUtils.getNonMayaSelectedItems()
if len(sels) == 0:
mc.menuItem(label='No Selected', p=MENU_NAME)
return
shape, path = sels[0].split(",")
stage = mayaUsd.lib.GetPrim(shape).GetStage()
prim = stage.GetPrimAtPath(path)
variant_sets = prim.GetVariantSets()
variant_names = variant_sets.GetNames()
if len(variant_names) == 0:
mc.menuItem(label='No Variant', p=MENU_NAME)
return
for i in variant_sets.GetNames():
pass
mc.menuItem(subMenu=True, label=i, p=MENU_NAME)
mc.radioMenuItemCollection()
variant_set = variant_sets.GetVariantSet(i)
selected = variant_set.GetVariantSelection()
for ii in variant_set.GetVariantNames():
mc.menuItem(label=ii, radioButton=True if ii == selected else False,
command=partial(batchSetUsdVariant, sels, i, ii))
def main():
if mc.window(WINDOW_NAME, exists=True):
mc.deleteUI(WINDOW_NAME, window=True)
mc.window(WINDOW_NAME, title="Usd Variant Tool", width=400, height=200, menuBar=True, mnb=True, mxb=False,
sizeable=False)
mc.menu(MENU_NAME, label='Variant Sets', postMenuCommand=create_variant_menu,
postMenuCommandOnce=False)
mc.rowColumnLayout(p=WINDOW_NAME, rs=(1, 1))
mc.text("")
mc.button(label="Activate Prim", command=setActivate)
mc.button(label="Deactivate Prim", command=setDeactivate)
mc.showWindow(WINDOW_NAME)
if __name__ == '__main__':
main()
pass
|