Nodes: Panels integration with blend files and UI
Part 3/3 of #109135, #110272 Switch to new node group interfaces and deprecate old DNA and API. This completes support for panels in node drawing and in node group interface declarations in particular. The new node group interface DNA and RNA code has been added in parts 1 and 2 (#110885, #110952) but has not be enabled yet. This commit completes the integration by * enabling the new RNA API * using the new API in UI * read/write new interfaces from blend files * add versioning for backward compatibility * add forward-compatible writing code to reconstruct old interfaces All places accessing node group interface declarations should now be using the new API. A runtime cache has been added that allows simple linear access to socket inputs and outputs even when a panel hierarchy is used. Old DNA has been deprecated and should only be accessed for versioning (inputs/outputs renamed to inputs_legacy/outputs_legacy to catch errors). Versioning code ensures both backward and forward compatibility of existing files. The API for old interfaces is removed. The new API is very similar but is defined on the `ntree.interface` instead of the `ntree` directly. Breaking change notifications and detailed instructions for migrating will be added. A python test has been added for the node group API functions. This includes new functionality such as creating panels and moving items between different levels. This patch does not yet contain panel representations in the modifier UI. This has been tested in a separate branch and will be added with a later PR (#108565). Pull Request: https://projects.blender.org/blender/blender/pulls/111348
This commit is contained in:
@@ -1218,7 +1218,11 @@ class NodeSocket(StructRNA, metaclass=RNAMetaPropGroup):
|
||||
link.to_socket == self))
|
||||
|
||||
|
||||
class NodeSocketInterface(StructRNA, metaclass=RNAMetaPropGroup):
|
||||
class NodeTreeInterfaceItem(StructRNA):
|
||||
__slots__ = ()
|
||||
|
||||
|
||||
class NodeTreeInterfaceSocket(NodeTreeInterfaceItem, metaclass=RNAMetaPropGroup):
|
||||
__slots__ = ()
|
||||
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ from bpy.props import (
|
||||
|
||||
def build_default_empty_geometry_node_group(name):
|
||||
group = bpy.data.node_groups.new(name, 'GeometryNodeTree')
|
||||
group.inputs.new('NodeSocketGeometry', data_("Geometry"))
|
||||
group.outputs.new('NodeSocketGeometry', data_("Geometry"))
|
||||
group.interface.new_socket(data_("Geometry"), in_out={'OUTPUT'}, socket_type='NodeSocketGeometry')
|
||||
group.interface.new_socket(data_("Geometry"), in_out={'INPUT'}, socket_type='NodeSocketGeometry')
|
||||
input_node = group.nodes.new('NodeGroupInput')
|
||||
output_node = group.nodes.new('NodeGroupOutput')
|
||||
output_node.is_active_output = True
|
||||
|
||||
@@ -260,6 +260,112 @@ class NODE_OT_tree_path_parent(Operator):
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class NodeInterfaceOperator():
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
space = context.space_data
|
||||
if not space or space.type != 'NODE_EDITOR' or not space.edit_tree:
|
||||
return False
|
||||
if space.edit_tree.is_embedded_data:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class NODE_OT_interface_item_new(NodeInterfaceOperator, Operator):
|
||||
'''Add a new item to the interface'''
|
||||
bl_idname = "node.interface_item_new"
|
||||
bl_label = "New Item"
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
item_type: EnumProperty(
|
||||
name="Item Type",
|
||||
description="Type of the item to create",
|
||||
items=[
|
||||
('INPUT', "Input", ""),
|
||||
('OUTPUT', "Output", ""),
|
||||
('PANEL', "Panel", "")],
|
||||
default='INPUT',
|
||||
)
|
||||
|
||||
socket_type = 'NodeSocketFloat'
|
||||
|
||||
def execute(self, context):
|
||||
snode = context.space_data
|
||||
tree = snode.edit_tree
|
||||
interface = tree.interface
|
||||
|
||||
# Remember active item and position to determine target position.
|
||||
active_item = interface.active
|
||||
active_pos = active_item.position if active_item else -1
|
||||
|
||||
if self.item_type == 'INPUT':
|
||||
item = interface.new_socket("Socket", socket_type=self.socket_type, in_out={'INPUT'})
|
||||
elif self.item_type == 'OUTPUT':
|
||||
item = interface.new_socket("Socket", socket_type=self.socket_type, in_out={'OUTPUT'})
|
||||
elif self.item_type == 'PANEL':
|
||||
item = interface.new_panel("Panel")
|
||||
else:
|
||||
return {'CANCELLED'}
|
||||
|
||||
if active_item:
|
||||
# Insert into active panel if possible, otherwise insert after active item.
|
||||
if active_item.item_type == 'PANEL' and item.item_type != 'PANEL':
|
||||
interface.move_to_parent(item, active_item, len(active_item.interface_items))
|
||||
else:
|
||||
interface.move_to_parent(item, active_item.parent, active_pos + 1)
|
||||
interface.active = item
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class NODE_OT_interface_item_duplicate(NodeInterfaceOperator, Operator):
|
||||
'''Add a copy of the active item to the interface'''
|
||||
bl_idname = "node.interface_item_duplicate"
|
||||
bl_label = "Duplicate Item"
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
if not super().poll(context):
|
||||
return False
|
||||
|
||||
snode = context.space_data
|
||||
tree = snode.edit_tree
|
||||
interface = tree.interface
|
||||
return interface.active is not None
|
||||
|
||||
def execute(self, context):
|
||||
snode = context.space_data
|
||||
tree = snode.edit_tree
|
||||
interface = tree.interface
|
||||
item = interface.active
|
||||
|
||||
if item:
|
||||
item_copy = interface.copy(item)
|
||||
interface.active = item_copy
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class NODE_OT_interface_item_remove(NodeInterfaceOperator, Operator):
|
||||
'''Remove active item from the interface'''
|
||||
bl_idname = "node.interface_item_remove"
|
||||
bl_label = "Remove Item"
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
def execute(self, context):
|
||||
snode = context.space_data
|
||||
tree = snode.edit_tree
|
||||
interface = tree.interface
|
||||
item = interface.active
|
||||
|
||||
if item:
|
||||
interface.remove(item)
|
||||
interface.active_index = min(interface.active_index, len(interface.ui_items) - 1)
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
classes = (
|
||||
NodeSetting,
|
||||
|
||||
@@ -267,5 +373,8 @@ classes = (
|
||||
NODE_OT_add_simulation_zone,
|
||||
NODE_OT_add_repeat_zone,
|
||||
NODE_OT_collapse_hide_unused_toggle,
|
||||
NODE_OT_interface_item_new,
|
||||
NODE_OT_interface_item_duplicate,
|
||||
NODE_OT_interface_item_remove,
|
||||
NODE_OT_tree_path_parent,
|
||||
)
|
||||
|
||||
@@ -859,22 +859,20 @@ class NODE_PT_overlay(Panel):
|
||||
col.prop(overlay, "show_named_attributes", text="Named Attributes")
|
||||
|
||||
|
||||
class NODE_UL_interface_sockets(bpy.types.UIList):
|
||||
def draw_item(self, context, layout, _data, item, icon, _active_data, _active_propname, _index):
|
||||
socket = item
|
||||
color = socket.draw_color(context)
|
||||
class NODE_MT_node_tree_interface_context_menu(Menu):
|
||||
bl_label = "Node Tree Interface Specials"
|
||||
|
||||
if self.layout_type in {'DEFAULT', 'COMPACT'}:
|
||||
row = layout.row(align=True)
|
||||
def draw(self, _context):
|
||||
layout = self.layout
|
||||
|
||||
row.template_node_socket(color=color)
|
||||
row.prop(socket, "name", text="", emboss=False, icon_value=icon)
|
||||
elif self.layout_type == 'GRID':
|
||||
layout.alignment = 'CENTER'
|
||||
layout.template_node_socket(color=color)
|
||||
layout.operator("node.interface_item_duplicate", icon='DUPLICATE')
|
||||
|
||||
|
||||
class NodeTreeInterfacePanel(Panel):
|
||||
class NODE_PT_node_tree_interface(Panel):
|
||||
bl_space_type = 'NODE_EDITOR'
|
||||
bl_region_type = 'UI'
|
||||
bl_category = "Group"
|
||||
bl_label = "Interface"
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
@@ -888,119 +886,52 @@ class NodeTreeInterfacePanel(Panel):
|
||||
return False
|
||||
return True
|
||||
|
||||
def draw_socket_list(self, context, in_out, sockets_propname, active_socket_propname):
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
|
||||
snode = context.space_data
|
||||
tree = snode.edit_tree
|
||||
sockets = getattr(tree, sockets_propname)
|
||||
active_socket_index = getattr(tree, active_socket_propname)
|
||||
active_socket = sockets[active_socket_index] if active_socket_index >= 0 else None
|
||||
|
||||
split = layout.row()
|
||||
|
||||
split.template_list("NODE_UL_interface_sockets", in_out, tree, sockets_propname, tree, active_socket_propname)
|
||||
split.template_node_tree_interface(tree.interface)
|
||||
|
||||
ops_col = split.column()
|
||||
|
||||
add_remove_col = ops_col.column(align=True)
|
||||
props = add_remove_col.operator("node.tree_socket_add", icon='ADD', text="")
|
||||
props.in_out = in_out
|
||||
props = add_remove_col.operator("node.tree_socket_remove", icon='REMOVE', text="")
|
||||
props.in_out = in_out
|
||||
ops_col = split.column(align=True)
|
||||
ops_col.operator_menu_enum("node.interface_item_new", "item_type", icon='ADD', text="")
|
||||
ops_col.operator("node.interface_item_remove", icon='REMOVE', text="")
|
||||
ops_col.separator()
|
||||
ops_col.menu("NODE_MT_node_tree_interface_context_menu", icon='DOWNARROW_HLT', text="")
|
||||
|
||||
ops_col.separator()
|
||||
|
||||
up_down_col = ops_col.column(align=True)
|
||||
props = up_down_col.operator("node.tree_socket_move", icon='TRIA_UP', text="")
|
||||
props.in_out = in_out
|
||||
props.direction = 'UP'
|
||||
props = up_down_col.operator("node.tree_socket_move", icon='TRIA_DOWN', text="")
|
||||
props.in_out = in_out
|
||||
props.direction = 'DOWN'
|
||||
|
||||
if active_socket is not None:
|
||||
# Mimicking property split.
|
||||
layout.use_property_split = False
|
||||
layout.use_property_decorate = False
|
||||
layout_row = layout.row(align=True)
|
||||
layout_split = layout_row.split(factor=0.4, align=True)
|
||||
|
||||
label_column = layout_split.column(align=True)
|
||||
label_column.alignment = 'RIGHT'
|
||||
# Menu to change the socket type.
|
||||
label_column.label(text="Type")
|
||||
|
||||
property_row = layout_split.row(align=True)
|
||||
props = property_row.operator_menu_enum(
|
||||
"node.tree_socket_change_type",
|
||||
"socket_type",
|
||||
text=(iface_(active_socket.bl_label) if active_socket.bl_label
|
||||
else iface_(active_socket.bl_idname)),
|
||||
)
|
||||
props.in_out = in_out
|
||||
|
||||
with context.temp_override(interface_socket=active_socket):
|
||||
if bpy.ops.node.tree_socket_change_subtype.poll():
|
||||
layout_row = layout.row(align=True)
|
||||
layout_split = layout_row.split(factor=0.4, align=True)
|
||||
|
||||
label_column = layout_split.column(align=True)
|
||||
label_column.alignment = 'RIGHT'
|
||||
label_column.label(text="Subtype")
|
||||
property_row = layout_split.row(align=True)
|
||||
|
||||
property_row.context_pointer_set("interface_socket", active_socket)
|
||||
props = property_row.operator_menu_enum(
|
||||
"node.tree_socket_change_subtype",
|
||||
"socket_subtype",
|
||||
text=(iface_(active_socket.bl_subtype_label) if active_socket.bl_subtype_label
|
||||
else iface_(active_socket.bl_idname)),
|
||||
)
|
||||
|
||||
active_item = tree.interface.active
|
||||
if active_item is not None:
|
||||
layout.use_property_split = True
|
||||
layout.use_property_decorate = False
|
||||
|
||||
layout.prop(active_socket, "name")
|
||||
# Display descriptions only for Geometry Nodes, since it's only used in the modifier panel.
|
||||
if tree.type == 'GEOMETRY':
|
||||
layout.prop(active_socket, "description")
|
||||
field_socket_prefixes = {
|
||||
"NodeSocketInt",
|
||||
"NodeSocketColor",
|
||||
"NodeSocketVector",
|
||||
"NodeSocketBool",
|
||||
"NodeSocketFloat",
|
||||
}
|
||||
is_field_type = any(
|
||||
active_socket.bl_socket_idname.startswith(prefix)
|
||||
for prefix in field_socket_prefixes
|
||||
)
|
||||
if is_field_type:
|
||||
if in_out == 'OUT':
|
||||
layout.prop(active_socket, "attribute_domain")
|
||||
layout.prop(active_socket, "default_attribute_name")
|
||||
active_socket.draw(context, layout)
|
||||
if active_item.item_type == 'SOCKET':
|
||||
layout.prop(active_item, "socket_type", text="Type")
|
||||
layout.prop(active_item, "description")
|
||||
layout.prop(active_item, "in_out", text="Input/Output")
|
||||
# Display descriptions only for Geometry Nodes, since it's only used in the modifier panel.
|
||||
if tree.type == 'GEOMETRY':
|
||||
field_socket_types = {
|
||||
"NodeSocketInt",
|
||||
"NodeSocketColor",
|
||||
"NodeSocketVector",
|
||||
"NodeSocketBool",
|
||||
"NodeSocketFloat",
|
||||
}
|
||||
if active_item.socket_type in field_socket_types:
|
||||
if 'OUTPUT' in active_item.in_out:
|
||||
layout.prop(active_item, "attribute_domain")
|
||||
layout.prop(active_item, "default_attribute_name")
|
||||
active_item.draw(context, layout)
|
||||
|
||||
if active_item.item_type == 'PANEL':
|
||||
layout.prop(active_item, "name")
|
||||
layout.prop(active_item, "description")
|
||||
layout.prop(active_item, "default_closed", text="Closed by Default")
|
||||
|
||||
class NODE_PT_node_tree_interface_inputs(NodeTreeInterfacePanel):
|
||||
bl_space_type = 'NODE_EDITOR'
|
||||
bl_region_type = 'UI'
|
||||
bl_category = "Group"
|
||||
bl_label = "Inputs"
|
||||
|
||||
def draw(self, context):
|
||||
self.draw_socket_list(context, "IN", "inputs", "active_input")
|
||||
|
||||
|
||||
class NODE_PT_node_tree_interface_outputs(NodeTreeInterfacePanel):
|
||||
bl_space_type = 'NODE_EDITOR'
|
||||
bl_region_type = 'UI'
|
||||
bl_category = "Group"
|
||||
bl_label = "Outputs"
|
||||
|
||||
def draw(self, context):
|
||||
self.draw_socket_list(context, "OUT", "outputs", "active_output")
|
||||
layout.use_property_split = False
|
||||
|
||||
|
||||
class NODE_UL_simulation_zone_items(bpy.types.UIList):
|
||||
@@ -1209,6 +1140,8 @@ classes = (
|
||||
NODE_PT_material_slots,
|
||||
NODE_PT_geometry_node_asset_traits,
|
||||
NODE_PT_node_color_presets,
|
||||
NODE_MT_node_tree_interface_context_menu,
|
||||
NODE_PT_node_tree_interface,
|
||||
NODE_PT_active_node_generic,
|
||||
NODE_PT_active_node_color,
|
||||
NODE_PT_texture_mapping,
|
||||
@@ -1217,9 +1150,6 @@ classes = (
|
||||
NODE_PT_quality,
|
||||
NODE_PT_annotation,
|
||||
NODE_PT_overlay,
|
||||
NODE_UL_interface_sockets,
|
||||
NODE_PT_node_tree_interface_inputs,
|
||||
NODE_PT_node_tree_interface_outputs,
|
||||
NODE_UL_simulation_zone_items,
|
||||
NODE_PT_simulation_zone_items,
|
||||
NODE_UL_repeat_zone_items,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import bpy
|
||||
from bpy.types import NodeTree, Node, NodeSocket
|
||||
from bpy.types import NodeTree, Node, NodeSocket, NodeTreeInterfaceSocket
|
||||
|
||||
# Implementation of custom nodes from Python
|
||||
|
||||
@@ -25,19 +25,9 @@ class MyCustomSocket(NodeSocket):
|
||||
# Label for nice name display
|
||||
bl_label = "Custom Node Socket"
|
||||
|
||||
# Enum items list
|
||||
my_items = (
|
||||
('DOWN', "Down", "Where your feet are"),
|
||||
('UP', "Up", "Where your head should be"),
|
||||
('LEFT', "Left", "Not right"),
|
||||
('RIGHT', "Right", "Not left"),
|
||||
)
|
||||
|
||||
my_enum_prop: bpy.props.EnumProperty(
|
||||
name="Direction",
|
||||
description="Just an example",
|
||||
items=my_items,
|
||||
default='UP',
|
||||
input_value: bpy.props.FloatProperty(
|
||||
name="Value",
|
||||
description="Value when the socket is not connected",
|
||||
)
|
||||
|
||||
# Optional function for drawing the socket input value
|
||||
@@ -45,13 +35,35 @@ class MyCustomSocket(NodeSocket):
|
||||
if self.is_output or self.is_linked:
|
||||
layout.label(text=text)
|
||||
else:
|
||||
layout.prop(self, "my_enum_prop", text=text)
|
||||
layout.prop(self, "input_value", text=text)
|
||||
|
||||
# Socket color
|
||||
def draw_color(self, context, node):
|
||||
@classmethod
|
||||
def draw_color_simple(cls):
|
||||
return (1.0, 0.4, 0.216, 0.5)
|
||||
|
||||
|
||||
# Customizable interface properties to generate a socket from.
|
||||
class MyCustomInterfaceSocket(NodeTreeInterfaceSocket):
|
||||
# The type of socket that is generated.
|
||||
bl_socket_idname = 'CustomSocketType'
|
||||
|
||||
default_value: bpy.props.FloatProperty(default=1.0, description="Default input value for new sockets",)
|
||||
|
||||
def draw(self, context, layout):
|
||||
# Display properties of the interface.
|
||||
layout.prop(self, "default_value")
|
||||
|
||||
# Set properties of newly created sockets
|
||||
def init_socket(self, node, socket, data_path):
|
||||
socket.input_value = self.default_value
|
||||
|
||||
# Use an existing socket to initialize the group interface
|
||||
def from_socket(self, node, socket):
|
||||
# Current value of the socket becomes the default
|
||||
self.default_value = socket.input_value
|
||||
|
||||
|
||||
# Mix-in class for all custom nodes in this tree type.
|
||||
# Defines a poll function to enable instantiation.
|
||||
class MyCustomTreeNode:
|
||||
@@ -163,6 +175,7 @@ node_categories = [
|
||||
classes = (
|
||||
MyCustomTree,
|
||||
MyCustomSocket,
|
||||
MyCustomInterfaceSocket,
|
||||
MyCustomNode,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user