获取指定mesh的material


import maya.cmds as cmds

def get_mesh_shading_groups(mesh_name):
    # 获取mesh形状节点
    shape_node = cmds.listRelatives(mesh_name, shapes=True, fullPath=True)
    if not shape_node:
        return []
    
    # 获取连接到形状节点的shading engines
    shading_engines = cmds.listConnections(shape_node[0], type='shadingEngine')
    return shading_engines

def get_shading_group_material(shading_group):
    # 获取连接到shading group的材料
    materials = cmds.listConnections(shading_group + ".surfaceShader")
    
    if not materials:
        return None
    
    return materials[0]

def get_material(mesh):
    mesh_name = mesh.name()
    shading_groups = get_mesh_shading_groups(mesh_name)
    
    for shading_group in shading_groups:
        material = get_shading_group_material(shading_group)
        return material
 

get_material('mesh')

评论
  目录