`
schi
  • 浏览: 206659 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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。
在线版
http://download.autodesk.com/us/maya/2010help/API/circle_node_8py-example.html
0
0
分享到:
评论

相关推荐

    科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器

    科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法模拟器.py科学计数法...

    google_auth-1.23.0-py2.py3-none-any.whl____python 包

    google_auth-1.23.0-py2.py3-none-any.whl python 包 google_auth-1.23.0-py2.py3-none-any.whl python 包 google_auth-1.23.0-py2.py3-none-any.whl python 包google_auth-1.23.0-py2.py3-none-any.whl python 包 ...

    python安装模块如何通过setup.py安装(超简单)

    有些时候我们发现一些模块没有提供pip install 命令和安装教程 , 只提供了一个setup.py文件 , 这个时候如何安装呢? 步骤 打开cmd 到达安装目录 python setup.py build python setup.py install 总结 以上所述是...

    vn.py 2.0.7代码分析入门.docx

    vn.py 是一个基于Python语言构建的开源量化交易平台,专为金融市场的量化交易提供服务。它以其机构级的特性,如高效、稳定和可扩展性,成为国内量化交易者的重要工具。vn.py 2.0.7是其最新的版本,本文档旨在引导...

    pyinstxtractor.py 反编译pyinstaller打包的程序

    pyinstxtractor.py 反编译pyinstaller打包的程序 使用方法 python pyinstxtractor.py 示例:python pyinstxtractor.py main.exe

    get-platformio.py

    get-platformio.py get-platformio.py get-platformio.py get-platformio.py

    web.py 中文手册

    web.py 中文手册 webpy coobookweb.py 中文手册 webpy coobookweb.py 中文手册 webpy coobookweb.py 中文手册 webpy coobookweb.py 中文手册 webpy coobookweb.py 中文手册 webpy coobookweb.py 中文手册 webpy ...

    python3.6 get-pip.py

    curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o get-pip.py python get-pip.py

    python2.7中所用的get-pip.py文件+安装方法

    `get-pip.py`是一个Python脚本,它的主要作用是为没有预装`pip`的Python环境安装`pip`。在Python 2.7中,由于某些系统可能没有默认提供`pip`,或者`pip`版本过低,`get-pip.py`就显得非常有用。这个脚本可以下载并...

    web.py中文版用户手册

    web.py 是一个轻量级Python web框架,它简单而且功能强大。web.py是一个开源项目。该框架由美国作家、Reddit联合创始人、RSS规格合作创造者、著名计算机黑客Aaron Swartz开发。web.py目前已被很多家大型网站所使用。

    实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py

    实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数模拟器.py实数...

    有理数模拟器.py有理数模拟器.py

    有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py有理数模拟器.py...

    二叉树模拟器.py二叉树模拟器.py

    二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py二叉树模拟器.py...

    python_docx-0.8.10-py2.py3-none-any.whl

    估计这个安装包还只兼容python 2(python2 和python3差别还是挺大的,虽然现在python 3出来很久了,但是不少三方库还没有更新),因此需要自己找一个兼容的包:python_docx-0.8.6-py2.py3-none-any.whl。然后在...

    Python3 py转exe.py

    py转exe.py py转exe.py py转exe.py py转exe.pypy转exe.py py转exe.py py转exe.py py转exe.pypy转exe.py py转exe.py py转exe.py py转exe.pypy转exe.py py转exe.py py转exe.py py转exe.pypy转exe.py py转exe.py py转...

    PyPI 官网下载 | yolov5-5.0.0-py36.py37.py38-none-any.whl

    资源来自pypi官网。 资源全名:yolov5-5.0.0-py36.py37.py38-none-any.whl

    pip-20.3.4-py2.py3-none-any.whl

    pip-20.3.4-py2.py3-none-any.whl

    绩点计算器.py绩点计算器.py

    绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点计算器.py绩点...

    随机点名器.py随机点名器.py

    随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机点名器.py随机...

    循环队列模拟器.py循环队列模拟器.py

    循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列模拟器.py循环队列...

Global site tag (gtag.js) - Google Analytics