Skip to content

Commit

Permalink
Merge pull request #4323 from nortikin/sv_get_bottleneck
Browse files Browse the repository at this point in the history
Sv get bottleneck and rewritten update system
  • Loading branch information
Durman authored Jun 2, 2022
2 parents 40c8f53 + ff52da0 commit ed954eb
Show file tree
Hide file tree
Showing 51 changed files with 1,659 additions and 1,906 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"tracker_url": "http://www.blenderartists.org/forum/showthread.php?272679"
}

VERSION = 'v1.1.0-alpha.1' # looks like the only way to have custom format for the version
VERSION = 'v1.1.0-alpha.2' # looks like the only way to have custom format for the version

import sys
import importlib
Expand Down
17 changes: 10 additions & 7 deletions core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import importlib
import sverchok
from sverchok.utils.logging import debug, exception
from sverchok.core.update_system import clear_system_cache
from sverchok.core.socket_data import clear_all_socket_cache

reload_event = False

Expand All @@ -11,12 +10,16 @@
]

core_modules = [
"sv_custom_exceptions",
"sockets",
"handlers", "update_system", "main_tree_handler",
"events", "node_group", "group_handlers"
"sv_custom_exceptions", "update_system",
"sockets", "socket_data",
"handlers",
"events", "node_group",
"tasks",
"group_update_system",
"event_system",
]


def sv_register_modules(modules):
for m in modules:
if m.__name__ != "sverchok.menu":
Expand All @@ -25,7 +28,7 @@ def sv_register_modules(modules):
m.register()

def sv_unregister_modules(modules):
clear_system_cache()
clear_all_socket_cache()
for m in reversed(modules):
if hasattr(m, "unregister"):
try:
Expand Down
40 changes: 40 additions & 0 deletions core/event_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This file is part of project Sverchok. It's copyrighted by the contributors
# recorded in the version control history of the file, available from
# its original location https://github.com/nortikin/sverchok/commit/master
#
# SPDX-License-Identifier: GPL3
# License-Filename: LICENSE

from __future__ import annotations

import sverchok.core.events as ev
import sverchok.core.update_system as us
import sverchok.core.group_update_system as gus


update_systems = [us.control_center, gus.control_center]


def handle_event(event):
"""Main control center
1. preprocess the event
2. Pass the event to update system(s)"""
# print(f"{event=}")

# something changed in scene
if type(event) is ev.SceneEvent:
# this event was caused by update system itself and should be ignored
if 'SKIP_UPDATE' in event.tree:
del event.tree['SKIP_UPDATE']
return

was_handled = dict()
for handler in update_systems:
res = handler(event)
was_handled[handler] = res

if (results := sum(was_handled.values())) > 1:
duplicates = [f.__module__ for f, r in was_handled if r == 1]
raise RuntimeError(f"{event=} was executed more than one time, {duplicates=}")
elif results == 0:
raise RuntimeError(f"{event} was not handled")
134 changes: 70 additions & 64 deletions core/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,91 @@
# License-Filename: LICENSE

"""
Purpose of this module is centralization of update events.
For now it can be used in debug mode for understanding which event method are triggered by Blender
during evaluation of Python code.
Details: https://github.com/nortikin/sverchok/issues/3077
Events keep information about which Blender trigger was executed and with which
context
"""

from __future__ import annotations

from collections.abc import Iterable
from enum import Enum, auto
from typing import Union, List, TYPE_CHECKING
from typing import Union, TYPE_CHECKING

from bpy.types import Node

if TYPE_CHECKING:
from sverchok.core.node_group import SvGroupTree, SvGroupTreeNode
from sverchok.node_tree import SverchCustomTreeNode, SverchCustomTree
SvNode = Union[SverchCustomTreeNode, SvGroupTreeNode, Node]
from sverchok.core.node_group import SvGroupTree as GrTree, \
SvGroupTreeNode as GrNode
from sverchok.node_tree import SverchCustomTreeNode, SverchCustomTree as SvTree
SvNode = Union[SverchCustomTreeNode, GrNode, Node]


