白羽
2018-06-13
来源 :网络
阅读 2084
评论 0
摘要:本文将带你学习Maya API插件的2种形式,希望本文对大家学Maya有所帮助。
利用maya.standalone来运行。
通过maya的单机执行,是需要设置环境变量:
[python] view plain copy
1. #!/usr/bin/env python
2. # -*- coding: UTF-8 -*-
3.
4. #
5. # 描述:
6. # 运行这个程序将会启动一个Maya独立的python程序
7. #
8. # 使用方法:
9. #
10. # 设置MAYA_LOCATION环境变量到你的Maya安装路径并在Linux(Mac)的shell中执行:
11. #
12. # $MAYA_LOCATION/bin/mayapy helloWorld.py
13. #
14. # 注意:你必须使用Python可执行文件,才能正常工作。不同的平台它的路径都不一样。
15. # win系统的是:
16. #
17. # $MAYA_LOCATION/bin/mayapy.exe helloWorld.py
18.
19. # 导入相关模块
20. import maya.standalone
21. import maya.OpenMaya as OpenMaya
22.
23. import sys
24.
25. def main( argv=None ):
26. try:
27. # 尝试启动Maya独立的python程序
28. maya.standalone.initialize( name='python' )
29. except:
30. # 如果无法启动,输出错误信息
31. sys.stderr.write( "Failed in initialize standalone application" )
32. raise
33.
34. # 输出Hello world
35. sys.stderr.write( "Hello world! (script output)\n" )
36. # 执行print命令来输出Hello world
37. OpenMaya.MGlobal().executeCommand( "print \"Hello world! (command script output)\\n\"" )
38.
39. if __name__ == "__main__":
40. main()
2.利用maya的载入安装插件方法:
使用方法: 将helloWorldCmd.py放到 win C:\Documents and Settings\你的用户名\My Documents\maya\plug-ins mac /Users/你的用户名/Library/Preferences/Autodesk/maya/plug-ins
[python] view plain copy
1. #!/usr/bin/env python
2. # -*- coding: UTF-8 -*-
3.
4. '''''
5. Created on Oct 25, 2009
6.
7. @author: schi
8. '''
9. # 使用方法:
10. # Mel:
11. # loadPlugin helloWorldCmd.py;
12. # spHelloWorld;
13. #
14. # ---------------------------------------
15. #
16. # python:
17. # import maya.cmds as cmds
18. # cmds.loadPlugin("helloWorldCmd.py")
19. # cmds.spHelloWorld()
20.
21. # 导入相关模块
22. import sys
23. import maya.OpenMaya as om
24. import maya.OpenMayaMPx as ompx
25.
26. # 定义命令的名称
27. kPluginCmdName = 'spHelloWorld'
28.
29. # 命令
30. class SpHelloWorld( ompx.MPxCommand ):
31.
32. def __init__( self ):
33. # 你可以像官方一样使用未绑定的方法,ompx.MPxCommand.__init__(self)
34. # 我更倾向于super方法
35. super( SpHelloWorld, self ).__init__()
36.
37. # 执行spHelloWorld时会调用这个方法
38. def doIt( self, argList ):
39. # 如果你对Hello World很反感,可以改成自己的代码
40. print "Hello World!"
41.
42. # Creator用于创建命令的一个实例
43. def cmdCreator():
44. return ompx.asMpxPtr( SpHelloWorld() )
45.
46. # 注册插件
47. def initializePlugin( mobject ):
48. mplugin = ompx.MFnPlugin( mobject )
49. try:
50. mplugin.registerCommand( kPluginCmdName, cmdCreator )
51. except:
52. sys.stderr.write( '插件 %s 注册失败\n' % kPluginCmdName )
53. raise
54.
55. # 注销插件
56. def uninitializePlugin( mobject ):
57. mplugin = ompx.MFnPlugin( mobject )
58. try:
59. mplugin.deregisterCommand( kPluginCmdName )
60. except:
61. sys.stderr.write( '插件 %s 注销失败\n' % kPluginCmdName )
62. raise
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标常用软件之Maya频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号