MayaAPI学习案例

Arya Lv3

前言✨

在学习MayaAPI的过程中,顺便总结了一些案例,在之后的学习中也会常更新,主要是方便查阅。理论知识在这里

拍屏显示案例–DrawOverride

这个是一个修改拍屏上文字的案例,利用继承openMaya中的MPxDrawOverride,创建drawOverride插件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import maya.api.OpenMaya as om2
import maya.api.OpenMayaUI as omui2
import maya.api.OpenMayaRender as omr2
import maya.cmds as cmds

def maya_useNewAPI():
pass

class HelloWorldNode(omui2.MPxLocatorNode):
TYPE_NAME = "helloworld"
TYPE_ID=om2.MTypeId(0x0007f7f7)
DRAW_CLASSIFICATION = "drawdb/geometry/helloworld"
DRAW_REGISTRANT_ID = "HelloWorldNone"

def __init__(self):
super(HelloWorldNode,self).__init__()

@classmethod
def creator(cls):
return HelloWorldNode()

@classmethod
def initialize(cls):
pass

class HelloWorldDrawOverride(omr2.MPxDrawOverride):
NAME="HelloWorldDrawOverride"

def __init__(self,mobj):
super(HelloWorldDrawOverride,self).__init__(mobj,None,False)

def prepareForDraw(self,obj_path,camera_path,frame_context,old_data):
pass

def supportedDrawAPIs(self):
return omr2.MRenderer.kAllDevices

def hasUIDrawables(self):
return True

def addUIDrawables(self,obj_path,draw_manager,frame_context,data):
draw_manager.beginDrawable()
draw_manager.text2d(om2.MPoint(100,100),'Hello World')
draw_manager.endDrawable()

@classmethod
def creator(cls,obj):
return HelloWorldDrawOverride(obj)


def initializePlugin(plugin):
vendor='AryaGala'
version='1.0.0'
plugin_fn = om2.MFnPlugin(plugin, vendor, version)
try:
plugin_fn.registerNode(HelloWorldNode.TYPE_NAME,
HelloWorldNode.TYPE_ID,
HelloWorldNode.creator,
HelloWorldNode.initialize,
om2.MPxNode.kLocatorNode,
HelloWorldNode.DRAW_CLASSIFICATION)
except:
om2.MGlobal.displayError("Failed to register node: {}".format(HelloWorldNode.TYPE_NAME))

try:
omr2.MDrawRegistry.registerDrawOverrideCreator(HelloWorldNode.DRAW_CLASSIFICATION,
HelloWorldNode.DRAW_REGISTRANT_ID,
HelloWorldDrawOverride.creator)
except:
om2.MGlobal.displayError("Failed to register node: {}".format(HelloWorldDrawOverride.NAME))

def uninitializePlugin(plugin):
plugin_fn = om2.MFnPlugin(plugin)
try:
omr2.MDrawRegistry.deregisterDrawOverrideCreator(HelloWorldNode.DRAW_CLASSIFICATION,
HelloWorldNode.DRAW_REGISTRANT_ID)
except:
om2.MGlobal.displayError("Failed to deregister node: {}".format(HelloWorldDrawOverride.NAME))
try:
plugin_fn.deregisterNode(HelloWorldNode.TYPE_ID)
except:
om2.MGlobal.displayError("Failed to deregister command: {}".format(HelloWorldNode.TYPE_NAME))

if __name__=="__main__":
cmds.file(new=True, force=True)
plugin_name="helloword_plugin.py"
cmds.evalDeferred('if cmds.pluginInfo("{0}",q=True,loaded=True): cmds.unloadPlugin("{0}")'.format(plugin_name))
cmds.evalDeferred('if not cmds.pluginInfo("{0}",q=True,loaded=True): cmds.loadPlugin("{0}")'.format(plugin_name))
cmds.evalDeferred('cmds.createNode("helloworld")')

MPlug查询修改属性值案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import maya.api.OpenMaya as om
node_name = "pCube1"
attribute_name1 = "translateY"
attribute_name2 = "translate"

selection_list = om.MSelectionList()
selection_list.add(node_name)
obj = selection_list.getDependNode(0)

if obj.hasFn(om.MFn.kTransform):
# 查询设置单属性值
transform_fn = om.MFnTransform(obj)
plug1 = transform_fn.findPlug(attribute_name1, False)
attribute_value = plug1.asDouble()
print(f"{plug1}, {attribute_value}")
plug1.setDouble(2.0)

