1
$\begingroup$

I think it's a stupid question and the answer may be right in front of my nose. I create a menu populated by the text_blocks of the Text Editor, asking which text I want.

Then.... how do I process the selection ?

Here's my menu :

class Choose_Text(bpy.types.Menu):
    bl_idname = "OBJECT_MT_select_file"
    bl_label = "Where is my text ?"

    def draw(self, context):
        layout = self.layout

        for i in bpy.data.texts:
            layout.operator("something", text=i.name)

I guess, all the menu items should lead to the same operator and transmit a variable, is that right ?

Thanks for any help !

$\endgroup$
1
$\begingroup$

The menu invokes an operator and this operator would need to receive the filename as a parameter.

I assembled a few lines (sorry for not renaming the classes) I'm sure this can be improved.

AnimMenu - iterates over all loaded text files and add them to a manu in the main header. The files name is handed over by a StringProperty.

AnimOperator - Is invoked when a menu item has been selected it reads the value of the property and shows how the filename is received and how to retrieve files content.

import bpy

text=""

class AnimOperator(bpy.types.Operator):
    bl_idname = "object.anim_operator"
    bl_label = "Pose Anim Operator"

    text_name = bpy.props.StringProperty(name="anim_text_name")
    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        print("invoked script %s" % text )
        print("invoked script name=%s" % self.text_name )
        txt = bpy.data.texts[ self.text_name ].as_string()
        print("do something with script")
        return {'FINISHED'}

class AnimMenu(bpy.types.Menu):
    bl_label = "Anim Menu"
    bl_idname = "OBJECT_MT_anim_menu"

    def draw(self, context):
        layout = self.layout
        for txt in bpy.data.texts:
            props = layout.operator("object.anim_operator", text=txt.name)
            props.text_name = txt.name


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


def register():
    bpy.utils.register_class(AnimOperator)
    bpy.utils.register_class(AnimMenu)
    bpy.types.INFO_HT_header.append(draw_item)

def unregister():
    bpy.utils.unregister_class(AnimMenu)
    bpy.types.INFO_HT_header.remove(draw_item)
    bpy.utils.unregister_class(AnimOperator)

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ Great explanation ! Thanks a ton ! I'll try it immediatly $\endgroup$ – Nikos_VSE Aug 28 '14 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.