Maya Python脚本以显示数值顶点转换
我想用Python为Maya编写一个脚本,它允许您在平视显示中查看顶点的数字转换 因此,如果拾取一个顶点并沿轴移动,则平视显示中应显示自顶点的起始世界位置以来的移动值 例如,世界位置是顶点的“20,20,50”,我将其移动到平视显示器中的“20,20,30”应该是“020” 我离这里很远,但这是我到现在为止所做的Maya Python脚本以显示数值顶点转换,python,api,position,maya,vertex,Python,Api,Position,Maya,Vertex,我想用Python为Maya编写一个脚本,它允许您在平视显示中查看顶点的数字转换 因此,如果拾取一个顶点并沿轴移动,则平视显示中应显示自顶点的起始世界位置以来的移动值 例如,世界位置是顶点的“20,20,50”,我将其移动到平视显示器中的“20,20,30”应该是“020” 我离这里很远,但这是我到现在为止所做的 import maya.cmds as cmds selection = cmds.ls(sl=True) for obj in selection: vertexWer
import maya.cmds as cmds
selection = cmds.ls(sl=True)
for obj in selection:
vertexWert = cmds.pointPosition( obj , w=True)
print vertexWert
您可以通过对象的
.outMesh
属性上的attributeChanged
scriptJob获得有关更改的通知,以便在编辑网格时触发脚本。但是,这不知道网格更改的原因:例如,如果旋转顶点选择而不是移动它,它将触发。您必须存储垂直位置的副本,并将新位置与旧位置进行比较,以获得实际差异
下面是一个使用prints的非常基本的示例(该命令非常冗长,所以我将省略它)。我还使用了一个全局变量,这通常是一个坏主意,但听起来向问题中添加类会使演示变得更加困难:正确的做法是创建一个可调用的类来管理网格差异
# 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通过脚本擅长做的事情:在大网格上,这将非常缓慢,因为您要处理很多数字。对于小示例,它应该可以工作。您可以通过对象的
.outMesh
属性上的attributeChanged
scriptJob获得有关更改的通知,以便在编辑网格时触发脚本。但是,这不知道网格更改的原因:例如,如果旋转顶点选择而不是移动它,它将触发。您必须存储垂直位置的副本,并将新位置与旧位置进行比较,以获得实际差异
下面是一个使用prints的非常基本的示例(该命令非常冗长,所以我将省略它)。我还使用了一个全局变量,这通常是一个坏主意,但听起来向问题中添加类会使演示变得更加困难:正确的做法是创建一个可调用的类来管理网格差异
# 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通过脚本擅长做的事情:在大网格上,这将非常缓慢,因为您要处理很多数字。对于小的例子,它应该可以工作。我们能为您提供什么帮助?你的问题/错误是什么?你被“在HUD上打印东西”困住了吗?我被数学部分困住了。我需要在脚本中加入逻辑。我需要一个想法,让variabls中有正确的值。你在处理缓存对象吗?我不明白你剧本的目的我们能帮你什么?你的问题/错误是什么?你被“在HUD上打印东西”困住了吗?我被数学部分困住了。我需要在脚本中加入逻辑。我需要一个想法,让variabls中有正确的值。你在处理缓存对象吗?我不明白你剧本的目的