3
$\begingroup$

I have a popup that can allow the user the set a value. I can see that the value is getting set correctly as it reports it to the console. However after the function that calls the Operator completes how is it possible to access values that we set inside the Operator class?

class OpenBrowser(bpy.types.Operator):
    bl_idname = "open.browser"
    bl_label = "Select Path"
    bl_options = {"REGISTER", "UNDO"}

    directory = StringProperty(subtype="DIR_PATH") 
    cancel_search = BoolProperty(
        name="Cancel Search",
        description="If no further images are to be found",
        default=False,
    )

    def execute(self, context):
        display = "dirpath=" + self.directory  
        print(display)  
        return {'FINISHED'}

    def invoke(self, context, event): 
        wm = context.window_manager
        wm.fileselect_add(self)
        return {'RUNNING_MODAL'}  

bpy.ops.open.browser('INVOKE_DEFAULT')

How do I access the value that was set for directory following the the completion of the last function call?

$\endgroup$

2 Answers 2

1
$\begingroup$

Window Manager keeps track of registered operators

Lets say we add a property to the simple operator template, and make it register - undo

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    bl_options = {'REGISTER', 'UNDO'}
    xxx : bpy.props.StringProperty(default="foo")

Invoke it from the UI

bl_options = {'REGISTER', 'UNDO'}

Then in python console use WindowManager.operator_properties_last

>>> op = C.window_manager.operator_properties_last("object.simple_operator")
>>> op.xxx
'ggggg'

There is a also a collection of operators used in WindowManager.operators If the operator has register undo we can find the value used, for example having invoked bpy.ops.object.duplicate_move() in the UI

>>> C.window_manager.operators['
                                MESH_OT_snap_faces']
                                OBJECT_OT_duplicate_move']
                                OBJECT_OT_editmode_toggle']
                                TEXT_OT_run_script']
                                VIEW3D_OT_select']
                                VIEW3D_OT_select']
>>> op = C.window_manager.operators['OBJECT_OT_duplicate_move']
>>> # how much did it move
>>> op.macros['
               OBJECT_OT_duplicate']
               TRANSFORM_OT_translate']
>>> op.macros['TRANSFORM_OT_translate'].properties['value'].to_list()
[0.7582358121871948, 1.8491952419281006, 0.10750504583120346]

The collection is keyed using the idname

>>> bpy.ops.object.duplicate_move.idname()
'OBJECT_OT_duplicate_move'

Note that the value of properties here will be the as used only if the operator has 'UNDO' option, otherwise it will return the default value of the property.

Note the template class name is SimpleOperator but really should be OBJECT_OT_simple_operator to match its id name "object.simple_operator" as per the naming convention

From https://blender.stackexchange.com/a/159444/15543

$\endgroup$
0
$\begingroup$

Best I can think of is:

Register new property:

bpy.types.Scene.directory = bpy.props.StringProperty()

and copy contents of Your self.directory to this property while executing (in execute method):

context.scene.directory = self.directory
$\endgroup$

You must log in to answer this question.

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