簡體   English   中英

通過Python在Maya中將着色器從一個網格分配到另一個網格

[英]Assign shaders from one mesh to another in Maya via Python

我正在尋找一種方法,如果它們完全相似,則如何將着色器從一個角色轉移到另一個角色。 因此,我正在尋找一種方法,以從網格的一個面到另一個完全相似的面查詢着色器。 我已經停留在如何查詢着色器的那一刻。

有人可以給一些提示嗎?

謝謝

選擇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);
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標志,但是在很大的網格中,它可能很慢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM