Sverchok / SverchokRedux

A reimplementation of Sverchok
GNU General Public License v3.0
4 stars 2 forks source link

Auto population of template menu items for Script nodes #26

Closed zeffii closed 8 years ago

zeffii commented 8 years ago
import bpy

class TEXT_MT_templates_my_py(bpy.types.Menu):
    bl_label = "My Python"
    bl_idname = "OBJECT_MT_custom_py_menu"

    def draw(self, context):
        self.path_menu(
            bpy.utils.script_paths("templates_my_py"),
            "text.open",
            {"internal": True},
        )

def draw_item(self, context):
    layout = self.layout
    layout.menu(TEXT_MT_templates_my_py.bl_idname)

def register():
    bpy.utils.register_module(__name__)
    bpy.types.TEXT_MT_templates.append(draw_item)

def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.TEXT_MT_templates.remove(draw_item)

if __name__ == "__main__":
    register()

As long as the first argument to self.path_menu() is a list ["/home/blablab/dir"], it will expose all .py files in that dir as menu items.

ly29 commented 8 years ago

These small snippets are really useful.

nortikin commented 8 years ago

great idea!

zeffii commented 8 years ago

I haven tried this but I think this also allows for other uses, because this example calls the
bpy.ops.text.open(internal=True, filepath=whatever_was_clicked)

so if we made an operator that can load scripts as nodes then our def draw might include a line like

        bpy.utils.script_paths("templates_my_py"),
        "svrx.load_as_node",
        {"internal": True},

this would load as node and also import the text file to a text datablock , if it handled the "internal" parameter. :)

if not, we can make a util function that does do it this way :)