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

2024-09-28 21:15:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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

Tags: import脚本trueobjas世界ls数值
1条回答
网友
1楼 · 发布于 2024-09-28 21:15:08

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

下面是一个使用prints的非常基本的示例(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擅长通过脚本来做的事情:在大网格上,这会非常慢,因为你要处理大量的数字。不过,对于小例子来说,它应该是有效的。在

相关问题 更多 >