MImage_getSize 在玛雅 python

MImage_getSize in maya python

我是 Maya 插件开发的新手,目前我正在尝试与 OpenMaya 库中的 'MImage' class/function 交互。 我正在尝试使用 'getSize' 获取图像的尺寸。我找不到信息丰富的文档,而且该函数的行为与我预期的不一样。如果它是 'pythonic',我希望它是 return 一个元组 (x,y) 但它似乎想要整数,通过引用传递,我不确定如何在 python.

img = om.MImage()
img.readFromFile(currentFile)
dir(img)
w = int(0)
h = int(0)
img.getSize(w, h)

我已经试过了,但我得到了非常类似的错误:

'MImage_getSize', argument 2 of type 'unsigned int &'

暗示这可能是一个自动生成的界面,而不是考虑到 python 的手动编码?无论如何,我不确定如何使用它。

好的,所以我找到了解决方案,但它非常非常难看:

scriptUtilWidth = om.MScriptUtil()
scriptUtilHeight = om.MScriptUtil()
widthPtr = scriptUtilWidth.asUintPtr()
heightPtr = scriptUtilHeight.asUintPtr()
img.getSize( widthPtr, heightPtr)
w = scriptUtilWidth.getUint(widthPtr)
h = scriptUtilWidth.getUint(heightPtr)

有人知道更优雅的解决方案吗?看起来很原始。

我建议使用 Maya Python API 2.0,因为它更 Pythonic

MImage.getSize(),比如直接returns图片的宽高-见doc page:

OpenMaya.MImage.getSize()
  getSize() -> [width, height]

Get the width and height of the currently opened image.

这应该有效(注意 import 行):

import maya.api.OpenMaya as om

img = om.MImage()
img.readFromFile('/tmp/texture.png')
print(img.getSize())

输出:

[512L, 512L]