helloWorldCmd.py

这个helloWorldCmd.py和helloWorld.py不同,后者是通过maya.standalone来运行的,而前者是以插件的形式来使用的。
使用方法:
将helloWorldCmd.py放到
win
C:\Documents and Settings\你的用户名\My Documents\maya\plug-ins

mac
/Users/你的用户名/Library/Preferences/Autodesk/maya/plug-ins

如果不存在plug-ins文件夹,就创建一个
在脚本编辑器中执行:
loadPlugin helloWorldCmd.py;
spHelloWorld;


import maya.cmds as cmds
cmds.loadPlugin("helloWorldCmd.py")
cmds.spHelloWorld()


helloWorldCmd.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
Created on Oct 25, 2009

@author: schi
'''
# 使用方法:
# Mel:
# loadPlugin helloWorldCmd.py;
# spHelloWorld;
#
# ---------------------------------------
#
# python:
# import maya.cmds as cmds
# cmds.loadPlugin("helloWorldCmd.py")
# cmds.spHelloWorld()

# 导入相关模块
import sys
import maya.OpenMaya as om
import maya.OpenMayaMPx as ompx

# 定义命令的名称
kPluginCmdName = 'spHelloWorld'

# 命令
class SpHelloWorld( ompx.MPxCommand ):

    def __init__( self ):
        # 你可以像官方一样使用未绑定的方法,ompx.MPxCommand.__init__(self)
        # 我更倾向于super方法
        super( SpHelloWorld, self ).__init__()

    # 执行spHelloWorld时会调用这个方法
    def doIt( self, argList ):
        # 如果你对Hello World很反感,可以改成自己的代码
        print "Hello World!"

# Creator用于创建命令的一个实例
def cmdCreator():
    return ompx.asMpxPtr( SpHelloWorld() )

# 注册插件
def initializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject )
    try:
        mplugin.registerCommand( kPluginCmdName, cmdCreator )
    except:
        sys.stderr.write( '插件 %s 注册失败\n' % kPluginCmdName )
        raise

# 注销插件
def uninitializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject )
    try:
        mplugin.deregisterCommand( kPluginCmdName )
    except:
        sys.stderr.write( '插件 %s 注销失败\n' % kPluginCmdName )
        raise

你可以在maya安装目录下的devkit/plug-ins/scripted找到helloWorldCmd.py。
在线版
http://download.autodesk.com/us/maya/2010help/API/hello_world_cmd_8py-example.html

你可能感兴趣的:(C++,c,python,C#,脚本)