Maya script 根据 face id 获得 material list

Maya script 根据 face id 获得 material list

UE4导入FBX,对于单个物体来说,材质槽的顺序是根据面id来排材质的(多个物体合并可能是另外一种方式)

# -*- coding: utf-8 -*-
import maya.mel as mel
import maya.cmds as cmds

import re

def getMeshShadingEngines(obj):
    meshList = []
    shadeEngs = []
    if cmds.objectType(obj) == 'mesh':
        meshList.append(obj)
    else:
        meshList = cmds.listRelatives(obj, shapes= 1)
    if meshList:
        shadeEngs = cmds.listConnections(meshList, type = "shadingEngine")    
    return shadeEngs

def getShadingEngineSurfaceShader(shadeEngs):
    connections = cmds.listConnections(shadeEngs, c = 1)        
    materials = [connections[i + 1] for i in range(0, len(connections), 2) if '.surfaceShader' in connections[i]]  
    return materials

def getMeshMaterials(obj):
    #materials = []
    shadeEngs = getMeshShadingEngines(obj)  
    materials = getShadingEngineSurfaceShader(shadeEngs)
    materials = cmds.ls(materials, materials = True)
    
    if materials:
        materials = list(set(materials))
    else:
        materials = []
    return materials

def getMaterialColorTexturePath(mat):
    textureFile = ''
    try:
        fileNode = cmds.listConnections('%s.color' % (mat), type='file')
        textureFile = cmds.getAttr("%s.fileTextureName" % fileNode[0])
    except:
        pass
    return textureFile

def getFacesByMaterial(obj, mat):
    sels_pre = cmds.ls(sl = 1)
    cmds.hyperShade(objects = mat)
    objs = cmds.ls(sl = True)
    objs = [o for o in objs if obj in o]
    cmds.select(sels_pre, r = True)
    return objs

def getObjectMaterialsByFaceId(obj):
    shadeEngs = getMeshShadingEngines(obj)
    shadeEngs = list(set(shadeEngs))
    len(shadeEngs)
    mat_list = []
    if len(shadeEngs) > 1:
        mat_list = [[min([int(re.search('%s.f\[(.*)\]' % obj, m).group(1).split(':')[0]) for m in cmds.sets(sg, q = 1) if re.search('%s.f\[(.*)\]' % obj, m)]), sg] for sg in shadeEngs]
        mat_list.sort( key = lambda x: x[0])
        mat_list = [item[1] for item in mat_list]
        mat_list = [getShadingEngineSurfaceShader(sg)[0] for sg in mat_list]
    elif len(shadeEngs) == 1:
        mat_list.append(getShadingEngineSurfaceShader(shadeEngs[0])[0])
    return mat_list

 

posted @ 2021-06-25 12:04  ibingshan  阅读(262)  评论(0编辑  收藏  举报