玛雅胶卷基质

时间:2015-04-28 16:59:35

标签: python matrix maya

有没有人知道如何使用枢轴(后投影矩阵)计算maya的滚动矩阵?

目前我能得到的最好的是:

template <typename T> struct ValueExtractor
{
   static T get(std::string const& s)
   {
      T out;
      std::stringstream ss(s);
      ss >> out;
      return out;
   }
};

其中没有说明胶卷转轴。任何矩阵专业人员都知道如何计算它?

1 个答案:

答案 0 :(得分:0)

经过大量的反复试验,我得到了一个分享的答案。目前这并不代表电影翻译:

import math
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
import maya.OpenMayaUI as OpenMayaUI

# Get active view.
view = OpenMayaUI.M3dView.active3dView()

# Get camera MFnDagPath.
dagCam = OpenMaya.MDagPath()
view.getCamera(dagCam)

# Build MFnCamera.
fnCam = OpenMaya.MFnCamera(dagCam)

# Get post matrix.
postMatrix = fnCam.postProjectionMatrix()

# Get current film roll value to calculate roll.
filmRollValue = cmds.getAttr("%s.filmRollValue" % dagCam.fullPathName())
filmRollX = cmds.getAttr("%s.horizontalRollPivot" % dagCam.fullPathName())
filmRollY = cmds.getAttr("%s.verticalRollPivot" % dagCam.fullPathName())

dAspect = cmds.getAttr("defaultResolution.deviceAspectRatio")

# This is how rotation is calculated.
cz = math.cos(math.radians(filmRollValue))
sz = math.sin(math.radians(filmRollValue))

# Build the matrix.
# Row 1.
# Ensure to multiply mat[0][1] and mat[1][0] by device aspect.
matList = [0.0] * 16
matList[0] = cz
matList[1] = -sz * dAspect
matList[2] = 0.0
matList[3] = 0.0

# Row 2.
matList[4] = sz / dAspect
matList[5] = cz
matList[6] = 0.0
matList[7] = 0.0

# Row 3.
matList[8] = 0.0
matList[9] = 0.0
matList[10] = 1.0
matList[11] = 0.0

# Row 4.
# Here we're simply moving based along the rotation.
# We move it based on the offset from origin to film pivot.
# We multiply the y by the device aspect.
matList[12] = ((cz * -filmRollX) + (sz * (-filmRollY)) + filmRollX)
matList[13] = ((-sz * -filmRollX) + (cz * -filmRollY) + filmRollY) * dAspect
matList[14] = 0.0
matList[15] = 1.0

# Create MMatrix.
rollMMatrix = OpenMaya.MMatrix()
OpenMaya.MScriptUtil().createMatrixFromList(matList, rollMMatrix)

# Print values.
print("\nFilm Roll Value: %s" % filmRollValue)
print("Film Roll Pivot X: %s" % filmRollX)
print("Film Roll Pivot Y: %s" % filmRollY)

# I round because I don't want to see scientific notation.
print("\nMy Post Projection Matrix:")
for x in xrange(0, 4):
    print(round(rollMMatrix(x, 0), 5),
          round(rollMMatrix(x, 1), 5),
          round(rollMMatrix(x, 2), 5),
          round(rollMMatrix(x, 3), 5))

print("\nPost Projection Matrix from Maya:")
for x in xrange(0, 4):
    print(round(postMatrix(x, 0), 5),
          round(postMatrix(x, 1), 5),
          round(postMatrix(x, 2), 5),
          round(postMatrix(x, 3), 5))


Film Roll Value: 45.0
Film Roll Pivot X: 1.0
Film Roll Pivot Y: 1.0

My Post Projection Matrix:
(0.70711, -1.25708, 0.0, 0.0)
(0.39775, 0.70711, 0.0, 0.0)
(0.0, 0.0, 1.0, 0.0)
(-0.41421, 1.77778, 0.0, 1.0)

Post Projection Matrix from Maya:
(0.70711, -1.25708, 0.0, 0.0)
(0.39775, 0.70711, 0.0, 0.0)
(0.0, 0.0, 1.0, 0.0)
(-0.41421, 1.77778, 0.0, 1.0)