maya api 造球大法2.0

利用Maya API1.0创建10000个球体,我测试花了8.52秒,多次测试不会超过9秒最低7秒。比上次优化了10倍,也算是这段时间的进步吧。。
cmds:


image.png

api:


image.png

creatspheres.gif

还有undo和redo的功能,之前写的代码是没法undo的,这是不符合用户习惯的,所以我这次进行的改进。
sphere_undo.gif
import maya.OpenMaya as om
import maya.OpenMayaMPx as ompx

class Creat_Sphere(ompx.MPxCommand):

    COMMAND_NAME = "Creat_Sphere"
   
    magnitudesFlag = ["-m", "-magnitude"]
    helpFlag = ["-h", "-help"]
    helpText = '\nThe command is used to create multiple spheres\n'


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

    def craeat_sphere(self, magnitude):
        # Create Transform Node
        self.transform_fn = om.MFnDagNode()
        self.transform_obj = self.transform_fn.create("transform", "pSphere{}".format(magnitude+1))

        # Create Mesh Node
        self.mesh_fn = om.MFnDagNode()
        self.mesh_obj = self.mesh_fn.create("mesh", "pSphereShape{}".format(magnitude+1), self.transform_obj)

        # Create PolySphere Node
        self.sphere_fn = om.MFnDependencyNode()
        self.sphere_obj = self.sphere_fn.create("polySphere")

        # Find Sphere node's output plug
        self.src_plug = self.sphere_fn.findPlug("output", False)

        # Find Mesh node's inMesh plug
        self.dest_plug = self.mesh_fn.findPlug("inMesh", False)

        # Connect PolySphere node's output to Mesh node's input
        self.dg_mod = om.MDGModifier()
        self.dg_mod.connect(self.src_plug, self.dest_plug)
        self.dg_mod.doIt()

        # Get initialShadingGroup MObject
        self.selection_list = om.MSelectionList()
        self.selection_list.add("initialShadingGroup")

        # Add mesh object to the initialShadingGroup set
        self.mobj = om.MObject()
        self.selection_list.getDependNode(0, self.mobj)
        self.set_fn = om.MFnSet(self.mobj)
        self.set_fn.addMember(self.mesh_obj)
        # Return Base Node
        return self.transform_obj
        
    def doIt(self, arg_list):
        try:
            self.argDatabase = om.MArgDatabase(self.syntax(), arg_list)
        except RuntimeError:
            om.MGlobal.displayError('Error while parsing arguments.')
            raise
        
        # Add help info   
        help = self.argDatabase.isFlagSet(Creat_Sphere.helpFlag[0])
        if help:
            self.setResult(Creat_Sphere.helpText)
       
        # Add magnitudes to creat sphere      
        if self.argDatabase.isFlagSet(Creat_Sphere.magnitudesFlag[0]):
            self.magnitudes = self.argDatabase.flagArgumentInt(Creat_Sphere.magnitudesFlag[0], 0)
        else:
            self.magnitudes = 1
        self.creat_mutisphere = map(self.craeat_sphere, range(self.magnitudes))
        
    def undoIt(self):
        map(om.MGlobal.deleteNode, self.creat_mutisphere)

    def redoIt(self):
        self.creat_mutisphere = map(self.craeat_sphere, range(self.magnitudes))

    def isUndoable(self):
        return True

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

    @classmethod
    def create_syntax(cls):
        syntax = om.MSyntax()
        # Add flags here
        syntax.addFlag(Creat_Sphere.magnitudesFlag[0], Creat_Sphere.magnitudesFlag[1], om.MSyntax.kLong)
        syntax.addFlag(Creat_Sphere.helpFlag[0], Creat_Sphere.helpFlag[1])

        return syntax
    
    
def initializePlugin(plugin):
    """
    """
    vendor = "Steven Qiu"
    version = "2.0.0"

    plugin_fn = ompx.MFnPlugin(plugin, vendor, version)
    try:
        plugin_fn.registerCommand(Creat_Sphere.COMMAND_NAME, Creat_Sphere.creator, Creat_Sphere.create_syntax)
    except:
        om.MGlobal.displayError("Failed to register command: {0}".format(Creat_Sphere.COMMAND_NAME))


def uninitializePlugin(plugin):
    """
    """
    plugin_fn = ompx.MFnPlugin(plugin)
    try:
        plugin_fn.deregisterCommand(Creat_Sphere.COMMAND_NAME)
    except:
        om.MGlobal.displayError("Failed to deregister command: {0}".format(Creat_Sphere.COMMAND_NAME))

你可能感兴趣的:(maya api 造球大法2.0)