练习maya python API jointChain中文标注

# jointChain.py

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

kPluginCmdName = 'myJointChain'

# The length of the chain.
kLengthFlag = '-l'
kLengthLongFlag = '-length'
defaultLength = 3

jointDistance = 5     # the distance between two joints
jointOrientation = 20 # degrees.

##########################################################
# Plug-in 
##########################################################
class JointChainCommand(OpenMayaMPx.MPxCommand):
    
    
    def __init__(self):
        ''' Constructor. '''
        OpenMayaMPx.MPxCommand.__init__(self)
    
    
    def parseArgs(self, pArguments):
        ''' Parses the command's arguments. '''#解析命令的参数
        
        # Set the default chain length in case there are no arguments.如果没有设置参数则设定为默认值
        global defaultLength
        self.length = defaultLength
        
        # Obtain the flag value, if the flag is set.如果标示被设置则获得标示的数值
        argData = OpenMaya.MArgParser( self.syntax(), pArguments )
        if argData.isFlagSet( kLengthFlag ):
            
            # Get the value associated with the flag as an integer.得到的值与标示作为一个整型的关联
            flagValue = argData.flagArgumentInt( kLengthFlag, 0 )
            
            # Make sure this value is larger than the default length.确保该数值大于默认数值
            if flagValue > defaultLength:
                self.length = flagValue
    
    
    def doIt(self, pArguments):
        ''' Command Execution. '''#执行命令
        
        # Parse the passed arguments.解析传递参数。
        self.parseArgs( pArguments )
        
        # Create an instance of an MDagModifier to keep track of the created objects,创建一个MDagModifier的实例来对创建的物体进行跟踪
        # and to undo their creation in our undoIt() function.至于撤销操作是在undoIt()的功能下
        self.dagModifier = OpenMaya.MDagModifier()
        
        # Create the joint MObjects we will be manipulating.我们创建将要操作joint MObjects
        self.jointObjects = []
        for i in range(0, self.length):
            if i == 0:
                # The first joint has no parent.第一个骨节没有上层骨节
                newJointObj = self.dagModifier.createNode( 'joint' )
            else:
                # Assign the new joint as a child to the previous joint.分配新的骨节作为子骨子给之前的骨节
                newJointObj = self.dagModifier.createNode( 'joint', self.jointObjects[i-1] )
            # Keep track of all the joints created.继续创建所有的骨节
            self.jointObjects.append( newJointObj )
        
        # Create the inverse kinematic effector MObject. The effector is a child of the last joint object.创建IKMobject,IK是最后骨节的子物体
        # The [-1] index is a Python-specific way of referring to the last item in a list.
        self.effectorObj = self.dagModifier.createNode( 'ikEffector', self.jointObjects[-1] )
        
        # Invoke the command's redoIt() function to actually create and manipulate these objects.调用命令的redoIt()函数创建和操纵这些对象。
        self.redoIt()
        
    
    def redoIt(self):
        ''' Create and manipulate the nodes to form the joint chain. '''#创建和形成骨节链
        
        # Perform the operations enqueued within our reference to MDagModifier.在MDagModifier内执行排列操作
        self.dagModifier.doIt()
        
        #=======================================
        # JOINT MANIPULATION#骨节控制
        #=======================================
        # We can now use the function sets on the newly created DAG objects.现在我们可以使用新创建的DAG对象的功能集
        jointFn = OpenMayaAnim.MFnIkJoint()
        
        for i in range( 1, len( self.jointObjects ) ):

            jointFn.setObject( self.jointObjects[i] )
            # We set the orientation for our joint to be 'jointOrientation' degrees, to form an arc. 我们为骨节设置方向(定义的变量jointOrientation这么多)度,形成弧形
            # We use MFnIkJoint.setOrientation() instead of MFnTransform.setRotation() to let the
            # inverse-kinematic handle maintain the curvature.我们使用MFnIkJoint.setOrientation()来取代MFnTransform.setRotation()来让IK手柄保持曲率
            global jointOrientation
            rotationAngle = OpenMaya.MAngle( jointOrientation, OpenMaya.MAngle.kDegrees )
            jointFn.setOrientation( OpenMaya.MEulerRotation( rotationAngle.asRadians(), 0 , 0, OpenMaya.MEulerRotation.kXYZ ) )
            
            # We translate the joint by 'jointDistance' units along its parent's y axis.我们沿着父骨节的Y轴移动jointDistance(这么多)数值
            global jointDistance
            translationVector = OpenMaya.MVector( 0, jointDistance, 0 )
            jointFn.setTranslation( translationVector, OpenMaya.MSpace.kTransform )

        #=======================================
        # IK HANDLE MANIPULATION#IK手柄的控制
        #=======================================
        # We will use the MEL command 'ikHandle' to create the handle which will move our joint chain. This command
        # will be enqueued in our reference to the MDagModifier so that it can be undone in our call to MDagModifier.undoIt().
        #我要使用mel中的 'ikHandle' 命令来创建可以控制骨节链的手柄,该命令将被排入我们参考MDagModifier,以便它可以在我们的呼叫后对MDagModifier.undoIt()被撤消。
        
        # Obtain the DAG path of the first joint.设置第一个骨节的DAG路径
        startJointDagPath = OpenMaya.MDagPath()
        jointFn.setObject( self.jointObjects[0] )
        jointFn.getPath( startJointDagPath )
             
        # Obtain the DAG path of the effector.设置操纵器的DAG路径
        effectorDagPath = OpenMaya.MDagPath()
        effectorFn = OpenMayaAnim.MFnIkEffector( self.effectorObj )
        effectorFn.getPath( effectorDagPath )
        
        # Enqueue the following MEL command with the DAG paths of the start joint and the end effector.
        self.dagModifier.commandToExecute( 'ikHandle -sj ' + startJointDagPath.fullPathName() + ' -ee ' + effectorDagPath.fullPathName() )
        
        # We call MDagModifier.doIt() to effectively execute the MEL command and create the ikHandle. 我们调用MDagModifier.doIt()有效地执行MEL命令创建ikHandle
        self.dagModifier.doIt()#《《《《《《《《《《《《《《《《《《《《《事实上是 OpenMaya.MDagModifier.doIt()
        
    
    def undoIt(self):
        ''' Undo the command. '''
        # This call to MDagModifier.undoIt() undoes all the operations within the MDagModifier.这个调用MDagModifier.undoIt()撤消所有在MDagModifier内的操作
        # Observe that the number of calls to MDagModifier.undoIt() does not need to match the number of calls to MDagModifier.doIt().
        #观察呼叫MDagModifier.undoIt()的数目不需要匹配MDagModifier.doIt()的呼叫数目
        self.dagModifier.undoIt()#《《《《《《《《《《《《《《《《《《《《《事实上是 OpenMaya.MDagModifier.undoIt()
        
        
    def isUndoable(self):
        ''' This function must return True to indicate that it is undoable. ''' #这个函数必须返回true以指示它是不可撤销的 
        return True
        

