Skip to content

Commit e3befbe

Browse files
alphaditocarson-katri
authored andcommitted
Migrate to NodeTree.interface API for Blender 4.0 release
1 parent 63a7516 commit e3befbe

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

api/tree.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def _as_iterable(x):
2323
except TypeError:
2424
return [x,]
2525

26+
get_node_inputs = lambda x: [i for i in x.interface.items_tree if i.item_type == 'SOCKET' and i.in_out == 'INPUT']
27+
get_node_outputs = lambda x: [i for i in x.interface.items_tree if i.item_type == 'SOCKET' and i.in_out == 'OUTPUT']
28+
2629
def tree(name):
2730
tree_name = name
2831
def build_tree(builder):
@@ -34,13 +37,20 @@ def build_tree(builder):
3437
node_group = bpy.data.node_groups[tree_name]
3538
else:
3639
node_group = bpy.data.node_groups.new(tree_name, 'GeometryNodeTree')
40+
41+
node_group.is_modifier = True
42+
3743
# Clear the node group before building
3844
for node in node_group.nodes:
3945
node_group.nodes.remove(node)
40-
while len(node_group.inputs) > sum(map(lambda p: len(p.annotation.__annotations__) if issubclass(p.annotation, InputGroup) else 1, list(signature.parameters.values()))):
41-
node_group.inputs.remove(node_group.inputs[-1])
42-
for group_output in node_group.outputs:
43-
node_group.outputs.remove(group_output)
46+
47+
node_inputs = get_node_inputs(node_group)
48+
input_count = sum(map(lambda p: len(p.annotation.__annotations__) if issubclass(p.annotation, InputGroup) else 1, list(signature.parameters.values())))
49+
for node_input in node_inputs[input_count:]:
50+
node_group.interface.remove(node_input)
51+
52+
for group_output in get_node_outputs(node_group):
53+
node_group.interface.remove(group_output)
4454

4555
# Setup the group inputs
4656
group_input_node = node_group.nodes.new('NodeGroupInput')
@@ -65,19 +75,22 @@ def validate_param(param):
6575
inputs[param.name] = (param.annotation, param.default, None, None)
6676

6777
# Create the input sockets and collect input values.
68-
for i, node_input in enumerate(node_group.inputs):
78+
node_inputs = get_node_inputs(node_group)
79+
for i, node_input in enumerate(node_inputs):
6980
if node_input.bl_socket_idname != list(inputs.values())[i][0].socket_type:
70-
for ni in node_group.inputs:
71-
node_group.inputs.remove(ni)
81+
for ni in node_inputs:
82+
node_group.interface.remove(ni)
7283
break
7384
builder_inputs = {}
85+
86+
node_inputs = get_node_inputs(node_group)
7487
for i, arg in enumerate(inputs.items()):
7588
input_name = arg[0].replace('_', ' ').title()
76-
if len(node_group.inputs) > i:
77-
node_group.inputs[i].name = input_name
78-
node_input = node_group.inputs[i]
89+
if len(node_inputs) > i:
90+
node_inputs[i].name = input_name
91+
node_input = node_inputs[i]
7992
else:
80-
node_input = node_group.inputs.new(arg[1][0].socket_type, input_name)
93+
node_input = node_group.interface.new_socket(socket_type=arg[1][0].socket_type, name=input_name, in_out='INPUT')
8194
if arg[1][1] != inspect.Parameter.empty:
8295
node_input.default_value = arg[1][1]
8396
if arg[1][2] is not None:
@@ -104,22 +117,22 @@ def validate_param(param):
104117
for i, (k, v) in enumerate(outputs.items()):
105118
if not issubclass(type(v), Type):
106119
v = Type(value=v)
107-
node_group.outputs.new(v.socket_type, k)
120+
node_group.interface.new_socket(socket_type=v.socket_type, name=k, in_out='OUTPUT')
108121
node_group.links.new(v._socket, group_output_node.inputs[i])
109122
else:
110123
for i, result in enumerate(_as_iterable(outputs)):
111124
if not issubclass(type(result), Type):
112125
result = Type(value=result)
113126
# raise Exception(f"Return value '{result}' is not a valid 'Type' subclass.")
114-
node_group.outputs.new(result.socket_type, 'Result')
127+
node_group.interface.new_socket(socket_type=result.socket_type, name='Result', in_out='OUTPUT')
115128
node_group.links.new(result._socket, group_output_node.inputs[i])
116129

117130
_arrange(node_group)
118131

119132
# Return a function that creates a NodeGroup node in the tree.
120133
# This lets @trees be used in other @trees via simple function calls.
121134
def group_reference(*args, **kwargs):
122-
result = group(node_tree=node_group, *args, **kwargs)
135+
result = geometrynodegroup(node_tree=node_group, *args, **kwargs)
123136
group_outputs = []
124137
for group_output in result._socket.node.outputs:
125138
group_outputs.append(Type(group_output))

0 commit comments

Comments
 (0)