sineNode.py

4451 단어 htmlpython
앞의 hello World Cmd. py 와 helixCmd. py 는 사용자 정의 명령 과 매개 변수 명령 을 가 진 쓰기 방법 을 보 여 주 었 습 니 다. 다음은 사용자 정의 노드 의 쓰기 방법 을 살 펴 보 겠 습 니 다.
이 노드 와 표현 식 의 sin 방법 은 같은 기능 입 니 다.
sin 및 표현 식 사용
http://download.autodesk.com/us/maya/2010help/index.html?url=Useful_functions_sin.htm,topicNumber=d0e175415
sineNode.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# python:
# import maya
# maya.cmds.loadPlugin("sineNode.py")
# maya.cmds.createNode("spSineNode")
# maya.cmds.getAttr("spSineNode1.output")
#
# Mel:
# loadPlugin sineNode;
# createNode spSineNode;
# getAttr spSineNode1.output;

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

#         ID
kPluginNodeTypeName = 'spSineNode'
sineNodeId = om.MTypeId( 0x8700 )

# node definition
#     
class sineNode( ompx.MPxNode ):
    '''
    sine node
    '''

    # class variables
    input = om.MObject()
    output = om.MObject()
    
    def __init__( self ):
        super( sineNode, self ).__init__()
    
    #        
    # plug MPlug   
    # dataBlock MDataBlock   
    def compute( self, plug, dataBlock ):
        #        sineNode.output
        if ( plug == sineNode.output ):
            #        sineNode.input
            #          ,     
            dataHandle = dataBlock.inputValue( sineNode.input )
            
            #             
            #  MDataHandle   
            inputFloat = dataHandle.asFloat()
            #     ,         
            result = math.sin( inputFloat ) * 10.0
            #  sineNode.output       ,      
            outputHandle = dataBlock.outputValue( sineNode.output )
            #  result      
            outputHandle.setFloat( result )
            #     
            dataBlock.setClean( plug )
            
# creator
#     
def nodeCreator():
    return ompx.asMPxPtr( sineNode() )

# initializer
#      
def nodeInitializer():
    # input
    #   
    nAttr = om.MFnNumericAttribute()
    #   input  
    sineNode.input = nAttr.create( 'input', 'in',
                                  om.MFnNumericData.kFloat, 0.0 )
    nAttr.setStorable( 1 )
    
    # output
    #   
    nAttr = om.MFnNumericAttribute()
    #   output  
    sineNode.output = nAttr.create( 'output', 'out',
                                   om.MFnNumericData.kFloat, 0.0 )
    nAttr.setStorable( 1 )
    nAttr.setWritable( 1 )
    # add attributes
    #            
    sineNode.addAttribute( sineNode.input )
    sineNode.addAttribute( sineNode.output )
    # input     output  
    sineNode.attributeAffects( sineNode.input, sineNode.output )
    
# initialize the script plug-in
def initializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject )
    try:
        mplugin.registerNode( kPluginNodeTypeName,
                             sineNodeId,
                             nodeCreator,
                             nodeInitializer )
    except :
        sys.stderr.write( "Failed to register node: %s" % 
                         kPluginNodeTypeName )
        raise
# uninitialize the script plug-in
def uninitializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject )
    try:
        mplugin.deregisterNode( sineNodeId )
    except :
        sys.stderr.write( 'Failed to register node: %s' % 
                         kPluginNodeTypeName )
        raise

maya 설치 디 렉 터 리 에 있 는 devkit / plug - ins / scripted 에서 sinenode. py 를 찾 을 수 있 습 니 다.
온라인 판
http://download.autodesk.com/us/maya/2010help/API/sine_node_8py-example.html

좋은 웹페이지 즐겨찾기