Maya Python游戏与影视编程指南阅读笔记——第十章

11 篇文章 2 订阅
一、加载脚本化插件

在插件管理器中,点击Browser,找到要加载的插件,打开之后,该插件就添加到maya中
加载maya已定义插件:
打开maya安装环境下的devkit\plug-ins\scripted\helloWorldCmd.py文件,代码如下

########################################################################
# DESCRIPTION:
#
# Produces the Python command "spHelloWorld".
#
# This is a simple demonstration of how to use a Python plug-in.
#
# To use, make sure that helloWorldCmd.py is in your MAYA_PLUG_IN_PATH (and the
# C++ version is not), and then do the following:
#
#	import maya.cmds
#	maya.cmds.loadPlugin("helloWorldCmd.py")
#	maya.cmds.spHelloWorld()
#
# A "Hello World" text is output to the script editor window.
#
########################################################################

import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx

# command
class HelloWorldCmd(OpenMayaMPx.MPxCommand):
	kPluginCmdName = "spHelloWorld"

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

	@staticmethod
	def cmdCreator():
		return OpenMayaMPx.asMPxPtr( HelloWorldCmd() )

	def doIt(self,argList):
		print "Hello World!"


# Initialize the script plug-in
def initializePlugin(plugin):
	pluginFn = OpenMayaMPx.MFnPlugin(plugin)
	try:
		pluginFn.registerCommand(
			HelloWorldCmd.kPluginCmdName, HelloWorldCmd.cmdCreator
		)
	except:
		sys.stderr.write(
			"Failed to register command: %s\n" % HelloWorldCmd.kPluginCmdName
		)
		raise

# Uninitialize the script plug-in
def uninitializePlugin(plugin):
	pluginFn = OpenMayaMPx.MFnPlugin(plugin)
	try:
		pluginFn.deregisterCommand(HelloWorldCmd.kPluginCmdName)
	except:
		sys.stderr.write(
			"Failed to unregister command: %s\n" % HelloWorldCmd.kPluginCmdName
		)
		raise

#-
# ==========================================================================
# Copyright (C) 2011 Autodesk, Inc. and/or its licensors.  All
# rights reserved.
#
# The coded instructions, statements, computer programs, and/or related
# material (collectively the "Data") in these files contain unpublished
# information proprietary to Autodesk, Inc. ("Autodesk") and/or its
# licensors, which is protected by U.S. and Canadian federal copyright
# law and by international treaties.
#
# The Data is provided for use exclusively by You. You have the right
# to use, modify, and incorporate this Data into other products for
# purposes authorized by the Autodesk software license agreement,
# without fee.
#
# The copyright notices in the Software and this entire statement,
# including the above license grant, this restriction and the
# following disclaimer, must be included in all copies of the
# Software, in whole or in part, and all derivative works of
# the Software, unless such copies or derivative works are solely
# in the form of machine-executable object code generated by a
# source language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
# AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
# WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
# NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
# PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
# TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
# BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
# DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
# AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
# OR PROBABILITY OF SUCH DAMAGES.
#
# ==========================================================================
#+

在代码编辑器输入代码

import maya.cmds as cmds
cmds.spHelloWorld()

运行结果:
在这里插入图片描述

二、脚本化命令剖析

自定义脚本AR_HelloWorldCmd.py

import maya.OpenMaya as om
import maya.OpenMayaMPx as ommpx

class AR_HelloWorldCmd(ommpx.MPxCommand): ## 大部分自定义命令都继承于MPxCommand
    """
    将字符串打印到控制台的命令.
    """
    
    ## 命令的名称
    kPluginCmdName = 'ar_helloWorld'
    
    def __init__(self):
        """
        初始化实例.
        """
        ommpx.MPxCommand.__init__(self) 
    

    def doIt(self, args):   ## 命令引擎调用插件命令时,自动执行该方法,每个命令都必须有该方法
        """
        将字符串打印到控制台.
        """
        print('Hello, world.')
    
    @classmethod
    def cmdCreator(cls):
        """
        返回指向代理对象的指针.
        """
        return ommpx.asMPxPtr(cls())

def initializePlugin(obj):  ## 插件加载到maya中时调用此函数
    """
    初始化插件.
    """
    plugin = ommpx.MFnPlugin(
        obj,
        'Adam Mechtley & Ryan Trowbridge',
        '1.0',
        'Any'
    )
    try:
        plugin.registerCommand(
            AR_HelloWorldCmd.kPluginCmdName,
            AR_HelloWorldCmd.cmdCreator
        )
    except:
        raise Exception(
            'Failed to register command: %s'%
            AR_HelloWorldCmd.kPluginCmdName
        )

def uninitializePlugin(obj):    ## 卸载插件时调用该函数
    """
    取消初始化插件.
    """
    plugin = ommpx.MFnPlugin(obj)
    try:
        plugin.deregisterCommand(AR_HelloWorldCmd.kPluginCmdName)        
    except:
        raise Exception(
            'Failed to unregister command: %s'%
            AR_HelloWorldCmd.kPluginCmdName
        )

加载到插件管理器后,在代码编辑器输入

import maya.cmds as cmds
cmds.ar_helloWorld()

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值