Created
June 17, 2021 10:06
-
-
Save nrtkbb/3f23b9daa26329b014bf9cbfff15691c to your computer and use it in GitHub Desktop.
MObject や MDagPath などを dict のキーに入れるためのラッパークラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from maya.api.OpenMaya import MGlobal | |
from mhash import MHash | |
selection_list = MGlobal.getSelectionListByName('persp') | |
# MObject は直接 dict のキーにできないけど | |
mobject = selection_list.getDependNode(0) | |
# MHashのインスタンスなら dict のキーにできる | |
mhash1 = MHash(mobject) | |
a = {mhash1: 'hoge'} | |
# MObject だけじゃなくて MDagPath や MFn~も同様に MHash に入れられる | |
dag_path = selection_list.getDagPath(0) | |
mhash2 = MHash(dag_path) | |
# mhash2 から dag_path のメソッドを直接呼べる(プロパティも) | |
print(mhash2.fullPathName()) | |
# こちらも同様に dict のキーにできる | |
b = {mhash2: 'hoge'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MHash(object): | |
def __init__(self, m): | |
self.m = m | |
self.m_dir = set(dir(m)) | |
def __eq__(self, other): | |
# もともと MObject に実装されてる __eq__ を呼ぶだけ | |
return self.m == other.m | |
def __hash__(self): | |
# __hash__ もあるので呼んであげるだけ | |
return self.m.__hash__() | |
def __getattr__(self, name): | |
# まずは自分のクラスに存在するアトリビュートを確認して、存在すればそれを返すけど | |
if name in self.__dict__: | |
return self.__dict__[name] | |
# 定義していないプロパティやメソッドは self.m に委譲する | |
if name in self.m_dir: | |
return self.m.__getattribute__(name) | |
raise AttributeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment