To rephrase the real question being asked, how does one read, and then set, the OpenGL modelview matrix explicitly from Python code? For related reasons, I want to do that.
A big part of the answer can be found at http://www.rtrowbridge.com/blog/2009/02/15...r-python-users/. Ryan explains a lot of tricks with the MScriptUtil class, which is clearly key to getting things done here. It provides the way to get the required pointer types. For instance, you can read back the viewport information with this code:
[codebox]su = OpenMaya.MScriptUtil()
print glFT.glGetFloatv(OpenMayaRender.MGL_VIEWPORT,su.asFloatPtr())
for i in xrange(4):
print su.getFloatArrayItem(su.asFloatPtr(),i)[/codebox]
Accordingly, you might think this code would read back the modelview matrix:
[codebox]su = OpenMaya.MScriptUtil()
print glFT.glGetFloatv(OpenMayaRender.MGL_MODELVIEW_MATRIX,su.asFloatPtr())
for i in xrange(16):
print OpenMaya.MScriptUtil.getFloatArrayItem(su.asFloatPtr(),i)[/codebox]
Instead, this code hard-crashes Maya. Clearly, something needs to change, and presumably what need to change is to somehow get MScriptUtil to allocate the 16 storage locations ahead of time. MScriptUtil's documentation is kind of thin, though, and it's not clear exactly how to do that. I tried using setFloatArray ahead of time:
[codebox]su = OpenMaya.MScriptUtil()
fptr = su.asFloatPtr()
for i in xrange(16):
OpenMaya.MScriptUtil.setFloatArray(fptr,i,float(i))
print glFT.glGetFloatv(OpenMayaRender.MGL_MODELVIEW_MATRIX,fptr)
for i in xrange(16):
print OpenMaya.MScriptUtil.getFloatArrayItem(fptr,i)[/codebox]
That keeps Maya alive a little bit longer, but it still crashes after a few repeated executions. Another variant was to use createFromList:
[codebox]su = OpenMaya.MScriptUtil()
su.createFromList(map(float,xrange(16)),16)
print glFT.glGetFloatv(OpenMayaRender.MGL_MODELVIEW_MATRIX,su.asFloatPtr())
for i in xrange(16):
print OpenMaya.MScriptUtil.getFloatArrayItem(su.asFloatPtr(),i)[/codebox]
This doesn't crash at all, but instead of printing the expected matrix, it always prints the initialized values (i.e., [0.0, 1.0, ... 15.0]). Anyway, if anyone has figured out how to read back the OpenGL Matrix from Python code, the answer would be much appreciated!