2
$\begingroup$

I would like to be able to pass aarguments to operators that I am calling from panels/buttons but I do not know how to do that. At the moment I use "global" variables but they tend to make the code a bit like a puzzle since I need to constantly update them.

See the code below, in that example I would like to pass an argument (like ARG) to "object.simple_operator" when the button is clicked.

import bpy




def DO_SOMETHING(arg):

    print(arg)

return

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

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

    def execute(self, context):
        DO_SOMETHING(ARG)
        return {'FINISHED'}



class MyPanel(bpy.types.Panel):

    bl_label = "mypanel"
    bl_idname = "OBJECT_PT_MyPanel"

    def draw(self, context):

        layout = self.layout

        wm = context.window_manager

        obj = context.object

        row = layout.row()
        row.operator('object.simple_operator')
$\endgroup$
1
  • 4
    $\begingroup$ Related blender.stackexchange.com/a/17755/15543 Would usually add a property to the operator, lets say it's and int property called x and in layout use layout.operator("some.op").x = 5 to set the property to 5 (akin to bpy.ops.some.op(x=5)) For multiple op = layout.op... op.x = 5 op.string = "XXXX" $\endgroup$
    – batFINGER
    Dec 30, 2018 at 7:28

1 Answer 1

2
$\begingroup$

General example based on your starting point including a property group setup.

import bpy
from bpy.types import PropertyGroup


class MyToolPropertyGroup(PropertyGroup):
    testint = bpy.props.IntProperty(  # bl 2.80 use testint: bpy.props
        name="testint",
        description="",
        default=1,
        min=1,
        )


def DO_SOMETHING(arg1, arg2):
    print("%i * %f = %f" % (arg1, arg2, (arg1*arg2)))


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

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

    def execute(self, context):
        props = context.scene.MyPropertyGroup
        my_val = 2
        DO_SOMETHING(props.testint, my_val)
        return {'FINISHED'}


class MyPanel(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
#    bl_category = "Tab Name"  # not used in blender 2.80
    bl_context = "objectmode"
    bl_label = "mypanel"
    bl_idname = "OBJECT_PT_MyPanel"

    def draw(self, context):
        layout = self.layout
        obj = context.object
        props = context.scene.MyPropertyGroup
        row = layout.row()
        row.prop(props, "testint")
        row = layout.row()
        row.operator('object.simple_operator')


classes = [SimpleOperator, MyPanel, MyToolPropertyGroup, ]


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.MyPropertyGroup = bpy.props.PointerProperty(
            type=MyToolPropertyGroup)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.MyPropertyGroup

if __name__ == "__main__":
    register()
$\endgroup$
2
  • 1
    $\begingroup$ To avoid mixing up classes and instances It is recommended to use lower case for instances, bpy.types.Scene.my_prop_group = bpy.props.Po... Wonder if it is not easier to use operator properties direcly $\endgroup$
    – batFINGER
    Dec 30, 2018 at 7:36
  • $\begingroup$ Ok I am having an issue with your recommendation. props.editorpath=addon_prefs.editorpath AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting PropertyGrp.editorpath , this is what I have props.editorpath=addon_prefs.editorpath > one props is set with the method you mentioned, addon_prefs as it says comes form the addon's own preferences $\endgroup$
    – yarun can
    Jan 1, 2019 at 4:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .