通过Python在Maya中将着色器从一个网格分配到另一个网格

时间:2018-02-25 03:36:18

标签: python shader maya mel

我正在寻找一种方法,如果它们完全相似,如何将着色器从一个角色转移到另一个角色。所以我正在寻找如何从网格的一个面到另一个完全相似的网格查询着色器的方法。我已经停留在如何查询着色器的那一刻。

任何人都可以提供一些建议吗?

由于

2 个答案:

答案 0 :(得分:0)

选择source model,然后选择任意数量的target models(但具有相同的拓扑)并执行以下MEL代码:

global proc polyTransferShadingGroups(string $objects[]) {

    string $src = $objects[0];
    string $targ;
    int $targcount = size($objects);
    int $i;

    for($i = 1; $i < $targcount; ++$i) {
        string $targ = $objects[$i];
        string $shGroups[] = listSets -ets -type 1 -o $src;
        string $sg;

        for($sg in $shGroups) {
            string $sgMembers[] = sets -q $sg;
            string $f;

            for($f in $sgMembers) {
                string $obj = match(".*\.", $f);
                $obj = substitute("\.", $obj, "");

                if($obj == $src) {
                    string $index = match("\..*", $f);
                    sets -e -fe $sg ($targ + $index);
                }
            }
        }
    }
}
polyTransferShadingGroups(ls -sl);

答案 1 :(得分:0)

import maya.cmds as cmds
import re

def inRange(selection, fList):
    try:
        # extract ids 1234:5464 or just a number
        ranges = [ re.findall('(\d+:?\d+)', x)[0] for x in fList]
        # extract the id of our current face
        selectionIndex = re.findall('(\d+:?\d+)', selection)[-1]
    except:
        #if extraction above fail, it is not the good shader
        return False
    if selectionIndex in ranges:
        # if it is not a range 12354:46548
        # we check if our face is in the list
        return True
    else:
        # if it is a range, 13215:484984, let's check if our face id is between : 13215 >= ourFace <= 484984
        if [True for i in ranges if int(selectionIndex) >= int(i.split(':')[0]) and int(selectionIndex) <= int(i.split(':')[-1])]:
            return True
    # If everything above is not working, our face is not in the shader set
    return False

def shadeSimilar(target=list):   
    # split selected face to have the transofrm and the face id
    sel = cmds.ls(sl=True)[0].split('.')
    # lets query the shape to list every shaders connected
    shape = cmds.listRelatives(sel[0], c=True)[0]
    sg = list(set(cmds.listConnections(shape, type='shadingEngine')))

    for i in sg:
        # list the faces affected by the shader
        fList = cmds.sets(i, q=True)
        # check if our face is in the current shader loop
        if inRange(sel[-1], fList):
            # if True, let's apply this shader to the targets
            for t in target:
                # replace with the name of the new object 
                targetList = [x.replace(sel[0], t) for x in fList]
                # assign shader
                cmds.sets(targetList, e=True, forceElement=i)

# select the face first and the target second
shadeSimilar(target=cmds.ls(sl=True)[1:])
# shadeSimilar(target=['pSphere2', 'pSphere3'])

编辑:

请注意,您可以在ls命令中使用-fl标志,但在非常大的网格中,它可能很慢