使用Python计算maya中顶点的法线

Calculate the Normal of a vertex in maya with Python

当我制作一个指向我的对象的光时,我有一个脚本。我用 normalConstrain 做了这个。但是有没有其他方法可以不受限制地做到这一点?

我想我需要计算离我的光源最近的顶点的法线? 但我不知道该怎么做,如果有人能帮助我就好了!

See Screenshot

下面是我做 NormalConstrain 的方法:

    myNormalConstrain = cmds.normalConstraint('mySphere','myLight', aimVector = (0,0,1), worldUpType = 'scene',name='NormalConstrainToObject')

最简单的方法是创建一个 closestPointOnMesh 节点并将其连接到网格的 worldMesh 属性。唯一的问题是 Maya 2016 中存在一个错误(不确定其他错误),如果您使用的单位不是厘米,则从节点返回的法线值实际上并未标准化。这是一个函数:

import maya.cmds as cmds
import maya.api.OpenMaya as api


def get_closest_normal(surface, x, y , z):
    node = cmds.createNode('closestPointOnMesh')
    cmds.connectAttr(surface + '.worldMesh ', node + ".inMesh")
    cmds.setAttr( node + ".inPosition", x, y, z, type='double3')
    normal = cmds.getAttr(node + ".normal")
    # there's a bug in Maya 2016 where the normal
    # is not properly normalized.  Not sure
    # if it's fixed in other years....  this
    # is the workaround

    result = api.MVector(*normal)
    cmds.delete(node)
    result.normalize()
    return result


print get_closest_normal('pSphereShape1', 10, 1, 1)

您可以保留该节点并将其正常属性连接到某些内容以进行实时更新,而不是像此处那样获取编号并删除节点。然而,这是一个相当昂贵的更新节点,所以在没有测试性能以确保它是负担得起的情况下,不要将该版本用于动画装备之类的东西