Hi dear contributors and devs
This weekend I’ve created a Math Expression node that dynamically adds sockets on the fly, and I would like some testers.
It’s all implemented in Python using the GeometryCustomNode
type. (See this archived topic if you’re interested in how I managed to do this.)
How It Works
It relies on Python’s exec()
. The math expression string is sanitized and then transformed using the ast
module into functions that automatically arrange a node tree (similar to @Wannes’ plugin, but integrated directly within a node).
The project is part of the free ‘NodeBooster’ addon, which includes other nodes (such as a Python evaluator node) along with additional features.
Download it on Github:
Documentation
With this node you’ll be able to evaluate a math expression using various math nodes. The variables will be automatically created when you confirm the expression.
About:
- The following operators are supported:
+
,-
,*
,/
,%
,//
,**
, and the exponentiation notation using²
.- Many functions are recognized, such as
sin(a)
,nroot(a,n)
,lerp(fac,a,b)
, and many others. Please see the full list in your node editor under N Panel > Node Booster > Active Node > Glossary for function names, their arguments, and descriptions.- Macros for irrational numbers are available. The macros
Pi
,eNum
, andGold
can be automatically assigned to their corresponding Unicode symbols (π
,𝑒
, andφ
), which will be recognized and used. Make sure to press theπ
button to activate these macros.- Algebraic notations are supported (also known as implicit multiplication). You can work with expressions such as
2ab(4ac+π)²
, where variable names are split into individual characters (e.g.,abc
is interpreted asa*b*c
). To activate this feature, make sure theab
button is toggled ON!
- Note that function names will still be recognized and prioritized. For example, if you use
sin
, the interpreter will treat the sequence as a function rather than as individual variables.- If you use this algebraic notation mode, ensure there is no ambiguity in the order of operations. For instance,
1/2ab
does not yield the same result as1/(2ab)
.- If anything goes wrong, you will be notified with an error message.
- In the N Panel > Node Booster > Active Node, you can bake the Plugin Node into a Blender Node Group to share with third parties who do not have the tool.
- Debug: You can see how the node operates behind the scenes. Your expression is first sanitized and then transformed into a function expression, which is executed by the plugin to create a hidden node tree. Go to N Panel > Node Booster > Active Node > Development to view these debug expressions.
- Pro Tip: You will also see a ‘NodeTree’ template beneath these debug strings. You can drag and drop the NodeGroup icon (located to the left of the template) to add the hidden node tree to your tree. This allows you to observe what the plugin is doing behind the scenes.
Limitations:
- Currently, only Geometry Node Tree types are supported.
Possibilities for Improvement:
- This node has a lot of potential. We could implement dynamic
Int
orBool
types and evaluate comparison operations easily. More functions, such asispair(a)
orisneg(a)
, could be added (although adding these functionalities might prevent the node from being ported to the compositor and shader editor).- This node could evolve into a more sophisticated Advanced Expressions system, allowing the creation of multi-type variables (e.g.,
Int
,Bool
,Vector
,Quaternion
, andMatrix
) to perform more advanced 3D math using vector or matrix notations and functions. The socket type could dynamically swap, and we could even write simple VEX-like code similar to Houdini. If you’d like to see such a project come to life, please consider a donation.
I plan to propose this node as an extension, but if Blender developers decide to integrate Python backend nodes as a fully shipped feature with Blender, I could contribute to that project.
Cheers
-Dorian