#查询设置复合属性值
plug2 = transform_fn.findPlug(attribute_name2, False)
if plug2.isCompound:
for i in range(plug.numChildren()):
child_plug = plug.child(i)
attribute_value = child_plug.asDouble()
print(f"{child_plug}, {attribute_value}")

乘法节点案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import maya.api.OpenMaya as om2
import maya.api.OpenMayaUI as omui2
import maya.cmds as cmds

def maya_useNewAPI():
pass

class MultiplyNode(om2.MPxNode):
TYPE_NAME = "multi"
TYPE_ID=om2.MTypeId(0x0007f7f8)

multiplier_obj = None
multiplicand_obj = None
product_obj = None

def __init__(self):
super(MultiplyNode, self).__init__()

def compute(self, plug, data):
"""该函数一般在输入属性数值更新的时候,会对输入属性影响的输出属性进行计算
plug: 更新的输入属性所影响的输出属性的MPlug
data: MDataBlock,提供读取和写入输出属性值或输入属性值的句柄。
"""
# 当调用了compute方法,判断该值是否为脏,如果为脏运行下面的逻辑
if plug == MultiplyNode.product_obj:
multiplier = data.inputValue(MultiplyNode.multiplier_obj).asInt()
multiplicand = data.inputValue(MultiplyNode.multiplicand_obj).asDouble()
product = multiplier * multiplicand

product_data_handle = data.outputValue(MultiplyNode.product_obj)
product_data_handle.setDouble(product)

# 将所有的属性都设置为干净的
data.setClean(plug)


@classmethod
def creator(cls):
return MultiplyNode()

@classmethod
def initialize(cls):
#初始化节点,通常在这一步创建属性和将属性添加到节点上去以及设置属性间的影响关系

# create attribute
numeric_attr = om2.MFnNumericAttribute()

cls.multiplier_obj = numeric_attr.create("multiplier","mul",om2.MFnNumericData.kInt,2)
numeric_attr.keyable = True # 当这个选项为False时,属性插点没有办法在节点上显示出来
numeric_attr.readable = False

cls.multiplicand_obj = numeric_attr.create("multiplicand","mulc",om2.MFnNumericData.kDouble,0.0)
numeric_attr.keyable = True
numeric_attr.readable = False

cls.product_obj = numeric_attr.create("product","prod",om2.MFnNumericData.kDouble,0.0)
numeric_attr.writable = False


# add attribute
cls.addAttribute(cls.multiplier_obj)
cls.addAttribute(cls.multiplicand_obj)
cls.addAttribute(cls.product_obj)

# add attribute effect
# 如果没有设置影响关系,节点上不会显示输入和输出属性的插点
cls.attributeAffects(cls.multiplier_obj, cls.product_obj)
cls.attributeAffects(cls.multiplicand_obj, cls.product_obj)



def initializePlugin(plugin):
vendor='AryaGala'
version='1.0.0'
plugin_fn = om2.MFnPlugin(plugin, vendor, version)
try:
plugin_fn.registerNode(MultiplyNode.TYPE_NAME,
MultiplyNode.TYPE_ID,
MultiplyNode.creator,
MultiplyNode.initialize,
om2.MPxNode.kDependNode)
except:
om2.MGlobal.displayError("Failed to register node: {}".format(MultiplyNode.TYPE_NAME))

def uninitializePlugin(plugin):
plugin_fn = om2.MFnPlugin(plugin)
try:
plugin_fn.deregisterNode(MultiplyNode.TYPE_ID)
except:
om2.MGlobal.displayError("Failed to deregister command: {}".format(MultiplyNode.TYPE_NAME))


if __name__=="__main__":
cmds.file(new=True, force=True)
plugin_name="multiplyNode.py"
cmds.evalDeferred('if cmds.pluginInfo("{0}",q=True,loaded=True): cmds.unloadPlugin("{0}")'.format(plugin_name))
cmds.evalDeferred('if not cmds.pluginInfo("{0}",q=True,loaded=True): cmds.loadPlugin("{0}")'.format(plugin_name))
cmds.evalDeferred('cmds.createNode("multi")')
  • 标题: MayaAPI学习案例
  • 作者: Arya
  • 创建于 : 2023-12-07 08:00:00
  • 更新于 : 2024-06-13 17:31:08
  • 链接: https://aryagala0.github.io/2023/12/07/Maya/MayaAPI案例/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
 评论