circleNode.py

自定义节点

使用方法
在脚本编辑器中的python面板执行circleNodeTest.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Description:
# This script creates a new top level Maya menu that contains a
# single item "Move in Circle". When selected, it will create
# a sphere and a dependency node that moves this in a circle,
# and connect these 2 together.
#
# When the play button on the time slider is pressed, the sphere
# will move in a cirle around the Y axis.
#
# Procedures:
# circleMenu, createSphereAndAttachCircleNode
#

import maya.cmds as cmds

# Callback routine for the menu item created in CircleMenu
# 菜单命令
def createSphereAndAttachCircleNode( *args ):
# Create a circle node dependency object called "circleNode1"
# 创建一个circleNode
cmds.createNode( "spCircle", name="circleNode1" )

# Create a sphere called "sphere1"
# 创建一个球
cmds.sphere( name="sphere1", radius=1 )

# Connect the sine output attribute of "circleNode1"
# to the X position of "sphere1"
# 将circleNode的".sineOutput"属性连接到球的".translateX"
cmds.connectAttr( "circleNode1.sineOutput", "sphere1.translateX" )

# Connect the cosine output attribute of "circleNode1"
# to the Z position of "sphere1"
# 将circleNode的".cosineOutput"属性连接到球的".translateZ"
cmds.connectAttr( "circleNode1.cosineOutput", "sphere1.translateZ" )

# Connect the output of the time slider, "time1", in frames
# to the input of "circleNode1".
# 将time1的".outTime"属性连接到circleNode的".input"
cmds.connectAttr( "time1.outTime", "circleNode1.input" )

# "circleNode1" will now compute the X and Z positions of "sphere1"
# as a function of the frame in the current animation.

def circleMenu():
# The global string variable gMainWindow contains the name of top
# Level Maya window. Using this as the parent in a menu command
# will create a new menu at the same level as "File", "Edit", etc.
# 全局变量gMainWindow是Maya的顶级窗口
mainWindow = maya.mel.eval( "global string $gMainWindow;$temp = $gMainWindow" )

# Create a top level menu called "Circle". Its only menu item
# is called "Move in circle", and when invoked by the user, it
# will call the createSphereAndAttachCircleNode procedure shown above.
# 添加circleMenu菜单,并有一个Move in circle菜单项目
cmds.menu( "circleMenu", parent=mainWindow, tearOff=True, label="Circle" )
cmds.menuItem( "circleMenuItem1", label="Move in circle", command=createSphereAndAttachCircleNode )

# Run circleMenu to add "Circle" to the top level Maya menu list.
circleMenu()

这个脚本会在maya主菜单上创建一个新菜单,点击该菜单下的项目就会有一个使用circleNode的简单例子。可以通过调整circleNode的属性来观察这个节点的功能。

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

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

kPluginNodeTypeName = "spCircle"
kPluginNodeId = om.MTypeId( 0x87005 )

# Node definition
# 定义节点
class circle( ompx.MPxNode ):
# class variables
aInput = om.MObject()
aScale = om.MObject()
aFrames = om.MObject()
aSOutput = om.MObject()
aCOutput = om.MObject()

def __init__( self ):
super( circle, self ).__init__()

def compute( self, plug, data ):
# Check that the requested recompute is one of the output values
# 检查需要更新的属性
if plug == circle.aSOutput or plug == circle.aCOutput:
# Read the input values
# 读取输入值
inputData = data.inputValue( circle.aInput )
scaleData = data.inputValue( circle.aScale )
framesData = data.inputValue( circle.aFrames )

# Compute the output values
# 计算输出值
currentFrame = inputData.asFloat()
scaleFactor = scaleData.asFloat()
framesPerCircle = framesData.asFloat()
angle = 6.2831853 * ( currentFrame / framesPerCircle )
sinResult = math.sin( angle ) * scaleFactor
cosResult = math.cos( angle ) * scaleFactor

# Store them on the output plugs
# 储存结果到output
sinHandle = data.outputValue( circle.aSOutput )
cosHandle = data.outputValue( circle.aCOutput )
sinHandle.setFloat( sinResult )
cosHandle.setFloat( cosResult )
data.setClean( plug )
else:
return om.MStatus.kUnknownParameter
return om.MStatus.kSuccess

# creator
def nodeCreator():
return ompx.asMPxPtr( circle )

# initializer
def nodeInitializer():
nAttr = om.MFnNumericAttribute()

# Setup the input attributes
# 创建输入属性
circle.aInput = nAttr.create( 'input', 'in',
om.MFnNumericData.kFloat,
0.0 )
nAttr.setStorable( True )

circle.aScale = nAttr.create( 'scale', 'sc',
om.MFnNumericData.kFloat,
10.0 )
nAttr.setStorable( True )

circle.aFrames = nAttr.create( 'frames', 'fr',
om.MFnNumericData.kFloat,
48.0 )
nAttr.setStorable( True )

# Setup the output attributes
# 创建输出属性
circle.aSOutput = nAttr.create( 'sineOutput', 'so',
om.MFnNumericData.kFloat,
0.0 )
nAttr.setWritable( False )
nAttr.setStorable( False )

circle.aCOutput = nAttr.create( 'cosineOutput', 'co',
om.MFnNumericData.kFloat,
0.0 )
nAttr.setWritable( False )
nAttr.setStorable( False )

# Add the attributes to the node
# 添加属性到节点
circle.addAttribute( circle.aInput )
circle.addAttribute( circle.aScale )
circle.addAttribute( circle.aFrames )
circle.addAttribute( circle.aSOutput )
circle.addAttribute( circle.aCOutput )

# Set the attribute dependencies
# 设置属性影响更新
circle.attributeAffects( circle.aInput, circle.aSOutput )
circle.attributeAffects( circle.aInput, circle.aCOutput )
circle.attributeAffects( circle.aScale, circle.aSOutput )
circle.attributeAffects( circle.aScale, circle.aCOutput )
circle.attributeAffects( circle.aFrames, circle.aSOutput )
circle.attributeAffects( circle.aFrames, circle.aCOutput )

# initialize the script plug-in
def initializePlugin( mobject ):
mplugin = ompx.MFnPlugin( mobject, "Autodesk", "1.0", "Any" )
try:
mplugin.registerNode( kPluginNodeTypeName, kPluginNodeId, 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( kPluginNodeId )
except:
sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
raise

你可以在maya安装目录下的devkit/plug-ins/scripted找到circleNode.py。
在线版
[url]http://download.autodesk.com/us/maya/2010help/API/circle_node_8py-example.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值