[PYTHON] First command plugin

Overview

Describes the contents of the Maya command plug-in for beginners.

--What is a command plugin? ――How can you use the command plug-in? ――What kind of reference article do you have?

motivation

You don't see much information about command plugins. By the way, I didn't use it at all ... So my aim is to write an introductory article for myself. Anyway, the content is light because I think I will increase the number of articles about Maya Python.

** What is a command plugin? ** **

Official Help says:

Define a new custom MEL command and respond accordingly add functions to the maya.cmds Python module so they can be used in scripts.

In short, you can call your own Python module directly from cmds.

** Command plugin to do cmds.ls (sl = True) **

Anyway, it's quick to write and use it. So, let's write the function of cmds.ls (sl = True) with a command plugin. The following code is based on (or copied) from Maya Help.

command_select.py


# -*- coding: utf-8 -*-
import maya.api.OpenMaya as OpenMaya


def maya_useNewAPI():
    pass

class CommandSelection(OpenMaya.MPxCommand):
    kPluginCmdName = "selection"

    def __init__(self):
        OpenMaya.MPxCommand.__init__(self)

    @staticmethod
    def cmdCreator():
        return CommandSelection()

    def doIt(self, args):
        self.setResult(list(OpenMaya.MGlobal.getActiveSelectionList().getSelectionStrings()))
 
def initializePlugin(mobject):
    plugin = OpenMaya.MFnPlugin(mobject)
    plugin.registerCommand(CommandSelection.kPluginCmdName, CommandSelection.cmdCreator)

def uninitializePlugin(mobject):
    pluginFn = OpenMaya.MFnPlugin(mobject)
    pluginFn.deregisterCommand(CommandSelection.kPluginCmdName)

command_select_doit.py


cmds.selection()
>>> # Result: [u'pPlane2', u'pPlane1'] # 

Code content

* About the return value *

The return value of the command is not a return statement! (Prejudice) When I check Help, it says setResult (), so I call it with doIt ().

When I tried to setResult () my own class to improve it a little, An error occurs with # Command results must be string or numeric.. I haven't investigated the case of other parent classes, but I wonder if the command plugin can only return strings or numbers.

* About the parent class *

Classes prefixed with> ** MPx ** are called proxy classes and are called proxy classes.

Used to create various plugin types.

So I looked at the Reference to see what other classes are available. Which parent should be inherited according to the plugin you want to make? I need more study.

Class Description
MPxAttributePatternFactory Base class for custom attribute pattern factories.
MPxCommand Base class for custom commands.
MPxData Base Class for User-defined Dependency Graph Data Types.
MPxGeometryData Base Class for User-defined Dependency Graph Geometry Data Types.
MPxGeometryIterator Base class for user defined geometry iterators.
MPxNode Base class for user defined dependency nodes.
MPxSurfaceShape Parent class of all user defined shapes.

** Impression **

If you use a command plug-in for processing that seems to be used very often, the amount of code will be reduced and it may be easier. It can also be used as a MEL command, so it may be useful for MELer artists. I would like to continue studying while waiting for advice and advice from various fields.

** Reference site **

-Digital Matrix Simple Plugin -Digital Matrix Command to check selected node -[Autodesk Knowledge Network: Command Plugin](https://knowledge.autodesk.com/en/support/maya/learn-explore/caas/documentation/MAYAUL/2014/JPN/Maya-API-Documentation/files/Command -plugins-htm.html)

Recommended Posts

First command plugin
Linux command # 4
Linux command # 3
First Flask
Command memorandum
nkf command
command prompt
First draft
vim command
sed command
First python-review-
Linux command # 5
grep command
command memo
First gdb
top command
Command memorandum
Command memo
mv command
seq command