Maya Python脚本显示数值顶点转换

时间:2016-01-14 14:52:31

标签: python api position maya vertex

我想在Python中为Maya编写一个脚本,它允许您在平视显示器中查看顶点的数值转换。

因此,如果选择一个顶点并沿轴移动它,则在平视显示中应显示自顶点的起始世界位置以来的移动值。

例如,世界排名是“20,20,50'顶点,我把它移到'20,20,30'在平视显示器中应该显示' 0 0 20'。

我很远,但这是我迄今为止所做的。

import maya.cmds as cmds

selection = cmds.ls(sl=True) 

for obj in selection:
    vertexWert = cmds.pointPosition( obj , w=True)

print vertexWert

1 个答案:

答案 0 :(得分:2)

您可以在对象的attributeChanged属性上使用.outMesh scriptJob获取有关更改的通知,以便在编辑网格时触发脚本。但是,这不会知道为什么网格已更改:例如,如果您旋转顶点选择而不是移动它,它将会触发。你必须存储一个vert位置的副本,并将新的位置与旧的位置进行比较,以获得实际的差异。

这是一个使用打印的基本示例(headsUpDisplay命令非常冗长,所以我会把它留下来)。我也使用了一个全局变量,这通常是一个坏主意,但听起来像在问题中添加类会使得更难以演示:'正确'的做法是创建一个可调用的类来管理网格差异对你而言。

# to save the mesh positions. This does mean you can only use this code on one object at a time....
global _old_positions
_old_positions = None

# this is the callback function that gets called when the mesh is edited
def update_mesh_positions():
    selected = cmds.ls(sl=True, o=True)
    if selected:
        selected_verts = selected[0] + ".vtx[*]"
        global _old_positions
        # make sure we have something to work with....
        if not _old_positions:
            _old_positions = cmds.xform(selected_verts, q=True, t=True, ws=True)

        # gets all of the vert positions
        new_positions = cmds.xform(selected_verts, q=True, t=True, ws=True)

        # unpack the flat list of [x,y,z,x,y,z...] into 3 lists of [x,x], [y,y], etc...
        x1 = _old_positions[::3]
        y1 = _old_positions[1::3]
        z1 = _old_positions[2::3]

        x2 = new_positions[::3]
        y2 = new_positions[1::3]
        z2 = new_positions[2::3]


        old_verts = zip(x1, y1, z1)
        new_verts = zip(x2, y2, z2)

        # compare the old positions and new positions side by side
        # using enumerate() to keep track of the indices
        for idx, verts in enumerate(zip (old_verts, new_verts)):
            old, new = verts
            if old != new:
                # you'd replace this with the HUD printing code
                print idx, ":", new[0] - old[0],  new[1] - old[1], new[2] - old[2]

        # store the new positions for next time
        _old_positions = new_positions


#activate the script job and prime it
cmds.scriptJob(ac= ('pCubeShape1.outMesh', update_mesh_positions))
cmds.select('pCubeShape1')
update_mesh_positions()
# force an update so the first move is caught

这并不是Maya擅长通过脚本做的事情:在大网格上,这将是非常缓慢的,因为你正在处理大量的数字。对于小例子,它应该可以工作。