记录 Maya 的 PythonAPI 命令插件示例.

# -*- coding: utf-8 -*-
# !/usr/bin/env python2
# @Author: Mirror
# @Time: 2022/12/10
# description: Maya插件示例
# =========================================
import sys
import maya.api.OpenMaya as api


def maya_useNewAPI():
    u"""用于告诉Maya使用API2.0"""
    pass

class HelloWorldCmd(api.MPxCommand):
    kCmdName = "helloWorld"  # 命令名称

    def __inin__(self):
        api.MPxCommand.__init__(self)


    @staticmethod
    def creator():
        return HelloWorldCmd()

    def doIt(self, *args):
        u"""命令内容"""
        print("Hello,World!")

def initializePlugin(mobject):
    u"""注册插件"""
    fn_plugin = api.MFnPlugin(mobject, "mirrorcg.com", "1.0.0", "any")
    try:
        fn_plugin.registerCommand(
            HelloWorldCmd.kCmdName,
            HelloWorldCmd.creator
        )
    except:
        sys.stdout.write(u"Failed to register command: " + HelloWorldCmd.kCmdName)

def uninitializePlugin(mobject):
    u"""卸载插件"""
    fn_plugin = api.MFnPlugin(mobject)
    try:
        fn_plugin.deregisterCommand(
            HelloWorldCmd.kCmdName
        )
    except:
        sys.stdout.write(u"Failed to deregister command: " + HelloWorldCmd.kCmdName)