Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snlite newnode operator #4543

Merged
merged 5 commits into from
Jun 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 88 additions & 4 deletions utils/text_editor_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import bpy
from sverchok.utils.logging import getLogger

from sverchok.settings import get_params

def has_selection(self, text):
return not (text.select_end_line == text.current_line and
Expand All @@ -25,6 +25,84 @@ def fuzzy_compare(named_seeker, named_current):
except Exception as err:
print(f"Refresh Current Script called but encountered error {err}")

def get_first_sverchok_nodetree():

AREA = 'NODE_EDITOR'
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if not area.type == AREA: continue

for space in area.spaces:
if hasattr(space, "edit_tree"):
if space.tree_type in {'SverchCustomTreeType', 'SvGroupTree'}:
ng = space.edit_tree
for region in area.regions:
if region.type == 'WINDOW':
override = {
'window': window,
'screen': screen,
'area': area,
'region': region
}
return ng, override, space



class SvSNLiteAddFromTextEditor(bpy.types.Operator):
"""
you want to create an snlite from the current edit_text in TextEditor
this will create the node, load it with the text, and center the nodeview on that node.
"""
bl_label = "Make SNlite from Current Script"
bl_idname = "text.nodenew_from_texteditor"

def execute(self, context):
self.report({'INFO'}, f"Triggered: {self.bl_idname}")

ngs = bpy.data.node_groups
if not ngs:
self.report({'INFO'}, "No NodeGroups")
return {'FINISHED'}

edit_text = bpy.context.edit_text
text_file_name = edit_text.name
is_sv_tree = lambda ng: ng.bl_idname in {'SverchCustomTreeType', }
ngs = list(filter(is_sv_tree, ngs))

if not ngs:
self.report({'INFO'}, "No Sverchok NodeGroups")
return {'CANCELLED'}

if (result := get_first_sverchok_nodetree()):
ng, override, space = result

for n in ng.nodes:
if n.bl_idname == "SvScriptNodeLite":
if fuzzy_compare(n.script_name, text_file_name):
n.load()
return {'CANCELLED'}

snlite = ng.nodes.new('SvScriptNodeLite')

# middle of view, translated to nodetree location
dpi_fac = get_params({'render_location_xy_multiplier': 1.0}, direct=True)[0]
region = override['region']
mid_x = region.width / 2
mid_y = region.height / 2
print("mid==", mid_x, mid_y)
x, y = region.view2d.region_to_view(mid_x, mid_y)
snlite.location = x * 1 / dpi_fac, y *1 / dpi_fac

snlite.script_name = text_file_name
snlite.load()

#ng.nodes.active = snlite
#snlite.select = True
#bpy.ops.node.view_selected(override)

return {'FINISHED'}


class SvNodeRefreshFromTextEditor(bpy.types.Operator):

Expand All @@ -33,7 +111,7 @@ class SvNodeRefreshFromTextEditor(bpy.types.Operator):

def execute(self, context):

self.report({'INFO'}, "Triggered: text.noderefresh_from_texteditor")
self.report({'INFO'}, f"Triggered: {self.bl_idname}")

ngs = bpy.data.node_groups
if not ngs:
Expand Down Expand Up @@ -119,11 +197,16 @@ def add_keymap():

if 'noderefresh_from_texteditor' in dir(bpy.ops.text):
''' SHORTCUT 1 Node Refresh: Ctrl + Return '''
ident_str = 'text.noderefresh_from_texteditor'
ident_str = SvNodeRefreshFromTextEditor.bl_idname
if not (ident_str in keymaps):
new_shortcut = keymaps.new(ident_str, 'RET', 'PRESS', ctrl=1, head=0)
addon_keymaps.append((km, new_shortcut))

ident_str = SvSNLiteAddFromTextEditor.bl_idname
if not (ident_str in keymaps):
new_shortcut = keymaps.new(ident_str, 'RET', 'PRESS', ctrl=1, shift=1, head=0)
addon_keymaps.append((km, new_shortcut))

logger.debug('Sverchok added keyboard items to Text Editor.')


Expand All @@ -136,11 +219,12 @@ def remove_keymap():

def register():
global logger
bpy.utils.register_class(SvSNLiteAddFromTextEditor)
bpy.utils.register_class(SvNodeRefreshFromTextEditor)
logger = getLogger("text_editor_plugins")
add_keymap()

def unregister():
remove_keymap()
bpy.utils.unregister_class(SvNodeRefreshFromTextEditor)

bpy.utils.unregister_class(SvSNLiteAddFromTextEditor)