当尝试使用maya的python OpenMayaUI和M3dView类来获取modelViewMatrix时,我遇到了一个问题。如果将视图设置为旧视口(vp1),则在使用cmds模块移动摄像机后查询modelViewMatrix,它将不会更新。在Viewport 2.0和高质量渲染器中,此相同的行为也可以正常工作。
代码示例:
from maya import cmds
from maya import OpenMaya
from maya import OpenMayaUI
def do_this():
"""
Get the current view and print it's modelViewMatrix.
Then move it.
"""
print('Doing this:')
activeView = OpenMayaUI.M3dView.active3dView()
cameraDagPath = OpenMaya.MDagPath()
activeView.getCamera(cameraDagPath)
cameraDagPath.pop()
modelView_MMatrix = OpenMaya.MMatrix()
activeView.modelViewMatrix(modelView_MMatrix)
for column in xrange(0, 4):
print(round(modelView_MMatrix(column, 0), 5),
round(modelView_MMatrix(column, 1), 5),
round(modelView_MMatrix(column, 2), 5),
round(modelView_MMatrix(column, 3), 5))
cmds.xform(
cameraDagPath.fullPathName(),
translation=[0.0, 1.0, 0.0],
relative=True)
cmds.xform(
cameraDagPath.fullPathName(),
rotation=[0.0, 1.0, 0.0],
relative=True)
cmds.refresh()
def do_that():
"""
Get the current view and print it's modelViewMatrix.
"""
print('Doing that:')
activeView = OpenMayaUI.M3dView.active3dView()
cameraDagPath = OpenMaya.MDagPath()
activeView.getCamera(cameraDagPath)
cameraDagPath.pop()
modelView_MMatrix = OpenMaya.MMatrix()
activeView.modelViewMatrix(modelView_MMatrix)
for column in xrange(0, 4):
print(round(modelView_MMatrix(column, 0), 5),
round(modelView_MMatrix(column, 1), 5),
round(modelView_MMatrix(column, 2), 5),
round(modelView_MMatrix(column, 3), 5))
do_this()
do_that()
Doing this:
(-0.9641, 0.10179, -0.24527, -0.0)
(0.0, 0.92362, 0.3833, -0.0)
(0.26556, 0.36954, -0.89046, -0.0)
(-1231.52977, -2128.24206, 3574.6381, 1.0)
Doing that:
(-0.9641, 0.10179, -0.24527, -0.0)
(0.0, 0.92362, 0.3833, -0.0)
(0.26556, 0.36954, -0.89046, -0.0)
(-1231.52977, -2128.24206, 3574.6381, 1.0)
如何正确获取此更新?我发现的唯一修复是在这两个函数之间选择视口,它将正确更新。有没有办法在Maya中强制刷新modelViewMatrix?感谢任何帮助!