[Addon] Quickly look up text in Python API

Hi,

this simple addon is supposed to speed up looking up certain blender function/class/property names in the Python API from within the text editor. Helpful for addon coders and script writers.

You need to select some text and press the button (under Utilities panel, or in the Edit Menu or the Context Menu of the Text Editor of blender.)

Should work in all blender versions >= 2.80.0, theoretically also in older versions (given the older blender docs are reachable under the same base url. :slight_smile:

bl_info = {
    "name": "Python API Text Lookup",
    "author": "scorpion81",
    "version": (1, 1),
    "blender": (2, 80, 0),
    "location": "Text Editor > Text > Utilities > Find Text in Python API",
    "description": "Finds Text in Python API",
    "warning": "",
    "doc_url": "",
    "category": "Text Editor",
}

import bpy

class UtilityPanel(bpy.types.Panel):
    """Utility Panel in the Text Editor ui region"""
    bl_label = "Utilities"
    bl_idname = "TEXT_PT_utility"
    bl_space_type = 'TEXT_EDITOR'
    bl_region_type = 'UI'
    bl_context = "text"
    bl_category = "Text"

    def draw(self, context):
        layout = self.layout
        layout.operator("text.api_lookup", text="Find Text in Python API")
        layout.operator("screen.userpref_show")

class APILookupOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "text.api_lookup"
    bl_label = "API Lookup Operator"
    
    def selected_text(self, context):
        #extract selected string from currently open text
        #from current line till select_end_line
        #from current_character till select_end_character
        #concat
        text = context.edit_text
        body = ''
        cs = text.current_character
        ce = text.select_end_character
        ls = text.current_line_index
        le = text.select_end_line_index
        #print(ls, le, cs, ce)
        
        for li in range(ls,le+1):
            l = text.lines[li]
            if ls == le:
                body += l.body[cs:ce]
            elif li == ls:
                body += l.body[cs:]
            elif li == le:
                body += l.body[:ce]             
            else:
                body += l.body
                
        return body

    def execute(self, context):
      
        text = self.selected_text(context)
        #print(text)
        
        #open browser (2.8+ ?)
        #v = (2, 80, 0) 
        v = bpy.app.version
        version = str(v[0]) + "." + str(v[1])
        base_url = 'https://docs.blender.org/api/'+version+'/'
        url = base_url+'search.html?q='+text+'&check_keywords=yes&area=default'
        bpy.ops.wm.url_open(url=url)
        
        return {'FINISHED'}


def menu_draw(self, context):
    self.layout.separator()
    self.layout.operator("text.api_lookup", text="Find Text in Python API")

def register():
    bpy.utils.register_class(APILookupOperator)
    bpy.utils.register_class(UtilityPanel)
    bpy.types.TEXT_MT_context_menu.append(menu_draw)
    bpy.types.TEXT_MT_edit.append(menu_draw)


def unregister():
    bpy.types.TEXT_MT_edit.remove(menu_draw)
    bpy.types.TEXT_MT_context_menu.remove(menu_draw)
    bpy.utils.unregister_class(UtilityPanel)
    bpy.utils.unregister_class(APILookupOperator)
    
if __name__ == "__main__":
    register()

text_utilities.py (2.7 KB)

6 Likes

Maybe you would like to add a link in this thread? Essential Text Editor add-ons for coders

NOTE: at least in 2.91 the text you want to search has to be the only text on the line and it needs to be highlighted. Otherwise it will not send anything for searching.

Hey @JTA thanks for the finding. Indeed, if you select from “back to forth”, the order of the selected lines or characters is wrong lol… Odd why this isnt sorted already in the API, but added a small fix for this in 1.2, ordering the start and end values accordingly.

bl_info = {
    "name": "Python API Text Lookup",
    "author": "scorpion81",
    "version": (1, 2),
    "blender": (2, 80, 0),
    "location": "Text Editor > Text > Utilities > Find Text in Python API",
    "description": "Finds Text in Python API",
    "warning": "",
    "doc_url": "",
    "category": "Text Editor",
}

import bpy

class UtilityPanel(bpy.types.Panel):
    """Utility Panel in the Text Editor ui region"""
    bl_label = "Utilities"
    bl_idname = "TEXT_PT_utility"
    bl_space_type = 'TEXT_EDITOR'
    bl_region_type = 'UI'
    bl_context = "text"
    bl_category = "Text"

    def draw(self, context):
        layout = self.layout
        layout.operator("text.api_lookup", text="Find Text in Python API")
        layout.operator("screen.userpref_show")

class APILookupOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "text.api_lookup"
    bl_label = "API Lookup Operator"
    
    def selected_text(self, context):
        #extract selected string from currently open text
        #from current line till select_end_line
        #from current_character till select_end_character
        #concat
        text = context.edit_text
        body = ''
        cs = text.current_character
        ce = text.select_end_character
        ls = text.current_line_index
        le = text.select_end_line_index
        #print(ls, le, cs, ce)
        
        #end of selection can be smaller than beginning, so keep correct order
        if ce < cs:
            tmp = ce
            ce = cs
            cs = tmp
            
        if le < ls:
            tmp = le
            le = ls
            ls = tmp
        
        for li in range(ls,le+1):
            l = text.lines[li]
            if ls == le:
                body += l.body[cs:ce]
            elif li == ls:
                body += l.body[cs:]
            elif li == le:
                body += l.body[:ce]             
            else:
                body += l.body
                
        return body

    def execute(self, context):
      
        text = self.selected_text(context)
        #print(text)
        
        #open browser (2.8+ ?)
        #v = (2, 80, 0) 
        v = bpy.app.version
        version = str(v[0]) + "." + str(v[1])
        base_url = 'https://docs.blender.org/api/'+version+'/'
        url = base_url+'search.html?q='+text+'&check_keywords=yes&area=default'
        bpy.ops.wm.url_open(url=url)
        
        return {'FINISHED'}


def menu_draw(self, context):
    self.layout.separator()
    self.layout.operator("text.api_lookup", text="Find Text in Python API")

def register():
    bpy.utils.register_class(APILookupOperator)
    bpy.utils.register_class(UtilityPanel)
    bpy.types.TEXT_MT_context_menu.append(menu_draw)
    bpy.types.TEXT_MT_edit.append(menu_draw)


def unregister():
    bpy.types.TEXT_MT_edit.remove(menu_draw)
    bpy.types.TEXT_MT_context_menu.remove(menu_draw)
    bpy.utils.unregister_class(UtilityPanel)
    bpy.utils.unregister_class(APILookupOperator)
    
if __name__ == "__main__":
    register()

text_utilities.py (3.0 KB)

2 Likes

Thanks Scorp! You’re the best!