Extensions:2.4/Py/Nodes/Cookbook/Color

提供: wiki
移動先: 案内検索

Back to index

Ideas

  • Add your idea here.

Recipes

Average

Example output of average node.[.blend]

This script just produces an average of provided colors. Note that you can tweak the amount of inputs by just changing the value of AMOUNT_OF_INPUTS.

from Blender import Node

AMOUNT_OF_INPUTS = 4 # when tweaking, make sure this is >=1

class AverageNode(Node.Scripted):
    def __init__(self, sockets):
        cols = []
        
        for i in range(1, AMOUNT_OF_INPUTS+1):
            col_name = 'col' + str(i)
            col = Node.Socket(col_name, val=4*[1.0])
            cols.append(col)
        
        sockets.input = cols
        sockets.output = [cols[0],]

    def __call__(self):
        rgba = 4*[0.0]
        
        for i, color in enumerate(rgba):
            sum = 0.0
            
            for j in range(AMOUNT_OF_INPUTS):
                sum += self.input[j][i]
            
            rgba[i] = sum / AMOUNT_OF_INPUTS
        
        self.output.col1 = rgba

__node__ = AverageNode

Boolean

Example output of boolean node.[.blend]

This script provides various boolean operations. The operations provided are AND, OR and XOR (exclusive OR). Use mode input to define which operation to use. Operation value mappings can be seen in the beginning of the script.

from Blender import Node

AND = 0
OR = 1
XOR = 2

def avg(*args):
    amount = 0
    sum = 0.0
    list_len = len(args[0]) # should get min of lists here
    
    for i in range(len(args)):
        for j in range(list_len):
            sum += args[i][j]
        amount += list_len
    
    if amount:
        return sum / amount

class BooleanNode(Node.Scripted):
    def __init__(self, sockets):
        mode = Node.Socket('Mode', val=0.0, min=0.0, max=2.0)
        threshold = Node.Socket('Threshold', val=0.5, min=0.0, max=1.0)
        col1 = Node.Socket('Color1', val = 4*[1.0])
        col2 = Node.Socket('Color2', val = 4*[1.0])
        
        col = Node.Socket('Color', val = 4*[1.0])
        
        sockets.input = [mode, threshold, col1, col2]
        sockets.output = [col]

    def __call__(self):
        mode = int(self.input.Mode + .5)
        threshold = self.input.Threshold
        color1, color2 = self.input.Color1, self.input.Color2
        color1_ok, color2_ok = False, False
        set_output = False
        
        if avg(color1) > threshold:
            color1_ok = True
        
        if avg(color2) > threshold:
            color2_ok = True
        
        if mode == AND:
            if color1_ok and color2_ok:
                set_output = True
        elif mode == OR:
            if color1_ok or color2_ok:
                set_output = True
        elif mode == XOR:
            if color1_ok ^ color2_ok:
                set_output = True
        
        color = 4 * [0.0]
        if set_output:
            for i in range(4):
                color[i] = (color1[i] + color2[i]) / 2
        
        self.output.Color = color

__node__ = BooleanNode


Color randomizer

Example output of randomizer node.[.blend]

This script just randomizes the color. This is practically the same script as the one seen in the API.

from Blender import Node
from Blender.Noise import random

class RandomNode(Node.Scripted):
    def __init__(self, sockets):
        col = Node.Socket('Color', val = 4*[1.0])
        sockets.input = [col]
        sockets.output = [col]

    def __call__(self):
        self.output.Color = map(lambda x: x * random(), self.input.Color)

__node__ = RandomNode

Eight parts

Example output of eight parts node.[.blend]

This script paints the eight segments of vector space in given colors.

from Blender import Node

class EightPartNode(Node.Scripted):
    def __init__(self, sockets):
        vector = Node.Socket('Vector', val = 3*[1.0])
        colors = []
        
        for i in range(1, 9):
            colors.append(Node.Socket('col' + str(i), val = 4*[1.0]))
        
        sockets.input = [vector] + colors
        sockets.output = [Node.Socket('col', val = 4*[1.0])]

    def __call__(self):
        tex_coords = self.input.Vector
        
        if tex_coords[0] > 0.0:
            if tex_coords[1] > 0.0:
                if tex_coords[2] > 0.0:
                    self.output.col = self.input.col1
                else:
                    self.output.col = self.input.col2
            else:
                if tex_coords[2] > 0.0:
                    self.output.col = self.input.col3
                else:
                    self.output.col = self.input.col4
        else:
            if tex_coords[1] > 0.0:
                if tex_coords[2] > 0.0:
                    self.output.col = self.input.col5
                else:
                    self.output.col = self.input.col6
            else:
                if tex_coords[2] > 0.0:
                    self.output.col = self.input.col7
                else:
                    self.output.col = self.input.col8

__node__ = EightPartNode

Invert color

Example output of invert color node.[.blend]

This script inverts the given color. Basically this is just a reimplementation of the invert node provided with Blender.

from Blender import Node

class InvertNode(Node.Scripted):
    def __init__(self, sockets):
        sockets.input = [Node.Socket('Color', val = 4*[1.0])]
        sockets.output = [Node.Socket('Color', val = 4*[1.0])]

    def __call__(self):
        self.output.Color = map(lambda x: 1.0 - x, self.input.Color)

__node__ = InvertNode