4

Is it possible to access the property of an operator (such as the filepath to the append operator) by python ?

pistiwique
  • 1,106
  • 9
  • 22

1 Answers1

6

Passing properties to an operator, here's a small example (see the test call towards the end). This snippet is an elaboration on TextEditor > Templates > Python > Operator Simple

import bpy

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

property_one: bpy.props.StringProperty(default='state one')
property_two: bpy.props.StringProperty(default='state two')

@classmethod
def poll(cls, context):
    return True

def execute(self, context):
    print(self.property_one)
    print(self.property_two)
    return {'FINISHED'}


def register(): bpy.utils.register_class(SomeOperator)

def unregister(): bpy.utils.unregister_class(SomeOperator)

if name == "main": register()

# test call
bpy.ops.object.some_operator(property_one='woop_one', property_two='woop_two')

if you want to call such an operator from a layout element..

# if you want to pass a single property
row.operator('object.some_operator').property_one = 'some_new_value'

if you want to pass multiple properties

some_op = row.operator('object.some_operator') some_op.property_one = 'some new value one' some_op.property_two = 'some new value two'


Autocomplete

If you don't know the name of the properties, then you can use the Python console's autocomplete.

In this example hit ctrl+space after the parenthesis ( , here you see lines is the only property you can set.

enter image description here

Documentation

Another resource is the current api docs use the search feature, type in for example bpy.ops.text.scroll and it will show the signature.

In the case of WM_OT_append you should be looking for bpy.ops.wm.append.

Tooltips

Use the tooltip that appears by default upon hovering of menu items / buttons:

enter image description here

Gorgious
  • 30,723
  • 2
  • 44
  • 101
zeffii
  • 39,634
  • 9
  • 103
  • 186
  • Thank you for your reply but i'd like doing the same operation but with operators which i don't know name of the properties as the WM_OT_append operator. – pistiwique Aug 06 '16 at 20:17
  • @pistiwique how did you find the name of WM_OT_append ? – zeffii Aug 20 '16 at 11:36
  • By using the console's autocomplete typing bpy.types. – pistiwique Aug 20 '16 at 12:48
  • 1
    OK, then might I suggest using the tooltips that appear when hovering over menu items, that way you'll see the bpy.ops.* , when you autocomplete those , you will see the parameters as described above – zeffii Aug 20 '16 at 13:31