EDIT 2: I talked to Hans Goudey and he told me that they’re not planning to make a script node for the time being. That means that this idea will not make to the GSoC. Thank you all for your interaction!
EDIT: Thank you all for the support. I’ll refine and correct some errors in my description so becomes clearer what I’m thinking about. Please read it again if you already have read it.
Introduction
I’m getting started in the Blender development community and intend to participate in this year’s Google Summer of Code.
I’m a Computer Science undergraduate from Brazil and I’ve been using blender since 2014. I already started to explore the codebase and also submitted a patch to this task: T46809
Since I’m still getting to know the codebase and and this community, my idea for a project might be redundant or unfeasible, so any feedback is more than welcome!
Project Idea
Overview:
Create a “Script Node” in Geometry Nodes and implement the necessary interface to enable running Python scripts with data from other nodes.
Node structure:
This node’s initial state would consist in only a field to select a script, similar to the Script Node from Shaders, with no input nor output sockets.
The sockets would appear after a valid Python script is selected. Their names and types would be defined inside the script.
Script structure:
The script should specify what are its expected inputs and what are its outputs. I’m still not sure about how to implement that, maybe I can use class inheritance and reflection.
Either way, the presence of these input and output descriptors would determine if the script is valid for the Script Node or not.
There would be an API that allows manipulating the data received as input. For example, there would be methods to transform geometry, create new vertices, etc.
Example:
The following script is an example of how this could work. To be valid, the script must have declared a class that inherits from a base class and has some pre-defined methods implemented:
# Made up module and types
from GeoNode import BaseClass, GeometryType
import math
# Set the instances in a Fibonacci Spiral
class FibonacciSpiralInstances(BaseClass):
def __init__(self):
self.geometry: GeometryType = None
self.angle: float = 0
# override BaseClass method
def getInputTypes(self)-> dict:
return { "Instances": GeometryType, "Angle": float}
# override BaseClass method
def getOutputTypes(self)-> dict:
return { "Instances": GeometryType}
# override BaseClass method
def setInput(self, nodeInput: dict):
self.geometry = nodeInput["Instances"]
self.angle = nodeInput["Angle"]
# override BaseClass method
def execute(self):
idx = 0
if(self.geometry is not None):
for instance in self.geometry.instaces:
fib = self.fibonacci(idx)
xcoord = math.cos(self.angle * idx) * fib
ycoord = math.sin(self.angle * idx) * fib
# set instance position in local space
instance.setPosition((xcoord,ycoord,0), local=True)
# override BaseClass method
def getOutput(self):
return {"Instances": self.geometry}
# user defined method
def fibonacci(n: int):
a, b = 0, 1
for i in range(0, n):
a, b = b, a + b
return a
Benefits:
This node would allow much more flexibility in creating procedural geometry. Although many things that could be done in the script already are achievable through existing nodes alone, the possibility to perform these operations repeatedly ou recursively opens the door for more creative power.
I know that the idea of allowing loops in nodes has already been discussed somewhere in this forum, and I remember that allowing such a thing would imply in a lot of complications about resolving circular node dependencies. The Script Node would allow repetition and recursion without introducing these problems to the node processing engine.
Challenges and limitations:
Two challenges that I can think of are: performance and how to expose the data and methods to the script.
I still don’t know if the nodes are written in C/C++ or Python. If it’s the former, copying data to the Python environment and running the script could be a performance bottleneck. Maybe it’s something that the user would’ve to be aware of.
About exposing data and methods from nodes in Python, if there’s already an API for that, that’s great! If not, implementing one can be an enormous task, maybe beyond the scope of GSoC.