Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

I have an operator that I need to hand over arguments. I know that one possibility is to add attributes to the scene and use this but in draw-methods of operators I can modify the arguments passed to the operator directly. So I thought I should be able to do this from the console.

So far I have this operator (as an example):

from bpy.types import Operator
from bpy.props import StringProperty

class ExampleOP(Operator):
    bl_idname = "object.test"
    bl_label = "Is just an example"
    bl_options = {'REGISTER'}

    foo = StringProperty()
    result = StringProperty()

    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)

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

        col.prop_search(self, 'result', context.object.data, 'uv_textures', text="UV Layer")

    def execute(self, context):
        print(foo)
        return {'FINISHED'}

What I want to do is bpy.ops.object.test('INVOKE_DEFAULT', foo="something").

EDIT: It seems to me that the answer only works for *Property()-objects not any object. This gives me a

TpyeError: Convering py args to operator properties: keyword "ev" unrecognized

Note: Please do not post any answers telling me off, to do this in another way. NO, this is exactly what I need, I cannot handle the "print-statement" outwards of the operator and yes, and foo may not always be a StringProperty.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You already added Operator Properties to your operator, which is perfectly fine.

The missing link is how to access the operator property values, which is rather simple (remember it's a python class after all):

print(self.foo)

self refers to the operator instance, which stores the property values.

import bpy
from bpy.types import Operator
from bpy.props import StringProperty

class ExampleOP(Operator):
    bl_idname = "object.test"
    bl_label = "Is just an example"
    bl_options = {'REGISTER'}

    foo = StringProperty()
    result = StringProperty()

    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)

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

        col.prop_search(self, 'result', context.object.data, 'uv_textures', text="UV Layer")

    def execute(self, context):
        print("foo =", self.foo)
        return {'FINISHED'}


def register():
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()

    bpy.ops.object.test('INVOKE_DEFAULT', foo="bar")
share|improve this answer
    
oh what a beginner error in my code... I am not sure why this wasn't a compiler error, after all there's no gobal foo declared... thanks again for pointing that out –  WorldSEnder Feb 22 at 23:48
    
this only seems to work for *Property-objects, not for any datastructure you may want to hand over –  WorldSEnder Feb 23 at 0:41
    
You only can - and should! - hand bpy.props types over. Not sure what you're trying, if you need a dynamically-sized list of basic types (string, int, float, bool...), use a CollectionProperty with a PropertyGroup of basic type equivalents. There shouldn't be a need to pass arbitrary data types. You can reference objects by name (=string) for instance, no need to pass by reference. In some rare cases, it can be necessary to store stuff in static members of a class (class G: pass, read and write anywhere in code: G.global_var = 123). Note that it will likely not have undo-capabilities. –  CoDEmanX Feb 23 at 1:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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