在Maya中如何获取鼠标的3D位置?

0 投票
2 回答
3753 浏览
提问于 2025-04-15 16:52

我在Maya里创建了一个拖动的上下文,使用了以下代码,pos保存了我鼠标光标的2D坐标。我想把这个2D坐标转换成3D坐标,以便作为我想发射的光线的起点。我希望通过Python脚本或者Python API来实现这个。

import maya.cmds as mc

mc.draggerContext( 'testContext', pressCommand='getCursorPos()',
                    dragCommand='getCursorPos()', cursor='default')

def getCursorPos():

    #--get the 2D position of cursor (on the view port)----

    pos = mc.draggerContext( 'testContext', query=1, dragPoint=1) 

    #----convert to 3D coordinates in the scene--------
    ????????

提前谢谢你们!

2 个回答

0

你有没有查看过拖动上下文的投影选项(-pr)?

鼠标坐标是二维的数据,而投影选项是用来解决第三维度如何映射的方法。对于光线来说,也许以下选项中的某一个和你的问题有关:

xAxis 投影到X轴上最近的点
yAxis 投影到Y轴上最近的点
zAxis 投影到Z轴上最近的点

希望这些信息对你有帮助,祝你好运。

1

其实,你可以看看 draggerContext 命令帮助里的 space(sp) 选项。

mc.draggerContext('sampleContext', dragCommand='getCursorPos()', space='world')

def getCursorPos():
    print mc.draggerContext( 'testContext', query=1, dragPoint=1) 

...这个应该会给你打印出点击时的世界坐标 XYZ 值。

撰写回答