kopia lustrzana https://github.com/carson-katri/geometry-script
Support Blender 3.x alongside 4.x
rodzic
56d61980ad
commit
6c25f12729
33
api/tree.py
33
api/tree.py
|
@ -16,6 +16,8 @@ from .static.sample_mode import *
|
||||||
from .static.simulation import *
|
from .static.simulation import *
|
||||||
from .arrange import _arrange
|
from .arrange import _arrange
|
||||||
|
|
||||||
|
IS_BLENDER_4 = bpy.app.version[0] >= 4
|
||||||
|
|
||||||
def _as_iterable(x):
|
def _as_iterable(x):
|
||||||
if isinstance(x, Type):
|
if isinstance(x, Type):
|
||||||
return [x,]
|
return [x,]
|
||||||
|
@ -24,8 +26,16 @@ def _as_iterable(x):
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return [x,]
|
return [x,]
|
||||||
|
|
||||||
get_node_inputs = lambda x: [i for i in x.interface.items_tree if i.item_type == 'SOCKET' and i.in_out == 'INPUT']
|
def get_node_inputs(x):
|
||||||
get_node_outputs = lambda x: [i for i in x.interface.items_tree if i.item_type == 'SOCKET' and i.in_out == 'OUTPUT']
|
if IS_BLENDER_4:
|
||||||
|
return [i for i in x.interface.items_tree if i.item_type == 'SOCKET' and i.in_out == 'INPUT']
|
||||||
|
else:
|
||||||
|
return x.inputs
|
||||||
|
def get_node_outputs(x):
|
||||||
|
if IS_BLENDER_4:
|
||||||
|
return [i for i in x.interface.items_tree if i.item_type == 'SOCKET' and i.in_out == 'OUTPUT']
|
||||||
|
else:
|
||||||
|
return x.outputs
|
||||||
|
|
||||||
def tree(name):
|
def tree(name):
|
||||||
tree_name = name
|
tree_name = name
|
||||||
|
@ -39,6 +49,7 @@ def tree(name):
|
||||||
else:
|
else:
|
||||||
node_group = bpy.data.node_groups.new(tree_name, 'GeometryNodeTree')
|
node_group = bpy.data.node_groups.new(tree_name, 'GeometryNodeTree')
|
||||||
|
|
||||||
|
if IS_BLENDER_4:
|
||||||
node_group.is_modifier = True
|
node_group.is_modifier = True
|
||||||
|
|
||||||
# Clear the node group before building
|
# Clear the node group before building
|
||||||
|
@ -48,10 +59,16 @@ def tree(name):
|
||||||
node_inputs = get_node_inputs(node_group)
|
node_inputs = get_node_inputs(node_group)
|
||||||
input_count = sum(map(lambda p: len(p.annotation.__annotations__) if issubclass(p.annotation, InputGroup) else 1, list(signature.parameters.values())))
|
input_count = sum(map(lambda p: len(p.annotation.__annotations__) if issubclass(p.annotation, InputGroup) else 1, list(signature.parameters.values())))
|
||||||
for node_input in node_inputs[input_count:]:
|
for node_input in node_inputs[input_count:]:
|
||||||
|
if IS_BLENDER_4:
|
||||||
node_group.interface.remove(node_input)
|
node_group.interface.remove(node_input)
|
||||||
|
else:
|
||||||
|
node_group.inputs.remove(node_input)
|
||||||
|
|
||||||
for group_output in get_node_outputs(node_group):
|
for group_output in get_node_outputs(node_group):
|
||||||
|
if IS_BLENDER_4:
|
||||||
node_group.interface.remove(group_output)
|
node_group.interface.remove(group_output)
|
||||||
|
else:
|
||||||
|
node_group.outputs.remove(group_output)
|
||||||
|
|
||||||
# Setup the group inputs
|
# Setup the group inputs
|
||||||
group_input_node = node_group.nodes.new('NodeGroupInput')
|
group_input_node = node_group.nodes.new('NodeGroupInput')
|
||||||
|
@ -80,7 +97,10 @@ def tree(name):
|
||||||
for i, node_input in enumerate(node_inputs):
|
for i, node_input in enumerate(node_inputs):
|
||||||
if node_input.bl_socket_idname != list(inputs.values())[i][0].socket_type:
|
if node_input.bl_socket_idname != list(inputs.values())[i][0].socket_type:
|
||||||
for ni in node_inputs:
|
for ni in node_inputs:
|
||||||
|
if IS_BLENDER_4:
|
||||||
node_group.interface.remove(ni)
|
node_group.interface.remove(ni)
|
||||||
|
else:
|
||||||
|
node_group.inputs.remove(ni)
|
||||||
break
|
break
|
||||||
builder_inputs = {}
|
builder_inputs = {}
|
||||||
|
|
||||||
|
@ -91,7 +111,10 @@ def tree(name):
|
||||||
node_inputs[i].name = input_name
|
node_inputs[i].name = input_name
|
||||||
node_input = node_inputs[i]
|
node_input = node_inputs[i]
|
||||||
else:
|
else:
|
||||||
|
if IS_BLENDER_4:
|
||||||
node_input = node_group.interface.new_socket(socket_type=arg[1][0].socket_type, name=input_name, in_out='INPUT')
|
node_input = node_group.interface.new_socket(socket_type=arg[1][0].socket_type, name=input_name, in_out='INPUT')
|
||||||
|
else:
|
||||||
|
node_input = node_group.inputs.new(arg[1][0].socket_type, input_name)
|
||||||
if arg[1][1] != inspect.Parameter.empty:
|
if arg[1][1] != inspect.Parameter.empty:
|
||||||
node_input.default_value = arg[1][1]
|
node_input.default_value = arg[1][1]
|
||||||
if arg[1][2] is not None:
|
if arg[1][2] is not None:
|
||||||
|
@ -118,14 +141,20 @@ def tree(name):
|
||||||
for i, (k, v) in enumerate(outputs.items()):
|
for i, (k, v) in enumerate(outputs.items()):
|
||||||
if not issubclass(type(v), Type):
|
if not issubclass(type(v), Type):
|
||||||
v = Type(value=v)
|
v = Type(value=v)
|
||||||
|
if IS_BLENDER_4:
|
||||||
node_group.interface.new_socket(socket_type=v.socket_type, name=k, in_out='OUTPUT')
|
node_group.interface.new_socket(socket_type=v.socket_type, name=k, in_out='OUTPUT')
|
||||||
|
else:
|
||||||
|
node_group.outputs.new(v.socket_type, k)
|
||||||
node_group.links.new(v._socket, group_output_node.inputs[i])
|
node_group.links.new(v._socket, group_output_node.inputs[i])
|
||||||
else:
|
else:
|
||||||
for i, result in enumerate(_as_iterable(outputs)):
|
for i, result in enumerate(_as_iterable(outputs)):
|
||||||
if not issubclass(type(result), Type):
|
if not issubclass(type(result), Type):
|
||||||
result = Type(value=result)
|
result = Type(value=result)
|
||||||
# raise Exception(f"Return value '{result}' is not a valid 'Type' subclass.")
|
# raise Exception(f"Return value '{result}' is not a valid 'Type' subclass.")
|
||||||
|
if IS_BLENDER_4:
|
||||||
node_group.interface.new_socket(socket_type=result.socket_type, name='Result', in_out='OUTPUT')
|
node_group.interface.new_socket(socket_type=result.socket_type, name='Result', in_out='OUTPUT')
|
||||||
|
else:
|
||||||
|
node_group.outputs.new(result.socket_type, 'Result')
|
||||||
node_group.links.new(result._socket, group_output_node.inputs[i])
|
node_group.links.new(result._socket, group_output_node.inputs[i])
|
||||||
|
|
||||||
_arrange(node_group)
|
_arrange(node_group)
|
||||||
|
|
Ładowanie…
Reference in New Issue