basicObjectSet.py

这是一个自定义节点和命令都同时存在的一个例子。

basicObjectSet.py

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

# 使用:
# python
# import maya.cmds as cmds
# cmds.spBasicObjectSetTest()
#
# Mel
# spBasicObjectSetTest;

import maya.OpenMaya as om
import maya.OpenMayaMPx as ompx
import math, sys

kNodeName = 'spBasicObjectSet'
kCmdName = 'spBasicObjectSetTest'
kNodeId = om.MTypeId( 0x87012 )

# Node definition
# 定义节点
class BasicObjectSet( ompx.MPxObjectSet ):
    def __init__( self ):
        #ompx.MPxObjectSet.__init__( self )
        super( BasicObjectSet, self ).__init__()
        
# Cmd definition
# 定义命令
class BasicObjectSetTest( ompx.MPxCommand ):
    def __init__( self ):
        #ompx.MPxCommand.__init__( self )
        super( BasicObjectSetTest, self ).__init__()
        # 创建 MDGModifier 实例
        # 用于创建、删除和更改Dependency graph上的节点。
        # 它能自动地为所有操作提供撤销和重做。
        self.__fDGMod = om.MDGModifier()
        
    def doIt( self, args ):
        # Create the node
        # 创建节点
        #
        setNode = self.__fDGMod.createNode( kNodeId )
        self.__fDGMod.doIt()
        
        # Populate the set with the selected items
        # 将所选物体加入到set中
        #
        selList = om.MSelectionList()
        om.MGlobal.getActiveSelectionList( selList )
        if selList.length():
            setFn = om.MFnSet( setNode )
            setFn.addMembers( selList )
            
        # 将节点的名称作为本命令的返回结果
        depNodeFn = om.MFnDependencyNode( setNode )
        ompx.MPxCommand.setResult( depNodeFn.name() )
        
#===============================================================================
 
# node creator
def nodeCreator():
    return ompx.asMPxPtr( BasicObjectSet() )

# node initializer
def nodeInitializer():
    # nothing to initialize
    # 无须初始化
    pass

# cmd creator
def cmdCreator():
    return ompx.asMPxPtr( BasicObjectSetTest() )

# cmd syntax creator
def cmdSyntaxCreator():
    return om.MSyntax()

# initialize the script plug-in
def initializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject, 'Autodesk', '1.0', 'Any' )
    try:
        mplugin.registerCommand( kCmdName, cmdCreator, cmdSyntaxCreator )
    except:
        sys.stderr.write( "Failed to register command: %s" % kCmdName )
        raise
        
    try:
        mplugin.registerNode( kNodeName, kNodeId, nodeCreator,
                             nodeInitializer, ompx.MPxNode.kObjectSet )
    except:
        sys.stderr.write( "Failed to register node: %s" % kNodeName )
        raise
    
# uninitialize the script plug-in
def uninitializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject )
    try:
        mplugin.deregisterCommand( kCmdName )
    except:
        sys.stderr.write( "Failed to deregister command: %s" % kCmdName )
        raise
    
    try:
        mplugin.deregisterNode( kNodeId )
    except:
        sys.stderr.write( "Failed to deregister node: %s" % kNodeName )
        raise

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

你可能感兴趣的:(html,python)