I'm new to Maya plugin development and currently I'm trying to interface with the 'MImage' class/function in the OpenMaya library. I'm trying to get the dimensions of an image using 'getSize'. I can't find informative documentation and the function is not behaving as I'd expect it to. Were it 'pythonic', I'd expect it to return a tuple (x,y) but instead it seems to want integers, passed by reference and I'm not sure how to do this in python.
img = om.MImage()
img.readFromFile(currentFile)
dir(img)
w = int(0)
h = int(0)
img.getSize(w, h)
I've tried this but I get very c like error:
'MImage_getSize', argument 2 of type 'unsigned int &'
Suggesting that perhaps this was an automatically generated interface rather than something hand coded with python in mind? I'm not sure how to use it, in any case.
CodePudding user response:
Ok, so I found a solution but it's very, very ugly:
scriptUtilWidth = om.MScriptUtil()
scriptUtilHeight = om.MScriptUtil()
widthPtr = scriptUtilWidth.asUintPtr()
heightPtr = scriptUtilHeight.asUintPtr()
img.getSize( widthPtr, heightPtr)
w = scriptUtilWidth.getUint(widthPtr)
h = scriptUtilWidth.getUint(heightPtr)
does anybody know of a more elegant solution to this? It seems very raw.
CodePudding user response:
I would suggest using Maya Python API 2.0 since it's more Pythonic.
MImage.getSize()
, for example, directly returns the image's width and height - see doc page:
OpenMaya.MImage.getSize()
getSize() -> [width, height]
Get the width and height of the currently opened image.
This should work (note the import line):
import maya.api.OpenMaya as om
img = om.MImage()
img.readFromFile('/tmp/texture.png')
print(img.getSize())
Output:
[512L, 512L]