nodebpy 0.7.1__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {nodebpy-0.7.1 → nodebpy-0.8.0}/PKG-INFO +24 -22
- {nodebpy-0.7.1 → nodebpy-0.8.0}/README.md +20 -20
- {nodebpy-0.7.1 → nodebpy-0.8.0}/pyproject.toml +6 -4
- nodebpy-0.8.0/src/nodebpy/arrange.py +336 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/builder.py +83 -38
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/types.py +2 -1
- nodebpy-0.7.1/src/nodebpy/arrange.py +0 -365
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/__init__.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/__init__.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/graph.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/ordering.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/ranking.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/realize.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/stacking.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/structs.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/sugiyama.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/x_coords.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/arrange/y_coords.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/config.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/lib/nodearrange/utils.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/__init__.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/__init__.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/color.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/converter.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/distort.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/filter.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/group.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/input.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/interface.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/manual.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/matte.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/output.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/texture.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/compositor/vector.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/__init__.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/attribute.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/color.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/converter.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/geometry.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/grid.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/group.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/input.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/interface.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/manual.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/output.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/texture.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/vector.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/geometry/zone.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/__init__.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/color.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/converter.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/grid.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/group.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/input.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/interface.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/manual.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/output.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/script.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/shader.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/texture.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/vector.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/nodes/shader/zone.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/screenshot.py +0 -0
- {nodebpy-0.7.1 → nodebpy-0.8.0}/src/nodebpy/sockets.py +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: nodebpy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: Build nodes trees in Blender more elegantly with code
|
|
5
5
|
Author: Brady Johnston
|
|
6
6
|
Author-email: Brady Johnston <brady.johnston@me.com>
|
|
7
|
-
Requires-Dist: networkx>=3.6.1
|
|
8
7
|
Requires-Dist: bpy>=5.0.1 ; extra == 'bpy'
|
|
9
8
|
Requires-Dist: fake-bpy-module>=20260113 ; extra == 'dev'
|
|
10
9
|
Requires-Dist: jsondiff>=2.2.1 ; extra == 'dev'
|
|
10
|
+
Requires-Dist: networkx>=3.6.1 ; extra == 'dev'
|
|
11
11
|
Requires-Dist: pytest>=9.0.2 ; extra == 'dev'
|
|
12
12
|
Requires-Dist: pytest-cov>=7.0.0 ; extra == 'dev'
|
|
13
13
|
Requires-Dist: quarto-cli>=1.8.26 ; extra == 'dev'
|
|
@@ -16,10 +16,12 @@ Requires-Dist: ruff>=0.14.11 ; extra == 'dev'
|
|
|
16
16
|
Requires-Dist: syrupy>=5.0.0 ; extra == 'dev'
|
|
17
17
|
Requires-Dist: tree-clipper>=0.1.1 ; extra == 'dev'
|
|
18
18
|
Requires-Dist: ipython>=8.0.0 ; extra == 'jupyter'
|
|
19
|
+
Requires-Dist: networkx>=3.6.1 ; extra == 'networkx'
|
|
19
20
|
Requires-Python: >=3.11
|
|
20
21
|
Provides-Extra: bpy
|
|
21
22
|
Provides-Extra: dev
|
|
22
23
|
Provides-Extra: jupyter
|
|
24
|
+
Provides-Extra: networkx
|
|
23
25
|
Description-Content-Type: text/markdown
|
|
24
26
|
|
|
25
27
|
|
|
@@ -57,9 +59,9 @@ In `nodebpy` we use the `>>` operator to link from one node or socket into anoth
|
|
|
57
59
|
This should feel and behave much like the <kbd>Alt</kbd> + <kbd>Right Click</kbd> drag between nodes in [Node Wrangler](https://docs.blender.org/manual/en/latest/addons/node/node_wrangler.html). It will use some smart logic to match the most compatible sockets between the nodes, but if you ever want to be explicit you do so. The input and output sockets of a node are accessible as properties via the `i_*` and `o_*` prefixes, or you can use the `...` placeholder to specify the particular input to be user, or pass in the previous node as a named argument.
|
|
58
60
|
|
|
59
61
|
```py
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
g.Vector() >> g.SetPosition().i_offset
|
|
63
|
+
g.Vector() >> g.SetPosition(offset=...)
|
|
64
|
+
g.SetPosition(offset=g.Vector())
|
|
63
65
|
```
|
|
64
66
|
|
|
65
67
|
The `>>` operator will always look for the _most_ compatible sockets first (matching data types) before looking for other compatible but not identical socket data types to link.
|
|
@@ -76,7 +78,7 @@ Entering the `tree.inputs` and `tree.outputs` contexts will let you add new inte
|
|
|
76
78
|
|
|
77
79
|
```py
|
|
78
80
|
with TreeBuilder("MyTree") as tree:
|
|
79
|
-
points =
|
|
81
|
+
points = g.Points(position=g.RandomValue.vector(min=-1))
|
|
80
82
|
with tree.outputs:
|
|
81
83
|
points >> s.SocketGeometry("New Points")
|
|
82
84
|
```
|
|
@@ -86,7 +88,7 @@ with TreeBuilder("MyTree") as tree:
|
|
|
86
88
|
The node tree below creates a integer input and geometry output to the node group. We create a `rotation` variable that can be used later on as an argument, then construct a longer chain of nodes being created and linked together. The nodes are added and linked as each node is instantiated. After we exit the tree context, the nodes are automatically arranged.
|
|
87
89
|
|
|
88
90
|
``` python
|
|
89
|
-
from nodebpy import TreeBuilder,
|
|
91
|
+
from nodebpy import TreeBuilder, geometry as g, sockets as s
|
|
90
92
|
|
|
91
93
|
with TreeBuilder("AnotherTree", collapse=True) as tree:
|
|
92
94
|
with tree.inputs:
|
|
@@ -95,21 +97,21 @@ with TreeBuilder("AnotherTree", collapse=True) as tree:
|
|
|
95
97
|
instances = s.SocketGeometry("Instances")
|
|
96
98
|
|
|
97
99
|
rotation = (
|
|
98
|
-
|
|
99
|
-
>>
|
|
100
|
-
>>
|
|
100
|
+
g.RandomValue.vector(min=-1, seed=2)
|
|
101
|
+
>> g.AlignRotationToVector()
|
|
102
|
+
>> g.RotateRotation(rotate_by=g.AxisAngleToRotation(angle=0.3))
|
|
101
103
|
)
|
|
102
104
|
|
|
103
105
|
_ = (
|
|
104
106
|
count
|
|
105
|
-
>>
|
|
106
|
-
>>
|
|
107
|
-
>>
|
|
108
|
-
position=
|
|
107
|
+
>> g.Points(position=g.RandomValue.vector(min=-1))
|
|
108
|
+
>> g.InstanceOnPoints(instance=g.Cube(), rotation=rotation)
|
|
109
|
+
>> g.SetPosition(
|
|
110
|
+
position=g.Position() * 2.0 + (0, 0.2, 0.3),
|
|
109
111
|
offset=(0, 0, 0.1),
|
|
110
112
|
)
|
|
111
|
-
>>
|
|
112
|
-
>>
|
|
113
|
+
>> g.RealizeInstances()
|
|
114
|
+
>> g.InstanceOnPoints(g.Cube(), instance=...)
|
|
113
115
|
>> instances
|
|
114
116
|
)
|
|
115
117
|
```
|
|
@@ -126,16 +128,16 @@ The basic math operators also automatically add relevant nodes with their operat
|
|
|
126
128
|
|
|
127
129
|
```py
|
|
128
130
|
# operation is exposed as a property
|
|
129
|
-
math =
|
|
131
|
+
math = g.Math(1.0, 2.0, operation='ADD')
|
|
130
132
|
math.operation = "SUBTRACT"
|
|
131
133
|
|
|
132
134
|
# operation can be chose as a class method
|
|
133
|
-
math =
|
|
134
|
-
math =
|
|
135
|
+
math = g.Math.subtract(1.0, 2.0)
|
|
136
|
+
math = g.Math.add(1.0, 2.0)
|
|
135
137
|
|
|
136
|
-
# these are equivalent, the
|
|
137
|
-
|
|
138
|
-
|
|
138
|
+
# these are equivalent, the g.Math.multiply is automatically added
|
|
139
|
+
g.Value(1.0) * 2
|
|
140
|
+
g.Math.multiply(g.Value(1.0), 2.0)
|
|
139
141
|
```
|
|
140
142
|
|
|
141
143
|
# Design Considerations
|
|
@@ -33,9 +33,9 @@ In `nodebpy` we use the `>>` operator to link from one node or socket into anoth
|
|
|
33
33
|
This should feel and behave much like the <kbd>Alt</kbd> + <kbd>Right Click</kbd> drag between nodes in [Node Wrangler](https://docs.blender.org/manual/en/latest/addons/node/node_wrangler.html). It will use some smart logic to match the most compatible sockets between the nodes, but if you ever want to be explicit you do so. The input and output sockets of a node are accessible as properties via the `i_*` and `o_*` prefixes, or you can use the `...` placeholder to specify the particular input to be user, or pass in the previous node as a named argument.
|
|
34
34
|
|
|
35
35
|
```py
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
g.Vector() >> g.SetPosition().i_offset
|
|
37
|
+
g.Vector() >> g.SetPosition(offset=...)
|
|
38
|
+
g.SetPosition(offset=g.Vector())
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
The `>>` operator will always look for the _most_ compatible sockets first (matching data types) before looking for other compatible but not identical socket data types to link.
|
|
@@ -52,7 +52,7 @@ Entering the `tree.inputs` and `tree.outputs` contexts will let you add new inte
|
|
|
52
52
|
|
|
53
53
|
```py
|
|
54
54
|
with TreeBuilder("MyTree") as tree:
|
|
55
|
-
points =
|
|
55
|
+
points = g.Points(position=g.RandomValue.vector(min=-1))
|
|
56
56
|
with tree.outputs:
|
|
57
57
|
points >> s.SocketGeometry("New Points")
|
|
58
58
|
```
|
|
@@ -62,7 +62,7 @@ with TreeBuilder("MyTree") as tree:
|
|
|
62
62
|
The node tree below creates a integer input and geometry output to the node group. We create a `rotation` variable that can be used later on as an argument, then construct a longer chain of nodes being created and linked together. The nodes are added and linked as each node is instantiated. After we exit the tree context, the nodes are automatically arranged.
|
|
63
63
|
|
|
64
64
|
``` python
|
|
65
|
-
from nodebpy import TreeBuilder,
|
|
65
|
+
from nodebpy import TreeBuilder, geometry as g, sockets as s
|
|
66
66
|
|
|
67
67
|
with TreeBuilder("AnotherTree", collapse=True) as tree:
|
|
68
68
|
with tree.inputs:
|
|
@@ -71,21 +71,21 @@ with TreeBuilder("AnotherTree", collapse=True) as tree:
|
|
|
71
71
|
instances = s.SocketGeometry("Instances")
|
|
72
72
|
|
|
73
73
|
rotation = (
|
|
74
|
-
|
|
75
|
-
>>
|
|
76
|
-
>>
|
|
74
|
+
g.RandomValue.vector(min=-1, seed=2)
|
|
75
|
+
>> g.AlignRotationToVector()
|
|
76
|
+
>> g.RotateRotation(rotate_by=g.AxisAngleToRotation(angle=0.3))
|
|
77
77
|
)
|
|
78
78
|
|
|
79
79
|
_ = (
|
|
80
80
|
count
|
|
81
|
-
>>
|
|
82
|
-
>>
|
|
83
|
-
>>
|
|
84
|
-
position=
|
|
81
|
+
>> g.Points(position=g.RandomValue.vector(min=-1))
|
|
82
|
+
>> g.InstanceOnPoints(instance=g.Cube(), rotation=rotation)
|
|
83
|
+
>> g.SetPosition(
|
|
84
|
+
position=g.Position() * 2.0 + (0, 0.2, 0.3),
|
|
85
85
|
offset=(0, 0, 0.1),
|
|
86
86
|
)
|
|
87
|
-
>>
|
|
88
|
-
>>
|
|
87
|
+
>> g.RealizeInstances()
|
|
88
|
+
>> g.InstanceOnPoints(g.Cube(), instance=...)
|
|
89
89
|
>> instances
|
|
90
90
|
)
|
|
91
91
|
```
|
|
@@ -102,16 +102,16 @@ The basic math operators also automatically add relevant nodes with their operat
|
|
|
102
102
|
|
|
103
103
|
```py
|
|
104
104
|
# operation is exposed as a property
|
|
105
|
-
math =
|
|
105
|
+
math = g.Math(1.0, 2.0, operation='ADD')
|
|
106
106
|
math.operation = "SUBTRACT"
|
|
107
107
|
|
|
108
108
|
# operation can be chose as a class method
|
|
109
|
-
math =
|
|
110
|
-
math =
|
|
109
|
+
math = g.Math.subtract(1.0, 2.0)
|
|
110
|
+
math = g.Math.add(1.0, 2.0)
|
|
111
111
|
|
|
112
|
-
# these are equivalent, the
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
# these are equivalent, the g.Math.multiply is automatically added
|
|
113
|
+
g.Value(1.0) * 2
|
|
114
|
+
g.Math.multiply(g.Value(1.0), 2.0)
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
# Design Considerations
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "nodebpy"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.8.0"
|
|
4
4
|
description = "Build nodes trees in Blender more elegantly with code"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
7
7
|
{ name = "Brady Johnston", email = "brady.johnston@me.com" }
|
|
8
8
|
]
|
|
9
9
|
requires-python = ">=3.11"
|
|
10
|
-
dependencies = [
|
|
11
|
-
"networkx>=3.6.1",
|
|
12
|
-
]
|
|
10
|
+
dependencies = []
|
|
13
11
|
|
|
14
12
|
[project.scripts]
|
|
15
13
|
nodebpy = "nodebpy:main"
|
|
16
14
|
|
|
17
15
|
[project.optional-dependencies]
|
|
16
|
+
networkx = [
|
|
17
|
+
"networkx>=3.6.1",
|
|
18
|
+
]
|
|
18
19
|
bpy = [
|
|
19
20
|
"bpy>=5.0.1",
|
|
20
21
|
]
|
|
@@ -24,6 +25,7 @@ jupyter = [
|
|
|
24
25
|
dev = [
|
|
25
26
|
"fake-bpy-module>=20260113",
|
|
26
27
|
"jsondiff>=2.2.1",
|
|
28
|
+
"networkx>=3.6.1",
|
|
27
29
|
"pytest>=9.0.2",
|
|
28
30
|
"pytest-cov>=7.0.0",
|
|
29
31
|
"quarto-cli>=1.8.26",
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections import Counter, deque
|
|
4
|
+
|
|
5
|
+
import bpy
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _is_layoutable(node: bpy.types.Node) -> bool:
|
|
9
|
+
"""Check if a node should be included in column-based layout.
|
|
10
|
+
|
|
11
|
+
Frame nodes are containers and reroute nodes are tiny routing helpers;
|
|
12
|
+
neither should occupy a full column slot.
|
|
13
|
+
"""
|
|
14
|
+
return node.bl_idname not in ("NodeFrame", "NodeReroute")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def build_dependency_graph(
|
|
18
|
+
tree: bpy.types.NodeTree,
|
|
19
|
+
) -> tuple[dict[bpy.types.Node, set[bpy.types.Node]], Counter]:
|
|
20
|
+
"""Build a graph of node dependencies and count input connections.
|
|
21
|
+
|
|
22
|
+
Only layoutable nodes (excluding frames and reroutes) are included.
|
|
23
|
+
"""
|
|
24
|
+
layoutable = {n for n in tree.nodes if _is_layoutable(n)}
|
|
25
|
+
dependency_graph: dict[bpy.types.Node, set[bpy.types.Node]] = {
|
|
26
|
+
node: set() for node in layoutable
|
|
27
|
+
}
|
|
28
|
+
socket_input_connection_count: Counter = Counter()
|
|
29
|
+
|
|
30
|
+
for link in tree.links:
|
|
31
|
+
if link.from_node in layoutable and link.to_node in layoutable:
|
|
32
|
+
dependency_graph[link.from_node].add(link.to_node)
|
|
33
|
+
socket_input_connection_count[link.to_socket] += 1
|
|
34
|
+
|
|
35
|
+
return dependency_graph, socket_input_connection_count
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def topological_sort(
|
|
39
|
+
dependency_graph: dict[bpy.types.Node, set[bpy.types.Node]],
|
|
40
|
+
) -> list[bpy.types.Node]:
|
|
41
|
+
"""Sort nodes in topological (dependency) order using Kahn's algorithm."""
|
|
42
|
+
incoming = {node: 0 for node in dependency_graph}
|
|
43
|
+
for dependents in dependency_graph.values():
|
|
44
|
+
for target in dependents:
|
|
45
|
+
incoming[target] += 1
|
|
46
|
+
|
|
47
|
+
queue = deque(node for node, count in incoming.items() if count == 0)
|
|
48
|
+
result: list[bpy.types.Node] = []
|
|
49
|
+
|
|
50
|
+
while queue:
|
|
51
|
+
current = queue.popleft()
|
|
52
|
+
result.append(current)
|
|
53
|
+
for dependent in dependency_graph[current]:
|
|
54
|
+
incoming[dependent] -= 1
|
|
55
|
+
if incoming[dependent] == 0:
|
|
56
|
+
queue.append(dependent)
|
|
57
|
+
|
|
58
|
+
return result
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def organize_into_columns(
|
|
62
|
+
nodes_in_order: list[bpy.types.Node],
|
|
63
|
+
dependency_graph: dict[bpy.types.Node, set[bpy.types.Node]],
|
|
64
|
+
) -> list[list[bpy.types.Node]]:
|
|
65
|
+
"""Assign each node to a column based on its furthest dependent."""
|
|
66
|
+
columns: list[list[bpy.types.Node]] = []
|
|
67
|
+
column_of: dict[bpy.types.Node, int] = {}
|
|
68
|
+
|
|
69
|
+
for node in reversed(nodes_in_order):
|
|
70
|
+
col = (
|
|
71
|
+
max(
|
|
72
|
+
(column_of[dep] for dep in dependency_graph[node]),
|
|
73
|
+
default=-1,
|
|
74
|
+
)
|
|
75
|
+
+ 1
|
|
76
|
+
)
|
|
77
|
+
column_of[node] = col
|
|
78
|
+
|
|
79
|
+
if col == len(columns):
|
|
80
|
+
columns.append([node])
|
|
81
|
+
else:
|
|
82
|
+
columns[col].append(node)
|
|
83
|
+
|
|
84
|
+
# reverse so flow goes left-to-right
|
|
85
|
+
return list(reversed(columns))
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def calculate_node_dimensions(
|
|
89
|
+
node: bpy.types.Node,
|
|
90
|
+
socket_input_connection_count: Counter,
|
|
91
|
+
interface_scale: float,
|
|
92
|
+
) -> tuple[float, float]:
|
|
93
|
+
"""Calculate the visual dimensions of a node.
|
|
94
|
+
|
|
95
|
+
When a node is collapsed (``node.hide is True``) only linked sockets
|
|
96
|
+
contribute to the height, and header / property / vector-expansion rows
|
|
97
|
+
are omitted.
|
|
98
|
+
"""
|
|
99
|
+
HEADER = 20
|
|
100
|
+
SOCKET = 32
|
|
101
|
+
HIDDEN_SOCKET = 14
|
|
102
|
+
HIDDEN_HEADER = 30
|
|
103
|
+
|
|
104
|
+
if node.hide:
|
|
105
|
+
linked_inputs = sum(1 for s in node.inputs if s.enabled and s.is_linked)
|
|
106
|
+
linked_outputs = sum(1 for s in node.outputs if s.enabled and s.is_linked)
|
|
107
|
+
visible = max(linked_inputs, linked_outputs, 1)
|
|
108
|
+
height = (HIDDEN_HEADER + visible * HIDDEN_SOCKET) * interface_scale
|
|
109
|
+
return node.width, height
|
|
110
|
+
PROPERTY_ROW = 28
|
|
111
|
+
VECTOR_EXPANDED = 84
|
|
112
|
+
|
|
113
|
+
enabled_inputs = sum(1 for s in node.inputs if s.enabled)
|
|
114
|
+
enabled_outputs = sum(1 for s in node.outputs if s.enabled)
|
|
115
|
+
|
|
116
|
+
# count properties specific to this node type (not inherited)
|
|
117
|
+
inherited_ids = {
|
|
118
|
+
prop.identifier
|
|
119
|
+
for base in type(node).__bases__
|
|
120
|
+
for prop in base.bl_rna.properties
|
|
121
|
+
}
|
|
122
|
+
node_property_count = sum(
|
|
123
|
+
1 for prop in node.bl_rna.properties if prop.identifier not in inherited_ids
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
# count vector inputs that need expanded UI widgets (not connected)
|
|
127
|
+
unconnected_vectors = sum(
|
|
128
|
+
1
|
|
129
|
+
for s in node.inputs
|
|
130
|
+
if s.enabled and s.type == "VECTOR" and socket_input_connection_count[s] == 0
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
height = (
|
|
134
|
+
HEADER
|
|
135
|
+
+ enabled_outputs * SOCKET
|
|
136
|
+
+ node_property_count * PROPERTY_ROW
|
|
137
|
+
+ enabled_inputs * SOCKET
|
|
138
|
+
+ unconnected_vectors * VECTOR_EXPANDED
|
|
139
|
+
) * interface_scale
|
|
140
|
+
|
|
141
|
+
return node.width, height
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _socket_index(socket: bpy.types.NodeSocket) -> int:
|
|
145
|
+
"""Return the index of a socket among its node's enabled sockets."""
|
|
146
|
+
collection = socket.node.inputs if not socket.is_output else socket.node.outputs
|
|
147
|
+
idx = 0
|
|
148
|
+
for s in collection:
|
|
149
|
+
if s == socket:
|
|
150
|
+
return idx
|
|
151
|
+
if s.enabled:
|
|
152
|
+
idx += 1
|
|
153
|
+
return idx
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _reduce_crossings(
|
|
157
|
+
columns: list[list[bpy.types.Node]],
|
|
158
|
+
tree: bpy.types.NodeTree,
|
|
159
|
+
passes: int = 4,
|
|
160
|
+
) -> None:
|
|
161
|
+
"""Reorder nodes within columns to reduce edge crossings.
|
|
162
|
+
|
|
163
|
+
Uses the barycenter heuristic with socket-level precision: for each node
|
|
164
|
+
compute its weight from the position of the sockets it connects to in the
|
|
165
|
+
adjacent column, then sort by that weight. This correctly distinguishes
|
|
166
|
+
nodes that connect to different sockets on the same target.
|
|
167
|
+
|
|
168
|
+
Alternating forward and backward sweeps iteratively improve the ordering.
|
|
169
|
+
"""
|
|
170
|
+
if len(columns) < 2:
|
|
171
|
+
return
|
|
172
|
+
|
|
173
|
+
layoutable = {n for col in columns for n in col}
|
|
174
|
+
col_of = {n: ci for ci, col in enumerate(columns) for n in col}
|
|
175
|
+
|
|
176
|
+
# Pre-compute per-node link weights towards each adjacent column direction.
|
|
177
|
+
# For a forward sweep (fixing col i, sorting col i+1), a node in col i+1
|
|
178
|
+
# cares about its connections INTO col i. The weight of each connection is
|
|
179
|
+
# the position of the *node* in the fixed column plus a fractional offset
|
|
180
|
+
# derived from the socket index, so that multiple links to the same node
|
|
181
|
+
# produce distinct, correctly ordered weights.
|
|
182
|
+
#
|
|
183
|
+
# We store raw (neighbour_node, socket_fraction) pairs per node per
|
|
184
|
+
# direction and resolve them during each sweep once column order is known.
|
|
185
|
+
|
|
186
|
+
# link records: for each layoutable node, collect tuples of
|
|
187
|
+
# (neighbour_node, socket_fraction)
|
|
188
|
+
# keyed by which side the neighbour is on (left or right).
|
|
189
|
+
left_links: dict[bpy.types.Node, list[tuple[bpy.types.Node, float]]] = {
|
|
190
|
+
n: [] for n in layoutable
|
|
191
|
+
}
|
|
192
|
+
right_links: dict[bpy.types.Node, list[tuple[bpy.types.Node, float]]] = {
|
|
193
|
+
n: [] for n in layoutable
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
for link in tree.links:
|
|
197
|
+
src, dst = link.from_node, link.to_node
|
|
198
|
+
if src not in layoutable or dst not in layoutable:
|
|
199
|
+
continue
|
|
200
|
+
src_col, dst_col = col_of[src], col_of[dst]
|
|
201
|
+
if src_col >= dst_col:
|
|
202
|
+
continue # only consider forward edges
|
|
203
|
+
|
|
204
|
+
# Weight based on socket position on the neighbour node.
|
|
205
|
+
# For a node in the right column looking left: the relevant socket is
|
|
206
|
+
# on the source node (output side).
|
|
207
|
+
# For a node in the left column looking right: the relevant socket is
|
|
208
|
+
# on the target node (input side).
|
|
209
|
+
out_count = max(1, sum(1 for s in src.outputs if s.enabled))
|
|
210
|
+
in_count = max(1, sum(1 for s in dst.inputs if s.enabled))
|
|
211
|
+
src_frac = _socket_index(link.from_socket) / out_count
|
|
212
|
+
dst_frac = _socket_index(link.to_socket) / in_count
|
|
213
|
+
|
|
214
|
+
# dst looks left towards src: weight by src's output socket position
|
|
215
|
+
left_links[dst].append((src, src_frac))
|
|
216
|
+
# src looks right towards dst: weight by dst's input socket position
|
|
217
|
+
right_links[src].append((dst, dst_frac))
|
|
218
|
+
|
|
219
|
+
for iteration in range(passes):
|
|
220
|
+
if iteration % 2 == 0:
|
|
221
|
+
# forward sweep: fix column i, sort column i+1
|
|
222
|
+
col_range = range(1, len(columns))
|
|
223
|
+
else:
|
|
224
|
+
# backward sweep: fix column i, sort column i-1
|
|
225
|
+
col_range = range(len(columns) - 2, -1, -1)
|
|
226
|
+
|
|
227
|
+
for ci in col_range:
|
|
228
|
+
if iteration % 2 == 0:
|
|
229
|
+
fixed_col = columns[ci - 1]
|
|
230
|
+
links_map = left_links
|
|
231
|
+
else:
|
|
232
|
+
fixed_col = columns[ci + 1]
|
|
233
|
+
links_map = right_links
|
|
234
|
+
|
|
235
|
+
pos_in_fixed = {node: idx for idx, node in enumerate(fixed_col)}
|
|
236
|
+
original_pos = {node: float(idx) for idx, node in enumerate(columns[ci])}
|
|
237
|
+
|
|
238
|
+
barycenters: dict[bpy.types.Node, float] = {}
|
|
239
|
+
for node in columns[ci]:
|
|
240
|
+
weights = [
|
|
241
|
+
pos_in_fixed[nb] + frac
|
|
242
|
+
for nb, frac in links_map[node]
|
|
243
|
+
if nb in pos_in_fixed
|
|
244
|
+
]
|
|
245
|
+
if weights:
|
|
246
|
+
barycenters[node] = sum(weights) / len(weights)
|
|
247
|
+
else:
|
|
248
|
+
barycenters[node] = original_pos[node]
|
|
249
|
+
|
|
250
|
+
columns[ci].sort(key=lambda n: barycenters[n])
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def position_nodes_in_columns(
|
|
254
|
+
columns: list[list[bpy.types.Node]],
|
|
255
|
+
connection_counts: Counter,
|
|
256
|
+
spacing: tuple[float, float] = (50, 25),
|
|
257
|
+
) -> None:
|
|
258
|
+
"""Position nodes column-by-column with the given spacing.
|
|
259
|
+
|
|
260
|
+
Consecutive collapsed nodes are stacked tightly (with minimal gap) to
|
|
261
|
+
keep related math/converter chains visually grouped together.
|
|
262
|
+
"""
|
|
263
|
+
COLLAPSED_GAP = 4
|
|
264
|
+
|
|
265
|
+
x = 0.0
|
|
266
|
+
for column in columns:
|
|
267
|
+
col_width = 0.0
|
|
268
|
+
y = 0.0
|
|
269
|
+
prev_hidden = False
|
|
270
|
+
|
|
271
|
+
for node in column:
|
|
272
|
+
node.update()
|
|
273
|
+
|
|
274
|
+
width, height = calculate_node_dimensions(node, connection_counts, 1.0)
|
|
275
|
+
|
|
276
|
+
if width > col_width:
|
|
277
|
+
col_width = width
|
|
278
|
+
|
|
279
|
+
node.location = (x, y)
|
|
280
|
+
|
|
281
|
+
# use tight spacing between consecutive collapsed nodes
|
|
282
|
+
if node.hide and prev_hidden:
|
|
283
|
+
y -= height + COLLAPSED_GAP
|
|
284
|
+
else:
|
|
285
|
+
y -= height + spacing[1]
|
|
286
|
+
|
|
287
|
+
prev_hidden = node.hide
|
|
288
|
+
|
|
289
|
+
x += col_width + spacing[0]
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def position_reroutes(tree: bpy.types.NodeTree) -> None:
|
|
293
|
+
"""Place reroute nodes midway between their source and target."""
|
|
294
|
+
for node in tree.nodes:
|
|
295
|
+
if node.bl_idname != "NodeReroute":
|
|
296
|
+
continue
|
|
297
|
+
|
|
298
|
+
sources: list[bpy.types.Node] = []
|
|
299
|
+
targets: list[bpy.types.Node] = []
|
|
300
|
+
for link in tree.links:
|
|
301
|
+
if link.to_node == node:
|
|
302
|
+
sources.append(link.from_node)
|
|
303
|
+
if link.from_node == node:
|
|
304
|
+
targets.append(link.to_node)
|
|
305
|
+
|
|
306
|
+
neighbours = sources + targets
|
|
307
|
+
if not neighbours:
|
|
308
|
+
continue
|
|
309
|
+
|
|
310
|
+
avg_x = sum(n.location.x for n in neighbours) / len(neighbours)
|
|
311
|
+
avg_y = sum(n.location.y for n in neighbours) / len(neighbours)
|
|
312
|
+
node.location = (avg_x, avg_y)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def arrange_tree(
|
|
316
|
+
tree: bpy.types.NodeTree,
|
|
317
|
+
spacing: tuple[float, float] = (50, 25),
|
|
318
|
+
) -> None:
|
|
319
|
+
"""Arrange nodes in a node tree based on their dependencies.
|
|
320
|
+
|
|
321
|
+
Organises layoutable nodes into columns from left to right and positions
|
|
322
|
+
reroute nodes between their neighbours. Frame nodes are left untouched.
|
|
323
|
+
"""
|
|
324
|
+
if not tree.nodes:
|
|
325
|
+
return
|
|
326
|
+
|
|
327
|
+
dependency_graph, connection_counts = build_dependency_graph(tree)
|
|
328
|
+
|
|
329
|
+
if not dependency_graph:
|
|
330
|
+
return
|
|
331
|
+
|
|
332
|
+
nodes_in_order = topological_sort(dependency_graph)
|
|
333
|
+
columns = organize_into_columns(nodes_in_order, dependency_graph)
|
|
334
|
+
_reduce_crossings(columns, tree)
|
|
335
|
+
position_nodes_in_columns(columns, connection_counts, spacing)
|
|
336
|
+
position_reroutes(tree)
|