[英]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.