2
$\begingroup$

I want to execute a function after a path is accepted from

path : StringProperty(subtype='DIR_PATH')

enter image description here

$\endgroup$
2
  • $\begingroup$ It's not clear to me how you opened this dialog? How can I get to that screen? $\endgroup$
    – Harry McKenzie
    Commented Apr 15, 2023 at 14:34
  • 1
    $\begingroup$ from path : StringProperty(subtype='DIR_PATH') $\endgroup$
    – Karan
    Commented Apr 15, 2023 at 14:38

2 Answers 2

1
$\begingroup$

Here's a sample operator. When you execute the operator it brings up the dialog where you can choose a path and then you can execute code once you press Accept.

import bpy

class SampleAcceptOperator(bpy.types.Operator):
    bl_idname = "object.sample_accept_operator"
    bl_label = "Sample Accept Operator"
    bl_description = "Accept a directory path"

    filepath: bpy.props.StringProperty(subtype="DIR_PATH")

    def execute(self, context):
        print("Path accepted:", self.filepath)
        # Add your code here to execute after the path is accepted
        return {'FINISHED'}

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


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


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


if __name__ == "__main__":
    register()

    # test the operator
    bpy.ops.object.sample_accept_operator('INVOKE_DEFAULT')
$\endgroup$
1
$\begingroup$

You need to use the update callback.

Example :

import bpy


class HelloWorldPanel(bpy.types.Panel):
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout
        layout.prop(context.scene, "path")


def update_path(self, context):
    print(f"path updated : {self.path}")


def register():
    bpy.utils.register_class(HelloWorldPanel)
    bpy.types.Scene.path = bpy.props.StringProperty(subtype='DIR_PATH', update=update_path)


if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

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