sierpinski triangle 2d maya plug-in(with python API 2.0)

阅读更多
因为python API 2.0可用的类很少,OpenMayaMpx模块里的类被整合到OpenMaya里面了,这个模块不再存在,里面的一些方法也被丢弃了,所以使用python API 2.0来写插件和1.0是有区别的,我这里就以sierpinski triangle 2d为例来看看怎么使用python API 2.0来写插件.

首先,我们的插件是一个命令插件,就是创建一mel命令,命令只有一个-times(-t)参数可使用,接受的数据类型是int,和一个以上的三角形节点,使用方法
sierpinskiTriangle -t 5 objects;

或是
from maya import cmds
cmds.sierpinskiTriangle(objects, t=5)

sierpinskiTriangleCmd.py
import sys
import maya.api.OpenMaya as om
from maya import cmds

def maya_useNewAPI():
    pass

class STCmd(om.MPxCommand):
    
    cmdName = "sierpinskiTriangle"
    timeFlag = "-t"
    timeLongFlag = "-times"
    
    def __init__(self):
        super(STCmd, self).__init__()
    
    def doIt(self, args):
        times = 1
        timeFlagIndex = args.flagIndex(STCmd.timeFlag, STCmd.timeLongFlag)
        if timeFlagIndex != om.MArgList.kInvalidArgIndex:
            times = args.asInt(timeFlagIndex+1)
        argData = om.MArgDatabase(self.syntax(), args)
        objList = argData.getObjectList()
        if objList.isEmpty():
            self.displayError("No objects pass to the command or nothing is selected.")
        for i in range(objList.length()):
            objPath = objList.getDagPath(i)
            if objPath.apiType() == om.MFn.kMesh:
                obj = objPath
            else:
                obj = objPath.child(0)
                if obj.apiType() != om.MFn.kMesh:
                    continue
            
            meshFn = om.MFnMesh(obj)
            if meshFn.numEdges == 3:
                name = meshFn.name()
                for j in range(times):
                    for k in range(meshFn.numPolygons):
                        cmds.select("%s.f[%d]" % (name, k), r=1)
                        edges = cmds.polyInfo(fe=1)[0].split()[-3:]
                        meshFn.syncObject()
                        meshFn.split(((meshFn.kOnEdge, int(edges[2]), 0.5), (meshFn.kOnEdge, int(edges[1]), 0.5), (meshFn.kOnEdge, int(edges[0]), 0.5), (meshFn.kOnEdge, int(edges[2]), 0.5)))
                        meshFn.deleteFace(meshFn.numPolygons - 1)
                        meshFn.updateSurface()
        cmds.select(cl=1)

def cmdCreator():
    return STCmd()

def syntaxCreator():
    syntax = om.MSyntax()
    syntax.addFlag(STCmd.timeFlag, STCmd.timeLongFlag, syntax.kUnsigned)
    syntax.setObjectType(syntax.kSelectionList)
    syntax.useSelectionAsDefault(True)
    return syntax

def initializePlugin(mobject):
    mplugin = om.MFnPlugin(mobject, "mack stone", "0.1")
    try:
        mplugin.registerCommand(STCmd.cmdName, cmdCreator, syntaxCreator)
    except:
        sys.stderr.write("Failed to register command: %s\n" % STCmd.cmdName)
        raise

def uninitializePlugin(mobject):
    mplugin = om.MFnPlugin(mobject)
    try:
        mplugin.deregisterCommand(STCmd.cmdName)
    except:
        sys.stderr.write("Failed to unregister command: %s\n" % STCmd.cmdName)
        raise

要让maya知道你的插件是使用python API 2.0来写,必须要定义这个函数,不知道以后会不会有所改变
def maya_useNewAPI():
    pass

然后其它的基本一样,因为OpenMayaMpx模块被丢弃了,所以你只需要返回你命令类的实例就行了
def cmdCreator():
    return STCmd()

你可能感兴趣的:(maya,Maya,Python,API,2.0,python)