我是 Maya 插件開發的新手,目前我正在嘗試與 OpenMaya 庫中的“MImage”類/函式進行互動。我正在嘗試使用“getSize”獲取影像的尺寸。我找不到資訊豐富的檔案,而且該函式的行為不像我期望的那樣。如果它是“pythonic”,我希望它回傳一個元組 (x,y) 但它似乎想要整數,通過參考傳遞,我不確定如何在 python 中執行此操作。
img = om.MImage()
img.readFromFile(currentFile)
dir(img)
w = int(0)
h = int(0)
img.getSize(w, h)
我試過這個,但我得到了非常像 c 的錯誤:
'MImage_getSize','unsigned int &' 型別的引數 2
暗示這也許是一個自動生成的界面,而不是用 python 手工編碼的東西?無論如何,我不確定如何使用它。
uj5u.com熱心網友回復:
我建議使用 Maya Python API 2.0,因為它更像Pythonic。
MImage.getSize(),例如,直接回傳影像的寬度和高度 - 參見檔案頁面:
OpenMaya.MImage.getSize()
getSize() -> [width, height]
Get the width and height of the currently opened image.
這應該有效(注意匯入行):
import maya.api.OpenMaya as om
img = om.MImage()
img.readFromFile('/tmp/texture.png')
print(img.getSize())
輸出:
[512L, 512L]
uj5u.com熱心網友回復:
好的,所以我找到了一個解決方案,但它非常非常難看:
scriptUtilWidth = om.MScriptUtil()
scriptUtilHeight = om.MScriptUtil()
widthPtr = scriptUtilWidth.asUintPtr()
heightPtr = scriptUtilHeight.asUintPtr()
img.getSize( widthPtr, heightPtr)
w = scriptUtilWidth.getUint(widthPtr)
h = scriptUtilWidth.getUint(heightPtr)
有人知道更優雅的解決方案嗎?看起來很原始。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326711.html
