Page MenuHome

Make File Select dialog trigger update function on operator's filepath, directory and filename properties
ClosedPublic

Authored by Colin Basnett (cmbasnett) on Jul 26 2022, 12:11 PM.
Tokens
"Love" token, awarded by dreamak."Burninate" token, awarded by Bariboy."Love" token, awarded by PiloeGAO."Like" token, awarded by hitrpr."Love" token, awarded by Draise."Love" token, awarded by karmacop."Love" token, awarded by Raimund58."Love" token, awarded by iyadahmed2001.

Details

Summary

When an operator is attached to a file select dialog, it updates the directory, filename and filepath properties of the operator (if they exist) as the user navigates through the dialog.

As an operator author, I want to be notified when these properties are changed so that I can provide contextual options based on the contents of the selected file (for example, providing a list of elements available to import from the file to pick from).

This patch does just that. I've attached a .blend file to demonstrate the functionality. You will need to enable developer extras, run the script in the text editor and then press F3 and search for "filepath". The operator will appear in the list. Select it, then navigate around your file system. The name of the filepath and the size of the file will be updated as you select different files.


Here is a demo of a more real-life scenario, applied to an add-on I maintain where it imports animations from a file that contains an arbitrary amount of animations within it (real-world files have upwards of 2000 animations). The video demo shows it populating a list of animations from the selected file.

Diff Detail

Repository
rB Blender

Event Timeline

Colin Basnett (cmbasnett) requested review of this revision.Jul 26 2022, 12:11 PM
Colin Basnett (cmbasnett) created this revision.
source/blender/editors/space_file/file_ops.c
1574–1594

Note to reviewer: special care is taken here not to send update events if the value hasn't actually changed. Otherwise, the update function is spammed every time the UI is redrawn.

source/blender/editors/space_file/filesel.c
674–677

Note to reviewer: without this line, navigating the file browser dialog using the arrow keys would not update the operator properties.

Any reason why the "Note to reviewer" notes are not actual notes/comments in the code?

Any reason why the "Note to reviewer" notes are not actual notes/comments in the code?

Good point, I'll add the comments later today.

+1. This is very useful. Here's another example where MediaInfo is displayed in the sidebar: https://gist.github.com/tin2tin/9dd1236471d4b5b8b165a99aac6844dd

Or text based files ex. .py(included in the above snippet):

I'd hope to avoid passing the context around but there doesn't seem to be a practical way to avoid this because Python requires the context to run it's update functions, so I think this patch is fine as-is.

This revision is now accepted and ready to land.Sep 16 2022, 6:46 AM