I want to execute a function after a path is accepted from
path : StringProperty(subtype='DIR_PATH')
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')
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()
path : StringProperty(subtype='DIR_PATH')
$\endgroup$