2

I used a python template to create a menu which is populated by the names of files from the text editor:

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:
            if txt.name[-4:] =='.txt':
                layout.operator("object.anim_operator", text=txt.name).text_name = txt.name # from here

Another operator should receive the filename as a parameter

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

    text_name = bpy.props.StringProperty(name="anim_text_name") # to there

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        txt = bpy.data.texts[ text ].as_string()
        main(context, text_name)
        return {'FINISHED'}

I wasn't able to use this technique: How to set multiple custom attributes to operator class when called in UI layout?

props = layout.operator("object.anim_operator", text=txt.name)
props.text_name = txt.name

leads to:

AttributeError: 'OBJECT_OT_anim_operator' object has no attribute 'text_name'

stacker
  • 38,549
  • 31
  • 141
  • 243
  • The snippet actually seems to work. http://www.pasteall.org/52716/python – pink vertex Jul 09 '14 at 13:12
  • I don't know very much about Blender scripting, but I do know a bit of Python. I don't see "OBJECT_OT_anim_operator" anywhere in your code; the example you linked to defined a similarly named class. In both of your classes, the variable txt is initialized only in a method within the class. I'm a bit rusty when it comes to coding, though, so perhaps this is all fine. – Stan Jul 11 '14 at 14:58

1 Answers1

3

I had to add the self keyword to access the variable in the invoked operator (basic python, ouch!)

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 name=%s" % self.text_name ) # <<- self.

Gorgious
  • 30,723
  • 2
  • 44
  • 101
stacker
  • 38,549
  • 31
  • 141
  • 243