Extensions:2.4/Py/Nodes/Cookbook/Templates

提供: wiki
< Extensions:2.4‎ | Py‎ | Nodes‎ | Cookbook
移動先: 案内検索

Back to index

Basic template

This is the very basic template you can use as a basis for a script. Please see the API for more information about socket definitions and the functionality overall.

You can get a .blend containing a basic scene in which to develop PyNodes [here]. It contains two screens. Use ctrl-right, ctrl-left or the screen selector to move between them. Other is meant for setting up the node setup and other for altering the scene should it be necessary.

# John Doe 2008
# State license used here and provide a link to it if you want to use one
from Blender import Node

class TemplateNode(Node.Scripted): # change TemplateNode to match the name of your node
    def __init__(self, sockets):
        sockets.input = [Node.Socket('Color', val = 4*[1.0])] # define inputs here
        sockets.output = [Node.Socket('Color', val = 4*[1.0])] # define outputs here

    def __call__(self):
        # this just passes the input color to output using map. alter lambda to
        # provide functionality (hint 1.0 - x for invert)
        self.output.Color = map(lambda x: x, self.input.Color) # do operation here

__node__ = TemplateNode # change TemplateNode to match the name of your node

Basic template - version that will appear amongst templates available at the text editor

Save the script as scripttemplate_pynode.py into your Blender script directory. When in Blender, be sure to update your scripts so that Blender will find it. You can do this by opening Scripts Window and then hitting "Update Menus" in the Scripts menu. An alternative way to do this is to open User Preferences and then go to File Paths and hit the button left to folder icon just next to "Python Scripts:" field.

After you have done either of those, the template should appear amongst the script templates in the text editor window.

#!BPY
"""
Name: 'PyNode'
Blender: 246
Group: 'ScriptTemplate'
Tooltip: 'Create a new PyNode based on template'
"""

from Blender import Window
import bpy

script_data = \
'''
# John Doe 2008
# State license used here and provide a link to it if you want to use one
from Blender import Node

class TemplateNode(Node.Scripted): # change TemplateNode to match the name of your node
    def __init__(self, sockets):
        sockets.input = [Node.Socket('Color', val = 4*[1.0])] # define inputs here
        sockets.output = [Node.Socket('Color', val = 4*[1.0])] # define outputs here

    def __call__(self):
        # this just passes the input color to output using map. alter lambda to
        # provide functionality (hint 1.0 - x for invert)
        self.output.Color = map(lambda x: x, self.input.Color) # do operation here

__node__ = TemplateNode # change TemplateNode to match the name of your node
'''

new_text = bpy.data.texts.new('pynode_template.py')
new_text.write(script_data)
bpy.data.texts.active = new_text
Window.RedrawAll()