Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created January 25, 2018 19:00
Show Gist options
  • Save zeffii/59b0f8a443e4afd49f2cf861ad0ffd78 to your computer and use it in GitHub Desktop.
Save zeffii/59b0f8a443e4afd49f2cf861ad0ffd78 to your computer and use it in GitHub Desktop.
class SverchPresetAddOperator(bpy.types.Operator):
bl_idname = "node.sv_preset_" + get_preset_idname_for_operator(self.name)
bl_label = "Add {} preset".format(self.name)
bl_options = {'REGISTER', 'UNDO'}
first_mouse_x = IntProperty()
first_mouse_y = IntProperty()
@classmethod
def poll(cls, context):
try:
return context.space_data.node_tree.bl_idname in {'SverchCustomTreeType', 'SverchGroupTreeType'}
except:
return False
def modal(operator, context, event):
if event.type == 'MOUSEMOVE':
# delta_x = operator.first_mouse_x - event.mouse_x
# delta_y = operator.first_mouse_y - event.mouse_y
# context.object.location.x = self.first_value + delta * 0.01
bpy.ops.node.translate_attach(TRANSFORM_OT_translate={"value":(event.mouse_x, event.mouse_y, 0)})
# bpy.ops.node.translate_attach(TRANSFORM_OT_translate={"value":(delta_x, -delta_y, 0)})
elif event.type == 'LEFTMOUSE':
bpy.ops.node.translate_attach(TRANSFORM_OT_translate={"value":(event.mouse_x, event.mouse_y, 0)})
# bpy.ops.node.translate_attach(TRANSFORM_OT_translate={"value":(delta_x, -delta_y, 0)})
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
# context.object.location.x = self.first_value
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def invoke(operator, context, event):
# please not be confused: "operator" here references to
# SverchPresetAddOperator instance, and "self" references to
# SvPreset instance.
ntree = context.space_data.node_tree
id_tree = ntree.name
ng = bpy.data.node_groups[id_tree]
if ng:
# Deselect everything, so as a result only imported nodes
# will be selected
operator.first_mouse_x = event.mouse_x
operator.first_mouse_y = event.mouse_y
bpy.ops.node.select_all(action='DESELECT')
import_tree(ng, self.path)
context.window_manager.modal_handler_add(operator)
return {'RUNNING_MODAL'}
else:
operator.report({'WARNING'}, "No active nodetree")
return {'CANCELLED'}
# bpy.ops.node.translate_attach(NODE_OT_insert_offset={"value":(77.3197, 37.8496, 0)})
return {'FINISHED'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment