Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active April 3, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/0cb074c06ec27ef498765e6393cffab1 to your computer and use it in GitHub Desktop.
Save zeffii/0cb074c06ec27ef498765e6393cffab1 to your computer and use it in GitHub Desktop.
import string
import bpy
import bgl
import blf
from bpy.types import SpaceNodeEditor
from bpy.props import IntProperty, FloatProperty
### ---- lazy setup ---------------------------------------------------------------
verbose_nums = "ZERO ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE".split(" ")
verbose_numpads = [('NUMPAD_' + n) for n in string.digits]
CAPS = set(a for a in string.ascii_uppercase)
NUMS = set(verbose_nums)
NUMS2 = set(verbose_numpads)
NUMS = NUMS.union(NUMS2)
SPECIALS = {'BACK_SPACE','UP','DOWN','LEFT', 'RIGHT'}
KEYBOARD = CAPS.union(SPECIALS)
KEYBOARD = KEYBOARD.union(NUMS)
remap_nums = {k: str(idx) for idx, k in enumerate(verbose_nums)}
remap_extras = {k: str(idx) for idx, k in enumerate(verbose_numpads)}
remap_nums.update(remap_extras)
def draw_callback_px(self, context):
font_id = 0 # XXX, need to find out how best to get this.
# draw some text
bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
blf.position(font_id, 15, 30, 0)
blf.size(font_id, 20, 72)
blf.draw(font_id, self.search_string['content'])
# restore opengl defaults
bgl.glLineWidth(1)
bgl.glDisable(bgl.GL_BLEND)
bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
class ModalOperator(bpy.types.Operator):
"""Move an object with the mouse, example"""
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"
current_string = bpy.props.StringProperty()
def modal(self, context, event):
context.area.tag_redraw()
if event.type in KEYBOARD and event.value == 'PRESS':
print(event.type)
if event.type in CAPS or event.type in remap_nums.keys():
final_value = remap_nums.get(event.type, event.type.lower())
self.current_string = self.current_string + final_value
elif event.type == 'BACK_SPACE':
has_length = len(self.current_string)
self.current_string = self.current_string[:-1] if has_length else ''
print(self.current_string)
self.search_string['content'] = self.current_string
elif event.type in {'LEFTMOUSE', 'RET'}:
print('completed')
SpaceNodeEditor.draw_handler_remove(self._handle, 'WINDOW')
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
SpaceNodeEditor.draw_handler_remove(self._handle, 'WINDOW')
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.area.type == 'NODE_EDITOR':
args = (self, context)
self._handle = SpaceNodeEditor.draw_handler_add(draw_callback_px, args, 'WINDOW', 'POST_VIEW')
self.search_string = {}
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "NODE_EDITOR not found, cannot run operator")
return {'CANCELLED'}
def register():
bpy.utils.register_class(ModalOperator)
def unregister():
bpy.utils.unregister_class(ModalOperator)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment