Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/text_editor_Plugins.py
Created May 1, 2014 16:17
Show Gist options
  • Save zeffii/a0af0f106060b458f893 to your computer and use it in GitHub Desktop.
Save zeffii/a0af0f106060b458f893 to your computer and use it in GitHub Desktop.
import bpy
import os
from bpy.props import EnumProperty, StringProperty
import re
def has_selection(self, text):
return not (text.select_end_line == text.current_line and
text.current_character == text.select_end_character)
def converted(test_str):
if not "def sv_main(" in test_str:
return
r = re.compile('(?P<name>\w+)=(?P<defval>.*?|\[\])[,\)]')
k = [m.groupdict() for m in r.finditer(test_str)]
# print(k)
# convert dict
socket_mapping = {
'[]': 'v'
# assume more will follow
}
indent = " "
socket_members = []
for variable in k:
stype = variable['defval']
sname = variable['name']
shorttype = socket_mapping.get(stype, 's')
list_item = str([shorttype, sname, {0}])
l = list_item.format(sname)
socket_members.append(indent*2 + l)
socket_items = ",\n".join(socket_members)
declaration = "\n" + indent + 'in_sockets = [\n'
declaration += socket_items
declaration += "]"
return declaration
class SvVarnamesToSockets(bpy.types.Operator):
bl_label = ""
bl_idname = "txt.varname_rewriter"
def execute(self, context):
bpy.ops.text.select_line()
bpy.ops.text.copy()
copied_text = bpy.data.window_managers[0].clipboard
answer = converted(copied_text)
if answer:
print(answer)
bpy.data.window_managers[0].clipboard = answer
bpy.ops.text.move(type='LINE_BEGIN')
bpy.ops.text.move(type='NEXT_LINE')
bpy.ops.text.paste()
return {'FINISHED'}
class BasicTextMenu(bpy.types.Menu):
bl_idname = "TEXT_MT_svplug_menu"
bl_label = "Plugin Menu"
# @classmethod
# def poll(self, context):
# text = bpy.context.edit_text
# return has_selection(self, text)
def draw(self, context):
layout = self.layout
text = bpy.context.edit_text
no_selection = (text.current_character == text.select_end_character)
if no_selection:
layout.operator("txt.varname_rewriter", text='generate in_sockets')
# Sets the keymap to Ctrl + I for inside the text editor, will only
# appear if a selection is set.
if True:
wm = bpy.context.window_manager
km = wm.keyconfigs.default.keymaps['Text']
new_shortcut = km.keymap_items.new(
'wm.call_menu', 'I', 'PRESS', ctrl=True)
#new_shortcut = km.keymap_items.new('wm.call_menu', 'I', 'PRESS', ctrl=True)
new_shortcut.properties.name = 'TEXT_MT_svplug_menu'
def register():
bpy.utils.register_class(SvVarnamesToSockets)
bpy.utils.register_class(BasicTextMenu)
def unregister():
bpy.utils.unregister_class(SvVarnamesToSockets)
bpy.utils.unregister_class(BasicTextMenu)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment