Skip to content

Commit

Permalink
Merge pull request #4455 from nortikin/tree_socket_types
Browse files Browse the repository at this point in the history
Add list of available sockets for group tree
  • Loading branch information
Durman authored May 8, 2022
2 parents 59ebe30 + 04e703b commit 148c40f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
20 changes: 15 additions & 5 deletions core/node_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sverchok.data_structure import extend_blender_class
from mathutils import Vector

from sverchok.core.sockets import socket_type_names
from sverchok.core.group_handlers import MainHandler, NodeIdManager
from sverchok.core.events import GroupEvent
from sverchok.utils.tree_structure import Tree, Node
Expand Down Expand Up @@ -125,11 +126,14 @@ def update_sockets(self): # todo it lets simplify sockets API
n_in_s.use_prop = not t_in_s.hide_value
if hasattr(t_in_s, 'default_type'):
n_in_s.default_property_type = t_in_s.default_type
for node in (n for n in self.nodes if n.bl_idname == 'NodeGroupOutput'):
for n_in_s, t_out_s in zip(node.inputs, self.outputs):
n_in_s.use_prop = not t_out_s.hide_value
if hasattr(t_out_s, 'default_type'):
n_in_s.default_property_type = t_out_s.default_type
for out_node in (n for n in self.nodes if n.bl_idname == 'NodeGroupOutput'):
for n_in_s, t_out_s in zip(out_node.inputs, self.outputs):
if hasattr(n_in_s, 'default_property'):
n_in_s.use_prop = not t_out_s.hide_value
if hasattr(t_out_s, 'default_type'):
n_in_s.default_property_type = t_out_s.default_type
else:
n_in_s.use_prop = False

def check_reroutes_sockets(self):
"""
Expand Down Expand Up @@ -258,6 +262,12 @@ def get_update_path(self) -> List['SvGroupTreeNode']:
return group_nodes
raise LookupError(f'Path the group tree: {self} was not found')

if bpy.app.version >= (3, 2): # in 3.1 this can lead to a crash
@classmethod
def valid_socket_type(cls, socket_type: str):
# https://docs.blender.org/api/master/bpy.types.NodeTree.html#bpy.types.NodeTree.valid_socket_type
return socket_type in socket_type_names()


class BaseNode:
n_id: bpy.props.StringProperty(options={'SKIP_SAVE'})
Expand Down
22 changes: 20 additions & 2 deletions core/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import inspect
import sys
from typing import Set

from mathutils import Matrix, Quaternion
import bpy
Expand Down Expand Up @@ -73,6 +76,17 @@ def update_interface(self, context):
if input_node:
group_tree.update_nodes([input_node])


def socket_type_names() -> Set[str]:
names = set()
for name, member in inspect.getmembers(sys.modules[__name__]):
is_module_cls = inspect.isclass(member) and member.__module__ == __name__
if is_module_cls:
if NodeSocket in member.__bases__:
names.add(member.bl_idname)
return names


class SV_MT_AllSocketsOptionsMenu(bpy.types.Menu):
bl_label = "Sockets Options"

Expand Down Expand Up @@ -1481,13 +1495,17 @@ def socket_interface_classes():
prop_args['name'] = "Default value"
prop_args['update'] = lambda s, c: s.id_data.update_sockets()
socket_interface_attributes['__annotations__'] = {}
socket_interface_attributes['__annotations__']['default_value'] = (prop_func, prop_args)
socket_interface_attributes['__annotations__']['default_value'] = socket_cls.__annotations__['default_property']

def draw(self, context, layout):
col = layout.column()
col.prop(self, 'default_value')
col.prop(self, 'hide_value')
socket_interface_attributes['draw'] = draw
else:
def draw(self, context, layout):
pass

socket_interface_attributes['draw'] = draw
yield type(
f'{socket_cls.__name__}Interface', (bpy.types.NodeSocketInterface,), socket_interface_attributes)

Expand Down

0 comments on commit 148c40f

Please sign in to comment.