python + maya:将所选节点的着色器图形化为超大阴影网络

时间:2014-04-24 21:07:57

标签: python maya

我编写了一个脚本,用于获取所选对象使用的着色器。然后,我想在超级阴影中将这些着色器绘制到网络中。我在这里缺少什么?

我已经设法让它在这个小片段上工作但不在主代码上......

小测试片段:

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 个答案:

答案 0 :(得分: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的替代品。有人纠正这一点感到高兴!

相关问题