Maya旧版视口modelViewMatrix未正确更新

2024-06-06 22:35:08 发布

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

我在尝试使用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?谢谢你的帮助!在


Tags: columnthisdoprintdoingcmdsmayaround