Module sverchok.utils.sv_operator_mixins
Expand source code
# 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
import bpy
from bpy.props import StringProperty
class SvGenericNodeLocator():
"""
usage:
add this to an Operator class definition if you need to track the origin
of a click.
see at the bottom of this file in " class SvGenericCallbackOldOp " how to use it.
you can then use the "node.wrapper_tracked_ui_draw_op", in the UI draw function..
f.ex:
callback = "node.sverchok_mesh_baker_mk3"
self.wrapper_tracked_ui_draw_op(row, callback, icon='', text='')
"""
tree_name: StringProperty(default='', description="name of the node tree")
node_name: StringProperty(default='', description="name of the node")
def get_node(self, context):
""" context.node is usually provided, else tree_name/node_name must be passed """
if self.tree_name and self.node_name:
return bpy.data.node_groups[self.tree_name].nodes[self.node_name]
if hasattr(context, "node"):
return context.node
print("treename or nodename not supplied, node not found in available trees")
print(f"received tree_name: {tree_name} and node_name: {node_name}")
return None
def get_tree(self):
return bpy.data.node_groups.get(self.tree_name)
def sv_execute(self, context, node):
"""
you override this, inside this function you place the code you want to execute
if the locator finds the node.
- you can use a return statement to end the sv_execute function early.
- return can be one of two things. success or failure
success: (these are all equal)
return False,
return 0
return None
return
return {'FINISHED'}
failure:
return {'CANCELLED'} as a regular execute function
"""
pass
def execute(self, context):
node = self.get_node(context)
if node:
response = self.sv_execute(context, node)
if response:
return response
return {'FINISHED'}
msg = f'{self.bl_idname} was unable to locate the node <{self.tree_name}|{self.node_name}>'
self.report({'ERROR'}, msg)
return {'CANCELLED'}
class SvGenericCallbackWithParams():
bl_idname = "node.sv_generic_callback_with_params"
bl_label = "SvGeneric callback (with params)"
'''
#### using SvGenericCallbackWithParams(bpy.types.Operator) #####
class SomeNode..
def draw_buttons(self, context, layout):
callback = "node.sv_generic_callback_with_params"
my_op = layout.operator(callback, text='display_name').fn_name='some_function'
my_op.your_custom_param_name = 'something'
def some_function(self, operator):
print(operator.your_custom_param_name)
operator.report({ ...})
return {'FINISHED'}
'''
fn_name: bpy.props.StringProperty(default='')
def execute(self, context):
try:
f = getattr(context.node, self.fn_name)(self)
return f or {'FINISHED'}
except Exception as err:
print(repr(err))
return {'CANCELLED'}
# return just in case, else let the content of the called function decide the return value
return {'FINISHED'}
class SvGenericDirectorySelector():
bl_idname = "node.sv_generic_dir_selector"
bl_label = "sv Dir Select"
'''
#### using SvGenericDirectorySelector(bpy.types.Operator) #####
class SomeNode..
def draw_buttons(self, context, layout):
callback = "node.sv_generic_dir_selector"
my_op = layout.operator(callback, text='pick directory').fn_name='some_function'
def some_function(self, operator):
print(operator.path) <---- will contain full directory path selected from the dialogue
operator.report({ ...})
return {'FINISHED'}
'''
fn_name: bpy.props.StringProperty(default='')
directory: bpy.props.StringProperty(
name="Base Path",
description="Directory selected",
maxlen=1024, default="", subtype='DIR_PATH')
def execute(self, context):
node = self.node # definitely have the node here
try:
f = getattr(node, self.fn_name)(self)
return f or {'FINISHED'}
except Exception as err:
print(repr(err))
return {'CANCELLED'}
# return just in case, else let the content of the called function decide the return value
return {'FINISHED'}
def invoke(self, context, event):
self.node = context.node
wm = context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
class SvGenericCallbackOldOp(bpy.types.Operator, SvGenericNodeLocator):
"""
This operator is generic and will call .fn_name on the instance of the caller node
"""
bl_idname = "node.sverchok_generic_callback_old"
bl_label = "Sverchok text input"
bl_options = {'REGISTER', 'UNDO'}
fn_name: StringProperty(name='function name')
def execute(self, context):
n = self.get_node(context)
if not n:
return {'CANCELLED'}
f = getattr(n, self.fn_name, None)
if not f:
msg = f"{n.name} has no function named '{self.fn_name}'"
self.report({"WARNING"}, msg)
return {'CANCELLED'}
f()
return {'FINISHED'}
def register():
bpy.utils.register_class(SvGenericCallbackOldOp)
def unregister():
bpy.utils.unregister_class(SvGenericCallbackOldOp)
Functions
def register()
-
Expand source code
def register(): bpy.utils.register_class(SvGenericCallbackOldOp)
def unregister()
-
Expand source code
def unregister(): bpy.utils.unregister_class(SvGenericCallbackOldOp)
Classes
class SvGenericCallbackOldOp (...)
-
This operator is generic and will call .fn_name on the instance of the caller node
Expand source code
class SvGenericCallbackOldOp(bpy.types.Operator, SvGenericNodeLocator): """ This operator is generic and will call .fn_name on the instance of the caller node """ bl_idname = "node.sverchok_generic_callback_old" bl_label = "Sverchok text input" bl_options = {'REGISTER', 'UNDO'} fn_name: StringProperty(name='function name') def execute(self, context): n = self.get_node(context) if not n: return {'CANCELLED'} f = getattr(n, self.fn_name, None) if not f: msg = f"{n.name} has no function named '{self.fn_name}'" self.report({"WARNING"}, msg) return {'CANCELLED'} f() return {'FINISHED'}
Ancestors
- bpy_types.Operator
- builtins.bpy_struct
- SvGenericNodeLocator
Class variables
var bl_idname
var bl_label
var bl_options
var bl_rna
var fn_name : <_PropertyDeferred,
, {'name': 'function name', 'attr': 'fn_name'}>
Methods
def execute(self, context)
-
Expand source code
def execute(self, context): n = self.get_node(context) if not n: return {'CANCELLED'} f = getattr(n, self.fn_name, None) if not f: msg = f"{n.name} has no function named '{self.fn_name}'" self.report({"WARNING"}, msg) return {'CANCELLED'} f() return {'FINISHED'}
Inherited members
class SvGenericCallbackWithParams
-
Expand source code
class SvGenericCallbackWithParams(): bl_idname = "node.sv_generic_callback_with_params" bl_label = "SvGeneric callback (with params)" ''' #### using SvGenericCallbackWithParams(bpy.types.Operator) ##### class SomeNode.. def draw_buttons(self, context, layout): callback = "node.sv_generic_callback_with_params" my_op = layout.operator(callback, text='display_name').fn_name='some_function' my_op.your_custom_param_name = 'something' def some_function(self, operator): print(operator.your_custom_param_name) operator.report({ ...}) return {'FINISHED'} ''' fn_name: bpy.props.StringProperty(default='') def execute(self, context): try: f = getattr(context.node, self.fn_name)(self) return f or {'FINISHED'} except Exception as err: print(repr(err)) return {'CANCELLED'} # return just in case, else let the content of the called function decide the return value return {'FINISHED'}
Subclasses
- sverchok.nodes.list_mutators.polygon_sort.SvSetSortDirection
- sverchok.nodes.scene.FCurve_in.SvFCurveMK1CB
- sverchok.nodes.script.sn_functor_b.SvSNCallbackFunctorB
- sverchok.nodes.viz.viewer_texture.SvTextureViewerOperator
Class variables
var bl_idname
var bl_label
-
using SvGenericCallbackWithParams(bpy.types.Operator)
class SomeNode..
def draw_buttons(self, context, layout): callback = "node.sv_generic_callback_with_params" my_op = layout.operator(callback, text='display_name').fn_name='some_function' my_op.your_custom_param_name = 'something' def some_function(self, operator): print(operator.your_custom_param_name) operator.report({ ...}) return {'FINISHED'}
var fn_name : <_PropertyDeferred,
, {'default': '', 'attr': 'fn_name'}>
Methods
def execute(self, context)
-
Expand source code
def execute(self, context): try: f = getattr(context.node, self.fn_name)(self) return f or {'FINISHED'} except Exception as err: print(repr(err)) return {'CANCELLED'} # return just in case, else let the content of the called function decide the return value return {'FINISHED'}
class SvGenericDirectorySelector
-
Expand source code
class SvGenericDirectorySelector(): bl_idname = "node.sv_generic_dir_selector" bl_label = "sv Dir Select" ''' #### using SvGenericDirectorySelector(bpy.types.Operator) ##### class SomeNode.. def draw_buttons(self, context, layout): callback = "node.sv_generic_dir_selector" my_op = layout.operator(callback, text='pick directory').fn_name='some_function' def some_function(self, operator): print(operator.path) <---- will contain full directory path selected from the dialogue operator.report({ ...}) return {'FINISHED'} ''' fn_name: bpy.props.StringProperty(default='') directory: bpy.props.StringProperty( name="Base Path", description="Directory selected", maxlen=1024, default="", subtype='DIR_PATH') def execute(self, context): node = self.node # definitely have the node here try: f = getattr(node, self.fn_name)(self) return f or {'FINISHED'} except Exception as err: print(repr(err)) return {'CANCELLED'} # return just in case, else let the content of the called function decide the return value return {'FINISHED'} def invoke(self, context, event): self.node = context.node wm = context.window_manager wm.fileselect_add(self) return {'RUNNING_MODAL'}
Subclasses
- sverchok.nodes.viz.viewer_texture.SvTextureViewerDirSelect
Class variables
var bl_idname
var bl_label
-
using SvGenericDirectorySelector(bpy.types.Operator)
class SomeNode..
def draw_buttons(self, context, layout): callback = "node.sv_generic_dir_selector" my_op = layout.operator(callback, text='pick directory').fn_name='some_function' def some_function(self, operator): print(operator.path) <---- will contain full directory path selected from the dialogue operator.report({ ...}) return {'FINISHED'}
var directory : <_PropertyDeferred,
, {'name': 'Base Path', 'description': 'Directory selected', 'maxlen': 1024, 'default': '', 'subtype': 'DIR_PATH', 'attr': 'directory'}> var fn_name : <_PropertyDeferred,
, {'default': '', 'attr': 'fn_name'}>
Methods
def execute(self, context)
-
Expand source code
def execute(self, context): node = self.node # definitely have the node here try: f = getattr(node, self.fn_name)(self) return f or {'FINISHED'} except Exception as err: print(repr(err)) return {'CANCELLED'} # return just in case, else let the content of the called function decide the return value return {'FINISHED'}
def invoke(self, context, event)
-
Expand source code
def invoke(self, context, event): self.node = context.node wm = context.window_manager wm.fileselect_add(self) return {'RUNNING_MODAL'}
class SvGenericNodeLocator
-
usage: add this to an Operator class definition if you need to track the origin of a click.
see at the bottom of this file in " class SvGenericCallbackOldOp " how to use it. you can then use the "node.wrapper_tracked_ui_draw_op", in the UI draw function..
f.ex: callback = "node.sverchok_mesh_baker_mk3" self.wrapper_tracked_ui_draw_op(row, callback, icon='', text='')
Expand source code
class SvGenericNodeLocator(): """ usage: add this to an Operator class definition if you need to track the origin of a click. see at the bottom of this file in " class SvGenericCallbackOldOp " how to use it. you can then use the "node.wrapper_tracked_ui_draw_op", in the UI draw function.. f.ex: callback = "node.sverchok_mesh_baker_mk3" self.wrapper_tracked_ui_draw_op(row, callback, icon='', text='') """ tree_name: StringProperty(default='', description="name of the node tree") node_name: StringProperty(default='', description="name of the node") def get_node(self, context): """ context.node is usually provided, else tree_name/node_name must be passed """ if self.tree_name and self.node_name: return bpy.data.node_groups[self.tree_name].nodes[self.node_name] if hasattr(context, "node"): return context.node print("treename or nodename not supplied, node not found in available trees") print(f"received tree_name: {tree_name} and node_name: {node_name}") return None def get_tree(self): return bpy.data.node_groups.get(self.tree_name) def sv_execute(self, context, node): """ you override this, inside this function you place the code you want to execute if the locator finds the node. - you can use a return statement to end the sv_execute function early. - return can be one of two things. success or failure success: (these are all equal) return False, return 0 return None return return {'FINISHED'} failure: return {'CANCELLED'} as a regular execute function """ pass def execute(self, context): node = self.get_node(context) if node: response = self.sv_execute(context, node) if response: return response return {'FINISHED'} msg = f'{self.bl_idname} was unable to locate the node <{self.tree_name}|{self.node_name}>' self.report({'ERROR'}, msg) return {'CANCELLED'}
Subclasses
- sverchok.nodes.exchange.FCStd_read.SvReadFCStdOperator
- sverchok.nodes.exchange.FCStd_read.SvShowFcstdNamesOp
- sverchok.nodes.exchange.FCStd_read_mod.SvReadFCStdModOperator
- sverchok.nodes.exchange.FCStd_read_mod.SvShowFcstdNamesModOp
- sverchok.nodes.exchange.FCStd_sketch.SvReadFCStdSketchOperator
- sverchok.nodes.exchange.FCStd_spreadsheet.SvFCStdSpreadsheetOperator
- sverchok.nodes.exchange.FCStd_spreadsheet.SvShowFcstdParNamesOp
- sverchok.nodes.exchange.FCStd_spreadsheet.SvShowFcstdSpreadsheetsOp
- sverchok.nodes.exchange.FCStd_write.SvWriteFCStdOperator
- sverchok.nodes.exchange.approx_subd_to_nurbs.SvApproxSubdtoNurbsOperator
- sverchok.nodes.exchange.bezier_in.SvBezierInCallbackOp
- sverchok.nodes.exchange.nurbs_in.SvExNurbsInCallbackOp
- sverchok.nodes.list_mutators.multi_cache.SvvMultiCacheReset
- sverchok.nodes.logic.evolver.SvEvolverRun
- sverchok.nodes.logic.evolver.SvEvolverSetFittest
- sverchok.nodes.logic.genes_holder.SvGenesHolderReset
- sverchok.nodes.network.file_path.SvFilePathFinder
- sverchok.nodes.scene.get_objects_data.SvOB3BItemOperator
- sverchok.nodes.scene.get_objects_data.SvOB3Callback
- sverchok.nodes.scene.objects_in_lite.SvObjLiteCallback
- sverchok.nodes.solid.export_solid.SvExportSolidOperator
- sverchok.nodes.svg.svg_document.SvSVGWrite
- sverchok.nodes.svg.svg_document.SvSvgServer
- sverchok.nodes.viz.viewer_draw_curve.SvBakeCurveOp
- sverchok.nodes.viz.viewer_draw_surface.SvBakeSurfaceOp
- sverchok.nodes.viz.viewer_waveform_output.SvWaveformViewerOperator
- sverchok.nodes.viz.viewer_waveform_output.SvWaveformViewerOperatorDP
- sverchok.ui.nodeview_operators.SvNodeViewZoomBorder
- SvSnliteScriptSearch
- Sv3DviewAlign
- SvObjBakeMK3
- SvGenericCallbackOldOp
Class variables
var node_name : <_PropertyDeferred,
, {'default': '', 'description': 'name of the node', 'attr': 'node_name'}> var tree_name : <_PropertyDeferred,
, {'default': '', 'description': 'name of the node tree', 'attr': 'tree_name'}>
Methods
def execute(self, context)
-
Expand source code
def execute(self, context): node = self.get_node(context) if node: response = self.sv_execute(context, node) if response: return response return {'FINISHED'} msg = f'{self.bl_idname} was unable to locate the node <{self.tree_name}|{self.node_name}>' self.report({'ERROR'}, msg) return {'CANCELLED'}
def get_node(self, context)
-
context.node is usually provided, else tree_name/node_name must be passed
Expand source code
def get_node(self, context): """ context.node is usually provided, else tree_name/node_name must be passed """ if self.tree_name and self.node_name: return bpy.data.node_groups[self.tree_name].nodes[self.node_name] if hasattr(context, "node"): return context.node print("treename or nodename not supplied, node not found in available trees") print(f"received tree_name: {tree_name} and node_name: {node_name}") return None
def get_tree(self)
-
Expand source code
def get_tree(self): return bpy.data.node_groups.get(self.tree_name)
def sv_execute(self, context, node)
-
you override this, inside this function you place the code you want to execute if the locator finds the node.
- you can use a return statement to end the sv_execute function early.
-
return can be one of two things. success or failure
success: (these are all equal) return False, return 0 return None return return {'FINISHED'}
failure: return {'CANCELLED'} as a regular execute function
Expand source code
def sv_execute(self, context, node): """ you override this, inside this function you place the code you want to execute if the locator finds the node. - you can use a return statement to end the sv_execute function early. - return can be one of two things. success or failure success: (these are all equal) return False, return 0 return None return return {'FINISHED'} failure: return {'CANCELLED'} as a regular execute function """ pass