##########################################################
# Plug-in initialization.#插件的安装
##########################################################
def cmdCreator():
    ''' Creates an instance of the command. '''#注册这个命令
    return OpenMayaMPx.asMPxPtr( JointChainCommand() )

def syntaxCreator():
    ''' Defines the argument and flag syntax for this command. '''#注册这个命令的参数以及标示
    syntax = OpenMaya.MSyntax()
    syntax.addFlag( kLengthFlag, kLengthLongFlag, OpenMaya.MSyntax.kDouble )
    return syntax

def initializePlugin( mobject ):
    ''' Initializes the plug-in. '''#安装插件
    mplugin = OpenMayaMPx.MFnPlugin( mobject )
    try:
        mplugin.registerCommand( kPluginCmdName, cmdCreator, syntaxCreator )
    except:
        sys.stderr.write( 'Failed to register command: ' + kPluginCmdName )
        raise
    
def uninitializePlugin( mobject ):
    ''' Uninitializes the plug-in. '''#卸载插件
    mplugin = OpenMayaMPx.MFnPlugin( mobject )
    try:
        mplugin.deregisterCommand( kPluginCmdName )
    except:
        sys.stderr.write( 'Failed to unregister command: ' + kPluginCmdName )
        raise


##########################################################
# Sample usage.
##########################################################
''' 
# Copy the following lines and run them in Maya's Python Script Editor:

import maya.cmds as cmds
cmds.loadPlugin( 'jointChain.py' )
cmds.myJointChain( length=4 )

'''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值