Python + Maya:在Hypershade网络中绘制选定节点的着色器

0 投票
1 回答
4650 浏览
提问于 2025-04-18 04:08

我写了一个脚本,可以获取选中对象正在使用的着色器。接下来,我想把这些着色器在超阴影编辑器中绘制成网络图。请问我缺少了什么呢?

我在这个小片段上已经成功运行了,但在主代码上却不行...

小测试片段:

import maya.cmds as cmds
blinn = cmds.createNode('blinn')
cmds.hyperShade(blinn)

主代码:

import maya.cmds as cmds

# get selected nodes:
nodes = cmds.ls(selection=True, dag=True)
nodeCount = len(nodes)

# get shading groups from shapes:
if nodeCount >= 1:
    shadingGroups = cmds.listConnections(nodes, t='shadingEngine')
shadingGroupsCount = len(shadingGroups)

# get the shaders:
if shadingGroupsCount >= 1:
    shaders = cmds.ls(cmds.listConnections(shadingGroups), materials=1) 

# graph shaders to the network in the hypershade:
if shaders >= 1:
    cmds.hyperShade(shaders)

print shaders

1 个回答

1

根据我从你的代码中看到的,你是想获取选中对象的材质。

这个代码会遍历这些对象,并把它们的着色器添加到图形中。在开始添加之前,它会先清空图形。如果你想去掉这个清空的功能,只需要把标志设置为 resetGraph=True, dependGraphArea=True 就可以了。

import maya.cmds as cmds
import maya.mel as mel

nodes = cmds.ls(selection=True, dag=True)
## Open the panel, doesn't re-open if already up and sets focus
cmds.HypershadeWindow()
## Get the name of the hsPanel
hsPanel = cmds.getPanel(withFocus=True)
## Clear the graph
cmds.hyperShade(resetGraph=True, dependGraphArea=True)
for node in nodes:
    if len(nodes) > 0:
        ## Select a node
        cmds.select(node, r=1)
        ## List the materials assigned to the object
        cmds.hyperShade(shaderNetworksSelectMaterialNodes=1)
        ## Create an array of the materials
        materialSelection = cmds.ls(sl=1)
        ## Loop over the materials and graph them        
        for material in materialSelection:
            # cmds.select(material, r=1)
            try:
                cmds.hyperGraph(hsPanel, edit=True, addDependNode=material)
            except:
                mel.eval("hyperShadePanelGraphCommand(\"%s\", \"addSelected\")" % hsPanel)

    else:
        cmds.warning("Please select an object")

我对使用 mel eval 语句 hyperShadePanelGraphCommand() 不太满意,不过我找不到用 python 的替代方法。如果有人能纠正我,那就太好了!

撰写回答