class TreeEvent:
TREE_UPDATE = 'tree_update' # some changed in a tree topology
NODES_UPDATE = 'nodes_update' # changes in node properties, update animated nodes
FORCE_UPDATE = 'force_update' # rebuild tree and reevaluate every node
FRAME_CHANGE = 'frame_change' # unlike other updates this one should be un-cancellable
SCENE_UPDATE = 'scene_update' # something was changed in the scene

def __init__(self, event_type: str, tree: SverchCustomTree, updated_nodes: Iterable[SvNode] = None, cancel=True):
self.type = event_type
"""Adding removing nodes or links but not necessarily"""
# the event should be run via timer only https://developer.blender.org/T82318#1053877
tree: SvTree

def __init__(self, tree):
self.tree = tree
self.updated_nodes = updated_nodes
self.cancel = cancel

def __repr__(self):
return f"<TreeEvent type={self.type}>"


class GroupEvent:
GROUP_NODE_UPDATE = 'group_node_update'
GROUP_TREE_UPDATE = 'group_tree_update'
NODES_UPDATE = 'nodes_update'
EDIT_GROUP_NODE = 'edit_group_node' # upon pressing edit button or Tab

def __init__(self,
event_type: str,
group_nodes_path: List[SvGroupTreeNode],
updated_nodes: List[SvNode] = None):
self.type = event_type
self.group_node = group_nodes_path[-1]
self.group_nodes_path = group_nodes_path
return f"<{type(self).__name__} {self.tree.name=}>"


class ForceEvent(TreeEvent):
"""Indicates the whole tree should be recalculated"""
pass


class AnimationEvent(TreeEvent):
"""Frame was changed. Last event can be with the same frame"""
is_frame_changed: bool
is_animation_playing: bool

def __init__(self, tree, is_frame_change, is_animation_laying):
super().__init__(tree)
self.is_frame_changed = is_frame_change
self.is_animation_playing = is_animation_laying


class SceneEvent(TreeEvent):
"""Something was changed in the scene"""
pass


class PropertyEvent(TreeEvent):
"""Property of the node(s) was changed"""
updated_nodes: Iterable[SvNode]

def __init__(self, tree, updated_nodes):
super().__init__(tree)
self.updated_nodes = updated_nodes
self.to_update = group_nodes_path[-1].is_active

@property
def tree(self) -> SvGroupTree:
return self.group_node.node_tree

def __repr__(self):
return f'{self.type.upper()} event, GROUP_NODE={self.group_node.name}, TREE={self.tree.name}' \
+ (f', NODES={self.updated_nodes}' if self.updated_nodes else '')


class BlenderEventsTypes(Enum):
tree_update = auto() # this updates is calling last with exception of creating new node
node_update = auto() # it can be called last during creation new node event
add_node = auto() # it is called first in update wave
copy_node = auto() # it is called first in update wave
free_node = auto() # it is called first in update wave
add_link_to_node = auto() # it can detects only manually created links
node_property_update = auto() # can be in correct in current implementation
undo = auto() # changes in tree does not call any other update events
frame_change = auto()

def print(self, updated_element=None):
event_name = f"EVENT: {self.name: <30}"
if updated_element is not None:
element_data = f"IN: {updated_element.bl_idname: <25} INSTANCE: {updated_element.name: <25}"
else:
element_data = ""
print(event_name + element_data)
class GroupTreeEvent(TreeEvent):
"""The same as Tree event but inside a group tree"""
tree: GrTree
update_path: list[GrNode]

def __init__(self, tree, update_path):
super().__init__(tree)
self.update_path = update_path


class GroupPropertyEvent(GroupTreeEvent):
"""Property of a node(s) inside a group tree was changed"""
updated_nodes: Iterable[SvNode]

def __init__(self, tree, update_path, update_nodes):
super().__init__(tree, update_path)
self.updated_nodes = update_nodes


class FileEvent:
"""It indicates that new file was loaded"""
pass


class TreesGraphEvent:
"""It indicates that something was changed in trees relations defined via
group nodes"""
pass
Loading

0 comments on commit ed954eb

Please sign in to comment.