Object Classes For Custom Python Functions

kyllobrooks's Avatar


25 Nov, 2023 06:58 PM


Where can I get detailed information on how to properly form return types when using customer python nodes. For example, the following function doesn't recognize the Color() object constructor. "global name 'Color' is not defined".

def mix_rgb(color1, color2):
    Mixes two NodeBox RGB color objects in a subtractive way and returns the resulting color as a new NodeBox color object.
    color1 (Color): The first NodeBox color object.
    color2 (Color): The second NodeBox color object.
    Color: The resulting NodeBox color object after subtractive mixing.
    # Perform subtractive mixing
    # For subtractive color mixing, we subtract the second color from the first,
    # ensuring that we don't go below zero
    mixed_r = max(0, 255 - ((255 - color1.r*255) + (255 - color2.r*255)))
    mixed_g = max(0, 255 - ((255 - color1.g*255) + (255 - color2.g*255)))
    mixed_b = max(0, 255 - ((255 - color1.b*255) + (255 - color2.b*255)))
    # Normalize the values to 0-1 range expected by NodeBox
    mixed_r /= 255.0
    mixed_g /= 255.0
    mixed_b /= 255.0
    # Create and return the new blended color
    return Color(mixed_r, mixed_g, mixed_b)

  1. Support Staff 1 Posted by john on 26 Nov, 2023 07:08 AM

    john's Avatar


    Sadly, there are NO docs on on how to properly form return types when using customer python nodes within NodeBox. It's a dark art.

    I have written about a dozen python modules (available in my Cartan Node Library). Each one was made - with great pain - by sheer trial and error. Over the years I have gained enough knowledge to maybe figure out how to fix your Python code - which I will try to do soon.

    But before I do, did you consider making your negative color blend function entirely within Nodebox without the need to call any eternal libraries?

    It's fairly easy to do. You can make a subnetwork that takes two color values. For each one you can then use the lookup node to get r, g, and b (red, green, and blue between 0 and 1) for each. You can then do your math with add, subtract, and multiply nodes, and pass the final result into an rgb_color node to return the blended color.

    This does involve a bit of careful wiring but you avoid the need to muck about with external libraries.

    IN FACT, I have already done this and was planning on releasing it soon. I am currently working on expanding my blend node so that it offers a number of different blending methods, including negative mixing (see screenshot).

    BUT I don't want to share my blend node just yet, because I am still working on it and I still have a number of open questions to nail down.

    For example, your algorithm does not consider transparency (the alpha value); adding alpha values complicates things a bit but seems pretty important. Some formulas also include an option for gamma correction that tends to produce better results on computer screens. There seem to be a number of different methods or modes for mixing colors, each useful in different contexts, with some disagreement among experts as to how many methods there really are and the best algorithm for each.

    I am also weighing the difference between "blending" and "mixing". I already have an inter_color node for mixing based on a T value - useful for gradients or animations - in addition to my blend node which returns a single color. Since I am now adding methods to my blend node I wonder if I should continue having two separate nodes or merge them into a single node. Still pondering that one.

    I do hope to have something fairly soon (within a few days).

    So please let me know if you:

    A) still want help with your Python code,
    B) are willing to try making your function entirely in Nodebox, OR
    C) prefer to hang tight for a few days until I release my improved blend node



  2. 2 Posted by kyllobrooks on 26 Nov, 2023 06:15 PM

    kyllobrooks's Avatar


    I appreciate your time. Thank you. The function is really just an example for my problem, I would like to know how to return geometry, points, colors, floats, integers, strings, and all the other object primitives built into the tool.

    I wonder if I am simply missing an include or something similar that allows for the built in constructors to be called? I am frankly baffled that this documentation isn't around and if I figure this out i will be happy to share my own documentation.

    I found this thread that seems to hint at an answer for color:


    If "nodebox.graphics.Color" class is a Python class loaded by default, how do I call its constructor, and where are can I look at this class definition so i can do this properly?

  3. 3 Posted by kyllobrooks on 26 Nov, 2023 11:01 PM

    kyllobrooks's Avatar

    Thanks for the reply,

    I was able to solve my problem through trial and error. I needed to import the nodebox.graphics.Color class in order to use the constructor. I would like to see some detailed documentation on these classes somewhere.

    Which official repo do these classes live in? I would like to know so i can do my own documentation.

  4. Support Staff 4 Posted by john on 27 Nov, 2023 04:40 AM

    john's Avatar

    Good for you, Kyllo. That's what I did in my image.py code.

    The official NodeBox repo is here:


    The graphics subdirectory is here:


    If you could manage to document some of this stuff and share it here on the forum, we might just have to have a parade in your honor!


  5. Support Staff 5 Posted by john on 31 Dec, 2023 04:22 PM

    john's Avatar


    Still there? FYI I finally published a blend_color node with a subtractive color mode. Check it out:


    I'm also curious if you made any more progress making Python modules that accept and return Nodebox objects.


Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Already uploaded files

  • colours.py 1.08 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

03 Mar, 2024 04:34 AM
23 Feb, 2024 07:39 PM
23 Feb, 2024 10:02 AM
22 Feb, 2024 05:51 AM
21 Feb, 2024 05:27 PM


21 Feb, 2024 04:49 PM
16 Feb, 2024 05:24 PM
06 Feb, 2024 04:45 AM
19 Jan, 2024 09:50 AM
13 Jan, 2024 03:47 AM
05 Jan, 2024 05:10 PM