1
$\begingroup$

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'

$\endgroup$
  • $\begingroup$ The snippet actually seems to work. pasteall.org/52716/python $\endgroup$ – pink vertex Jul 9 '14 at 13:12
  • $\begingroup$ 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. $\endgroup$ – ksoo Jul 11 '14 at 14:58
1
$\begingroup$

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.
$\endgroup$

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.