Was playing around with your script to put it onto the right click menu …
# ##### 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': "Class Viewer",
'author': "Mackraken",
'version': (0, 0, 2),
'blender': (2, 5, 6),
'api': 35566,
'location': "Text Editor > Toolbar",
'warning': "",
'description': "List text's classes and definitions",
'wiki_url': "mailto:[email protected]",
'category': "Development"}
import bpy
def getfunc(space, tipo="def "):
text = space.text
defs = []
for i, l in enumerate(text.lines):
line = l.body
if line[0:len(tipo)]==tipo:
func = line[len(tipo):line.find("(")]
print(tipo, func)
defs.append([func, i+1])
return defs
def update(items, col):
for i in range(0, len(col)):
col.remove(0)
for i, item in enumerate(items):
if len(col)<i+1:
col.add()
col[i].name = item[0]
col[i].myint = item[1]
class PropertyGroup(bpy.types.PropertyGroup):
pass
bpy.utils.register_class(PropertyGroup)
## create CollectionProperty and link it to the property class
bpy.types.Scene.myCollection = bpy.props.CollectionProperty(type = PropertyGroup)
bpy.types.Scene.myCollection_index = bpy.props.IntProperty(min = -1, default = -1)
bpy.types.Scene.classCollection = bpy.props.CollectionProperty(type = PropertyGroup)
bpy.types.Scene.classCollection_index = bpy.props.IntProperty(min = -1, default = -1)
## create Properties for the collection entries:
PropertyGroup.mystring = bpy.props.StringProperty()
PropertyGroup.myint = bpy.props.IntProperty(default = False)
class OBJECT_OT_Jumptoline(bpy.types.Operator):
bl_label = "Jump"
bl_idname = "collection.jump"
__doc__ = "Simple Custom Button"
line = bpy.props.IntProperty(default=-1)
add = bpy.props.BoolProperty(default=True)
@classmethod
def poll(cls, context):
return context.area.active_space.text!=None
def execute(self, context):
scn = context.scene
if self.line >= 0:
return bpy.ops.text.jump(line=self.line)
add = self.add
if add:
collection = scn.myCollection
index = scn.myCollection_index
else:
collection = scn.classCollection
index = scn.classCollection_index
if index<len(collection):
num = collection[index].myint
bpy.ops.text.jump(line=num)
return {'FINISHED'}
## create operator to add or remove entries to/from the Collection
class OBJECT_OT_add_remove_Collection_Items(bpy.types.Operator):
bl_label = "Add or Remove"
bl_idname = "collection.add_remove"
__doc__ = "Simple Custom Button"
add = bpy.props.BoolProperty(default = True)
@classmethod
def poll(cls, context):
return context.area.active_space.text!=None
def invoke(self, context, event):
add = self.add
scn = context.scene
# updates the list
space = context.area.active_space
if add:
collection = scn.myCollection
defs = getfunc(space, "def ")
update(defs, collection)
else:
collection = scn.classCollection
classes = getfunc(space, "class ")
update(classes, collection)
return {'FINISHED'}
class OBJECT_PT_defViewer(bpy.types.Panel):
bl_label = "Def Viewer"
bl_space_type = "TEXT_EDITOR"
bl_region_type = "UI"
#bl_context = "object"
def draw(self, context):
scn = context.scene
layout = self.layout
##show collection in Panel:
row = layout.row()
row.label("defs")
row.operator("collection.jump")
row.operator("collection.add_remove", text="Update")
row = layout.row()
row.template_list(scn, "myCollection", scn, "myCollection_index")
class OBJECT_PT_ClassViewer(bpy.types.Panel):
bl_label = "Class Viewer"
bl_space_type = "TEXT_EDITOR"
bl_region_type = "UI"
#bl_context = "object"
def draw(self, context):
scn = context.scene
layout = self.layout
row = layout.row()
row.label("classes")
row.operator("collection.jump").add=False
row.operator("collection.add_remove", text="Update").add=False
row = layout.row()
row.template_list(scn, "classCollection", scn, "classCollection_index")
class ClassMenu(bpy.types.Menu):
bl_idname = "OBJECT_MT_select_class"
bl_label = "Select"
def draw(self, context):
scn = context.scene
layout = self.layout
for x in context.scene.classCollection:
layout.operator("collection.jump",text=x.name).line = x.myint
def GotoClasses(self, context):
self.layout.menu("OBJECT_MT_select_class", text="Classes", icon='PLUGIN')
classes = [OBJECT_PT_defViewer,
OBJECT_PT_ClassViewer,
OBJECT_OT_add_remove_Collection_Items,
ClassMenu,
OBJECT_OT_Jumptoline]
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.TEXT_MT_toolbox.append(GotoClasses)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
bpy.types.TEXT_MT_toolbox.append(GotoClasses)
if __name__ == "__main__":
register()