如何使用 pymel(或 maya.cmds)将自定义 UVset 中的 UV 链接到 Maya 中的纹理



我正在编写一个工具,该工具将帮助我在模型上创建自定义uvSets,该模型将链接到将发送到vray_extraTex渲染元素的渐变(无插值(。 这些将成为遮罩,并以与 vRay 多遮罩相同的方式使用。 但是,我无法使用 pymel 将作为vray_extraTex纹理的渐变链接到自定义 uvSet。

我可以在 Maya 中手动完成所有这些操作,但由于某种原因,我缺少一些让 pymel 将 UV 链接到坡道的东西。我正在使用具有两个 uvSet 且第二组处于活动状态的 pSphere 在 Maya 场景中进行测试。这段代码被精简了一点:

def main():
inclusiveSet = None
renderElements =[]
ramps = []
newChannels = ['TestA','TestB','TestC','TestD'] 
for i, channel in enumerate(newChannels):
modIndex = i % 3 # 0:Red, 1:Green, 2:Blue
shapeNode=pm.PyNode('pSphereShape1')
transformNode=shapeNode.getTransform()
if modIndex == 0: # the first channel in the new Render Element
# make an etex render element
eTexElement = pm.PyNode(pm.mel.eval('vrayAddRenderElement("ExtraTexElement")'))
eTexElement.vray_name_extratex.set('')
eTexElement.vray_explicit_name_extratex.set('empty_empty_empy')            
renderElements.append(eTexElement)
# make a ramp
ramp = pm.shadingNode('ramp', asTexture=True, name='eTex_{}_ramp'.format(transformNode.name()))
ramps.append(ramp)
ramp.outColor.connect(eTexElement.vray_texture_extratex)
# make a place2dtexture
place2d = pm.shadingNode('place2dTexture', asUtility=True)
place2d.outUV.connect(ramp.uv)
place2d.translateFrameU.set(len(renderElements) - 1)
# link UVs to ramp
# NOT WORKING
indices = pm.polyUVSet(shapeNode.name(), query=True, allUVSetsIndices=True)
currentUVSet = pm.polyUVSet(shapeNode, query=True, currentUVSet=True )
for i in indices:
if currentUVSet == pm.getAttr("{}.uvSet[{}].uvSetName".format(shapeNode.name(), i)):
pm.uvLink(uvSet='{}.uvSet[{}].uvSetName'.format(shapeNode.name(), i) , texture=ramp)
explicit_name = eTexElement.vray_explicit_name_extratex.get()
nameTokens = explicit_name.split('_')
nameTokens[modIndex] = channel
explicit_name = '_'.join(nameTokens)
eTexElement.vray_explicit_name_extratex.set(explicit_name)
main()

我没有收到任何错误,但是当我检查UV链接时,斜坡仍然设置为map1 uvSet,而不是第二组处于活动状态。

我希望看到斜坡连接到uvChooser节点并链接到第二个uvSet。

我在写这篇文章时意识到,也许我需要将斜坡附加到分配给地理的着色器上,然后才能使用 python 将它们 uvlink 起来。接下来我会尝试测试一下

因此,经过更多的测试,在将斜坡分配给对象着色器上的新 attr 并对我之前发布的代码进行了一些小修复后,我能够让它工作。 已修复:

def connectTexture(layeredTex, shapeNode):
shadingGrps = pm.listConnections(shapeNode,type='shadingEngine')
shaders = pm.ls(pm.listConnections(shadingGrps),materials=1)
shader = shaders[0]
pm.addAttr(shader, longName='eTexLayeredTex', usedAsColor=True, attributeType='float3' )
pm.addAttr(shader, longName='eTexLayeredTexR', attributeType='float', parent='eTexLayeredTex' )
pm.addAttr(shader, longName='eTexLayeredTexG', attributeType='float', parent='eTexLayeredTex' )
pm.addAttr(shader, longName='eTexLayeredTexB', attributeType='float', parent='eTexLayeredTex' )
layeredTex.outColor.connect(shader.eTexLayeredTex)

def main():
inclusiveSet=None
renderElements=[]
ramps=[]
newChannels = ['TestA','TestB','TestC','TestD']
shapeNode=pm.PyNode('pSphereShape1')
transformNode=shapeNode.getTransform()
# make a layeredTexture
layeredTexture = pm.shadingNode('layeredTexture', asTexture=True, name='eTex_{}_layeredTexture'.format(transformNode.name()))
layeredTexture.attr('inputs')[0].color.set(0,0,0)
layeredTexture.attr('inputs')[0].alpha.set(1)
layeredTexture.attr('inputs')[0].blendMode.set(1)
connectTexture(layeredTexture, shapeNode)
for i, channel in enumerate(newChannels):
modIndex = i % 3 # 0:Red, 1:Green, 2:Blue
if modIndex == 0: # the first channel in the new Render Element
# make an etex render element
eTexElement = pm.PyNode(pm.mel.eval('vrayAddRenderElement("ExtraTexElement")'))
eTexElement.vray_name_extratex.set('')
eTexElement.vray_explicit_name_extratex.set('empty_empty_empy')            
renderElements.append(eTexElement)
# make a ramp
ramp = pm.shadingNode('ramp', asTexture=True, name='eTex_{}_ramp'.format(transformNode.name()))
ramps.append(ramp)
ramp.interpolation.set(0)
ramp.colorEntryList[0].position.set(0.0)
ramp.colorEntryList[1].position.set((1.0 / 3.0))
ramp.colorEntryList[2].position.set((2.0 / 3.0))
ramp.colorEntryList[0].color.set(1,0,0)
ramp.colorEntryList[1].color.set(0,1,0)
ramp.colorEntryList[2].color.set(0,0,1)
ramp.defaultColor.set(0,0,0)            
ramp.outColor.connect(eTexElement.vray_texture_extratex)
ramp.outColor.connect( layeredTexture.attr( 'inputs[{}].color'.format( 1 + i // 3)))
# make a place2dtexture
place2d = pm.shadingNode('place2dTexture', asUtility=True)
place2d.outUV.connect(ramp.uv)
place2d.outUvFilterSize.connect(ramp.uvFilterSize)
place2d.wrapU.set(0)
place2d.wrapV.set(0)
place2d.translateFrameU.set(len(renderElements) - 1)
# link UVs to ramp
indices = pm.polyUVSet(shapeNode.name(), query=True, allUVSetsIndices=True)
currentUVSet = pm.polyUVSet(shapeNode, query=True, currentUVSet=True )[0]
for index in indices:
if currentUVSet == pm.getAttr('{}.uvSet[{}].uvSetName'.format(shapeNode.name(), index)):
pm.uvLink(uvSet='{}.uvSet[{}].uvSetName'.format(shapeNode.name(), index) , texture=ramp)
explicit_name = eTexElement.vray_explicit_name_extratex.get()
nameTokens = explicit_name.split('_')
nameTokens[modIndex] = channel
explicit_name = '_'.join(nameTokens)
eTexElement.vray_explicit_name_extratex.set(explicit_name)
main()

他们的关键是将坡道连接到分配给物体的材质。奇怪的是,在创建uvLinks之后,您可以删除斜坡和材质之间的连接,并且uvLinks仍然可以正常工作。我发布了这个以防有人遇到类似的问题

最新更新