Instantly share code, notes, and snippets.
Last active
August 2, 2019 13:53
-
Save tin2tin/b4be91df8b7001cd3a07439ed3378427 to your computer and use it in GitHub Desktop.
Text Editor Pie Menu by Lapineige from here: https://blenderartists.org/t/addon-auto-completion-in-blenders-text-editor/627858/13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# ##### BEGIN GPL LICENSE BLOCK ##### | |
# | |
# This program is free software; you can redistribute it and/or | |
# modify it under the terms of the GNU General Public License | |
# as published by the Free Software Foundation; either version 2 | |
# of the License, or (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software Foundation, | |
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
# | |
# ##### END GPL LICENSE BLOCK ##### | |
bl_info = { | |
"name": "Text Editor Pie Menu", | |
"description": "A Pie Menu for the Text Editor", | |
"author": "Lapineige, tintwotin", | |
"version": (0, 1, 0), | |
"blender": (2, 80, 0), | |
"location": "keyboard", | |
"warning": "Press Ctrl W to use the pie.", | |
"wiki_url": "", | |
"category": "Text Editor" | |
} | |
import bpy | |
from re import search | |
from bpy.types import Menu | |
def changeText(self, context): | |
bpy.context.space_data.text = bpy.data.texts[bpy.context.scene.tmp_text] | |
bpy.types.Scene.tmp_text = bpy.props.EnumProperty(items=[(txt.name, txt.name, '', '', bpy.data.texts.find(txt.name)) for txt in bpy.data.texts], update=changeText) | |
class TEXT_MT_pie(Menu): | |
# label is displayed at the center of the pie menu. | |
bl_label = "Text Editor Pie" | |
bl_idname = "text.text_editor_pie" | |
@classmethod | |
def poll(cls, context): | |
return context.space_data.type == 'TEXT_EDITOR' | |
def draw(self, context): | |
layout = self.layout | |
pie = layout.menu_pie() | |
pie.operator('text.unindent') | |
pie.operator('text.better_indent', text='Indent') | |
pie.prop(context.space_data, 'show_word_wrap', text='Wrap') | |
pie.operator('text.run_script') | |
pie.operator('text.better_uncomment', text='Uncomment') | |
pie.operator('text.better_comment', text='Comment') | |
pie.operator('text.select_line') | |
pie.menu(menu='text.text_list_menu', text='Choose Text') | |
class TEXT_MT_list(bpy.types.Menu): | |
bl_label = "Text List" | |
bl_idname = "text.text_list_menu" | |
def draw(self, context): | |
layout = self.layout | |
layout.props_enum(context.scene, 'tmp_text') | |
class BetterComment(bpy.types.Operator): | |
""" """ | |
bl_idname = "text.better_comment" | |
bl_label = "Better Comment" | |
def execute(self, context): | |
base = context.space_data.text.current_line.body | |
bpy.ops.text.comment() | |
if context.space_data.text.current_line.body == base: | |
context.space_data.text.current_line.body = '#' + context.space_data.text.current_line.body | |
return {'FINISHED'} | |
class BetterUnComment(bpy.types.Operator): | |
""" """ | |
bl_idname = "text.better_uncomment" | |
bl_label = "Better Uncomment" | |
def execute(self, context): | |
base = context.space_data.text.current_line.body | |
bpy.ops.text.uncomment() | |
if '#' in base: | |
if context.space_data.text.current_line.body == base and search(r'[^#]', base).start() > base.index('#'): | |
context.space_data.text.current_line.body = base[search(r'[^#]', base).start():] | |
return {'FINISHED'} | |
class BetterIndent(bpy.types.Operator): | |
""" """ | |
bl_idname = "text.better_indent" | |
bl_label = "Better Indent" | |
def execute(self, context): | |
base = context.space_data.text.current_line.body | |
bpy.ops.text.indent() | |
if search(r'[^ ]', context.space_data.text.current_line.body).start() == search(r'[^ ]', base).start(): | |
context.space_data.text.current_line.body = ' '*context.space_data.tab_width + base | |
return {'FINISHED'} | |
addon_keymaps = [] | |
def register(): | |
bpy.utils.register_class(TEXT_MT_pie) | |
bpy.utils.register_class(TEXT_MT_list) | |
bpy.utils.register_class(BetterComment) | |
bpy.utils.register_class(BetterUnComment) | |
bpy.utils.register_class(BetterIndent) | |
wm = bpy.context.window_manager | |
kc = bpy.context.window_manager.keyconfigs.addon | |
if wm.keyconfigs.addon: | |
km = wm.keyconfigs.addon.keymaps.new(name = "Window",space_type='EMPTY', region_type='WINDOW') | |
kmi = km.keymap_items.new('wm.call_menu_pie', 'W', 'PRESS', ctrl=True) | |
kmi.properties.name = TEXT_MT_pie.bl_idname | |
addon_keymaps.append((km,kmi)) | |
def unregister(): | |
bpy.utils.unregister_class(TEXT_MT_pie) | |
bpy.utils.unregister_class(TEXT_MT_list) | |
bpy.utils.unregister_class(BetterComment) | |
bpy.utils.unregister_class(BetterUnComment) | |
bpy.utils.unregister_class(BetterIndent) | |
wm = bpy.context.window_manager | |
kc = wm.keyconfigs.addon | |
if kc: | |
for km, kmi in addon_keymaps: | |
km.keymap_items.remove(kmi) | |
addon_keymaps.clear() | |
if __name__ == "__main__": | |
register() | |
##bpy.ops.wm.call_menu_pie(name=TEXT_MT_pie.bl_idname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment