在给定点获取纹理颜色的问题

时间:2019-06-03 20:13:38

标签: python maya

我正在尝试在与多边形顶点有关的u坐标处获取纹理的颜色。我遇到的问题是,即使没有以这种方式绘制,我得到的每个坐标也会产生相同的颜色。

我一直在尝试放置纹理并稍后对其进行绘画,然后在一个简单的平面中获得每个顶点的颜色。我所看到的大概是已经有了uv坐标的polyListComponentConversioncolorAtPoint所拥有的东西。 我得到的点是[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0],并且颜色始终是相同的,并且在两个角之一中绘制。 如果改为使用这些,而我尝试对另一个值(例如[.5, .5])进行硬编码,那么我会得到另一种看起来准确的颜色。

我要做的是:

#Create and place the texture files into the poly
def createShadingEngine(vName):

    vNewShadingGroup = mc.sets(name = '{0}_SG'.format(vName), empty = True, renderable = True, noSurfaceShader = True)
    vNewMaterial = mc.shadingNode('lambert', name = '{0}_mat'.format(vName), asShader = True)
    mc.connectAttr('{0}.outColor'.format(vNewMaterial), '{0}.surfaceShader'.format(vNewShadingGroup))
    vNewTexture = mc.shadingNode('file', name = '{0}_file'.format(vName), asTexture = True)
    vNewUtility = mc.shadingNode('place2dTexture', name = '{0}_tex'.format(vName), asUtility = True)
    mc.connectAttr('{0}.outColor'.format(vNewTexture), '{0}.color'.format(vNewMaterial))

    vConnectAttr = ['coverage', 'translateFrame', 'rotateFrame', 'mirrorU', 'mirrorV', 'stagger', 'wrapU', 'wrapV',
                    'repeatUV', 'offset', 'rotateUV', 'noiseUV', 'vertexUvOne', 'vertexUvTwo', 'vertexUvThree', 'vertexCameraOne']
    for vAttr in vConnectAttr:
        mc.connectAttr('{0}.{1}'.format(vNewUtility, vAttr), '{0}.{1}'.format(vNewTexture, vAttr))
        mc.connectAttr('{0}.outUV'.format(vNewUtility), '{0}.uv'.format(vNewTexture))
        mc.connectAttr('{0}.outUvFilterSize'.format(vNewUtility), '{0}.uvFilterSize'.format(vNewTexture))

    return vNewShadingGroup

#Get the map points for the poly
def getPolyMap(vPoly):

    vPoints = []

    if mc.objExists(vPoly):
        vShape = mc.listRelatives('pPlane1', children = True, fullPath = True, shapes = True)[0]
        vEngine = mc.listConnections(vShape, type = 'shadingEngine')
        vMaterials = mc.ls(mc.listConnections(vEngine), materials = True)
        vTextureFiles = mc.listConnections(vMaterials, type = 'file')

        if vTextureFiles :
            vMapPoints = mc.polyListComponentConversion('{0}.vtx[*]'.format(vPoly), fv = True, tuv = True)
            vMapPoints = mc.ls(vMapPoints, flatten = True)
            for vPoint in vMapPoints:
                vCoord = mc.polyEditUV(vPoint, q = True)
                print vPoint, vCoord
                vPoints.append(vCoord)

    return vPoints

#Get the color at those points
def getColorAtPointUV(vPoly, vUVPoint = [0, 0]):

    vColor = ''

    vShape = mc.listRelatives('pPlane1', children = True, fullPath = True, shapes = True)[0]
    vEngine = mc.listConnections(vShape, type = 'shadingEngine')
    vMaterials = mc.ls(mc.listConnections(vEngine), materials = True)
    vTextureFiles = mc.listConnections(vMaterials, type = 'file')
    if vTextureFiles and len(vUVPoint) == 2:
        vColor = mc.colorAtPoint('{0}.outColor'.format(vTextureFiles ), o = 'RGB', u = vUVPoint[0], v = vUVPoint[1])

    return vColor

#With those functions I execute:
vShape = mc.listRelatives('pPlane1', children = True, fullPath = True, shapes = True)[0]
vEngine = mc.listConnections(vShape, type = 'shadingEngine')
vMaterials = mc.ls(mc.listConnections(vEngine), materials = True)
vTextureFiles = mc.listConnections(vMaterials, type = 'file')
cv = getPolyMap('pPlane1')
for cc in cv:
    print cc, mc.colorAtPoint(vTextureFiles, o = 'RGB', u = cc[0], v = cc[1])

我从中得到的结果是:

[0.0, 0.0] [0.00784313678741455, 0.9921568036079407, 0.00784313678741455]
[1.0, 0.0] [0.00784313678741455, 0.9921568036079407, 0.00784313678741455]
[0.0, 1.0] [0.00784313678741455, 0.9921568036079407, 0.00784313678741455]
[1.0, 1.0] [0.00784313678741455, 0.9921568036079407, 0.00784313678741455]

uv的每个角都打印为绿色,即使它的一个角只有该颜色。 另一方面,如果我强制使用u = .5v = .5,则结果是正确的(默认灰色[0.5019607543945312, 0.5019607543945312, 0.5019607543945312]

无法发布图片,但是一个灰色正方形,右上角是绿色点)。

0 个答案:

没有答案