Maya Python脚本将纹理应用于所有对象而非选定对象

1 投票
1 回答
35 浏览
提问于 2025-04-12 12:29

我正在使用Adobe的Substance Painter提供的一个脚本,这个脚本本来是用来导入一个sbsar文件,把它变成一个着色器节点,然后再把它应用到我选中的物体上。但是当我运行这个脚本时,它却把效果应用到了场景中的所有物体上。

import maya.cmds as cmds 
 
def _connect_place2d(substance_node): 
    """ Connects the place2d texture node to the Substance node """ 
    place_node = cmds.shadingNode('place2dTexture', asUtility=True) 
 
    connect_attrs = [('outUV', 'uvCoord'), ('outUvFilterSize', 'uvFilterSize')] 
 
    for out_attr, in_attr in connect_attrs: 
        cmds.connectAttr('{}.{}'.format(place_node, out_attr), 
                         '{}.{}'.format(substance_node, in_attr)) 
 
def _find_shading_group(node): 
    """ Walks the shader graph to find the shading group """ 
    result = None 
 
    connections = cmds.listConnections(node, source=False) 
 
    if connections: 
        for connection in connections: 
            if cmds.nodeType(connection) == 'shadingEngine': 
                result = connection 
            else: 
                result = _find_shading_group(connection) 
                if result is not None: 
                    break 
 
    return result 
 
def _apply_substance_workflow_to_selected(substance_file, workflow): 
    """ Imports a mesh into Maya and applies the shader from a 
        Substance workflow to it """ 
    geometry = cmds.ls(geometry=True) 
 
    # Create the substance node and connect the place2d texture node 
    substance_node = cmds.shadingNode('substanceNode', asTexture=True) 
    _connect_place2d(substance_node) 
 
    # Load the Substance file 
    cmds.substanceNodeLoadSubstance(substance_node, substance_file) 
 
    # Apply the workflow 
    cmds.substanceNodeApplyWorkflow(substance_node, workflow=workflow) 
 
    # Acquire the shading group and apply it to the mesh 
    shading_group = _find_shading_group(substance_node) 
 
 
def demo_load_sbsar_workflow(): 
    """ Acquires an sbsar from a file dialog, loading and applying it to 
        any selected mesh """ 
    file_filter = 'Substance (*.sbsar);;' 
 
    files = cmds.fileDialog2(cap='Select a Substance file', fm=1, dialogStyle=2, 
                             okc='Open', fileFilter=file_filter) 
 
    if files: 
        substance_file = files[0] 
        _apply_substance_workflow_to_selected(substance_file, 
                                              cmds.substanceGetWorkflow()) 
 
if __name__ == '__main__': 
    demo_load_sbsar_workflow()

我原本以为它会立刻把我选中的纹理应用到我选中的模型上,而不是所有的物体。

1 个回答

0

这一行代码 geometry = cmds.ls(geometry=True) 的意思是选择所有的几何体。如果你把它换成:

geometry = cmds.ls(sl=True, geometry=True)

那么它就只会选择你当前选中的形状,但前提是你得先选中那些形状节点。

撰写回答