WIP: Experiment: Drop import operator helper and file drop type #111242

Closed
Guillermo Venegas wants to merge 23 commits from guishe/blender:drop-helper into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Contributor

Added a operator that handles files drops and redirects this drops to file importers operators
This is done with FileHandler type class, that can be register to poll if file a extension can be imported with a operator.

Important note:

This pr is not intended for review, this pr collects changes that can be done in several patches, some changes allow file importers to show a popup dialog instead a import window, or changes that allow multiple file to be dropped at the time
For curious ones or people who want to leave a little comment the important changes are in the files

  • BKE_screen.h
  • io_drop_import_helper.cc
  • rna_ui.cc

Usage

WM_OT_gpencil_import_svg operator import .svg files as gpencil objects, we can register a FileHandler class like

class WM_FH_gpencil(bpy.types.FileHandler):
    bl_idname = "WM_FH_gpencil"
    bl_label = "gpencil"
    bl_import_operator = "WM_OT_gpencil_import_svg"
    bl_file_extensions = [{"extension": ".svg"}]

    @classmethod
    def poll(cls, context):
        return context.area.type == "VIEW_3D"

With this now if a .svg file is dropped in the view3d we can use the WM_OT_gpencil_import_svg operator to import this file as a gpencil object with a tiny dialog box.

Multiple operators supports handling same file

When a file can be handled with multiple operators, a menu will be displayed and the user should pick which operator to use.

How to make a operator be able to receive filedrops?

Internally the operator that can handle a file with a extension will be invoke as a default.

  • If the operator only supports to handle one file at the time, the operator should have a FILE_PATH StringProperty called filepath
filepath: StringProperty(subtype='FILE_PATH')
  • if the operator supports multiple files, the operator should have a DIR_PATH StringProperty called directory and a OperatorFileListElement CollectionProperty called files
directory: StringProperty(subtype='DIR_PATH')
files: CollectionProperty(type=OperatorFileListElement)
  • Or both

When the operator is invoked, it can check if this properties are set, and if set show a import a popup dialog or directly execute the operator, or if not set show the usual file importer window.

Note:

Sequencer have this nice drag&drop preview, if any image or movie es dropped this operator will be checked first, so in this region adding FileDrops for images or movies will not work


This is an alternative to #106871 that adds static file drop zones, and based on the @Harley comment #106871 (comment)
Added a operator that handles files drops and redirects this drops to file importers operators This is done with `FileHandler` type class, that can be register to poll if file a extension can be imported with a operator. Important note: -- This pr is not intended for review, this pr collects changes that can be done in several patches, some changes allow file importers to show a popup dialog instead a import window, or changes that allow multiple file to be dropped at the time For curious ones or people who want to leave a little comment the important changes are in the files - BKE_screen.h - io_drop_import_helper.cc - rna_ui.cc Usage --- `WM_OT_gpencil_import_svg` operator import `.svg` files as gpencil objects, we can register a `FileHandler` class like ```python class WM_FH_gpencil(bpy.types.FileHandler): bl_idname = "WM_FH_gpencil" bl_label = "gpencil" bl_import_operator = "WM_OT_gpencil_import_svg" bl_file_extensions = [{"extension": ".svg"}] @classmethod def poll(cls, context): return context.area.type == "VIEW_3D" ``` With this now if a `.svg` file is dropped in the `view3d` we can use the `WM_OT_gpencil_import_svg` operator to import this file as a `gpencil object` with a tiny dialog box. <video src="/attachments/fb5baee3-71c1-4c48-ab7c-03dbbad433c6" controls></video> Multiple operators supports handling same file --- When a file can be handled with multiple operators, a menu will be displayed and the user should pick which operator to use. <video src="/attachments/d3ccecac-20b6-4388-a3ba-d8df936b33d4" controls></video> How to make a operator be able to receive filedrops? --- Internally the operator that can handle a file with a extension will be `invoke` as a default. - If the operator only supports to handle one file at the time, the operator should have a `FILE_PATH` `StringProperty` called `filepath` ```python filepath: StringProperty(subtype='FILE_PATH') ``` - if the operator supports multiple files, the operator should have a `DIR_PATH` `StringProperty` called `directory ` and a `OperatorFileListElement` `CollectionProperty` called `files` ```python directory: StringProperty(subtype='DIR_PATH') files: CollectionProperty(type=OperatorFileListElement) ``` - Or both When the `operator` is `invoked`, it can check if this `properties` are `set`, and if set show a import a popup dialog or directly execute the operator, or if not set show the usual file importer window. Note: ---- Sequencer have this nice drag&drop preview, if any image or movie es dropped this operator will be checked first, so in this region adding `FileDrops` for images or movies will not work <video src="/attachments/6fd17e41-581f-4a3e-86e5-cfc8e1568f89" controls></video> <hr> This is an alternative to #106871 that adds static file drop zones, and based on the @Harley comment https://projects.blender.org/blender/blender/pulls/106871#issuecomment-924929
Author
Contributor

<video src="/attachments/59dc3f48-3116-4f63-ae77-c569fcf72240" title="2023-08-17 18-36-05.mp4" controls></video>
Guillermo Venegas changed title from WIP: Experiment: Drop import operator helper and file import userpref to WIP: Experiment: Drop import operator helper and file importers userpref 2023-08-18 02:41:45 +02:00
Guillermo Venegas changed title from WIP: Experiment: Drop import operator helper and file importers userpref to WIP: Experiment: Drop import operator helper and file drop type 2023-08-24 05:29:56 +02:00
Guillermo Venegas force-pushed drop-helper from 07ba92a7c1 to 84a43bcb76 2023-08-24 06:00:22 +02:00 Compare

I do wonder why the "which importer handles which extensions" is defined in some central location, and is part of user preferences at all. Conceptually, couldn't it be that "hey, I can do these extensions, call this function on me when they are dropped" be part of each importer itself?

That way, all the built-in importers (both C++ and Python ones) could have these functions added. Any 3rd party importers would not be handling the drop operation, until they also implement this (new, optional) functionality.

I do wonder why the "which importer handles which extensions" is defined in some central location, and is part of user preferences at all. Conceptually, couldn't it be that "hey, I can do these extensions, call this function on me when they are dropped" be part of each importer itself? That way, all the built-in importers (both C++ and Python ones) could have these functions added. Any 3rd party importers would not be handling the drop operation, until they also implement this (new, optional) functionality.
Author
Contributor

Hello, Yes, doing it as a preference had many complications.

I've made a new FileDrop registrable type that can register a function poll_extension and an operator name to call in case poll_extension is successful, and is bound to a space type.

Although poll_extension could also be added to the operators instead of making the new type

The FileDrop in python looks like

class WM_FDT_alembic(bpy.types.FileDrop):
    bl_space_type = "VIEW_3D"
    bl_idname = "WM_FDT_alembic"
    bl_operator = "WM_OT_alembic_import"

    @classmethod
    def poll_extension(cls, context, extension):
        return extension == ".abc"

Feedback is welcome

Hello, Yes, doing it as a preference had many complications. I've made a new FileDrop registrable type that can register a function poll_extension and an operator name to call in case poll_extension is successful, and is bound to a space type. Although poll_extension could also be added to the operators instead of making the new type The FileDrop in python looks like ```python class WM_FDT_alembic(bpy.types.FileDrop): bl_space_type = "VIEW_3D" bl_idname = "WM_FDT_alembic" bl_operator = "WM_OT_alembic_import" @classmethod def poll_extension(cls, context, extension): return extension == ".abc" ``` Feedback is welcome
Guillermo Venegas added 4 commits 2023-08-31 21:50:49 +02:00
Author
Contributor

Added a menu to be able to chose what operator to use in case two or more operators can handle a extension
For extensions that only have one will just call operator that handles each dropped file

Few considerations:

  1. For extensions that can be handled with multiple operators its used the name, that can be null o multiple operators can have duplicated names, what should i do to show the menu to pick what operator to use in case multiple operators can handle same file.
  2. Should enforce operator existence on register FileDropType and should unregister FileDropTypes when a operator is removed?
  3. Currently the operator that handles drops and makes calls to others operator that can handle a specific extension, does this by setting filepath and directory and names for files in files collection property, should allow user to be able to define how to name this properties instead of hard makes them hard coded?
Added a menu to be able to chose what operator to use in case two or more operators can handle a extension For extensions that only have one will just call operator that handles each dropped file <video src="/attachments/bbd2b635-aa3b-421c-aa81-7eb5bfbdde50" title="2023-08-31 13-55-21.mp4" controls></video> Few considerations: 1. For extensions that can be handled with multiple operators its used the name, that can be null o multiple operators can have duplicated names, what should i do to show the menu to pick what operator to use in case multiple operators can handle same file. 2. Should enforce operator existence on register FileDropType and should unregister FileDropTypes when a operator is removed? 3. Currently the operator that handles drops and makes calls to others operator that can handle a specific extension, does this by setting `filepath` and `directory` and `names` for files in `files` collection property, should allow user to be able to define how to name this properties instead of hard makes them hard coded?
Harley Acheson added this to the User Interface project 2023-09-02 01:50:00 +02:00
Iliya Katushenock added the
Interest
Pipeline, Assets & IO
label 2023-09-02 03:06:16 +02:00
Guillermo Venegas added 2 commits 2023-09-05 17:12:40 +02:00
Guillermo Venegas added 2 commits 2023-09-11 17:43:48 +02:00

There is a design proposal in #68935 for a more general type like bpy.types.FileHandler that would have this type of drag & drop functionality as a subset. We also want to be able to have a single File > Import ... menu entry that can automatically pick the right importer based on the extension. And some more advanced cases as explained in that issue.

So I would suggest to change this bpy.types.FileDrop to a bpy.types.FileHandler that we can extend later. For the fields, perhaps:

  • bl_idname: same as it is now
  • bl_label: UI name for the file format, that we may need to show in the UI
  • bl_file_extensions: list of strings instead of poll function, as we might want to show that list somewhere in the UI
  • bl_import_operator: same as bl_operator, but leaving possibility to distinguish from an export operator

Instead of a space type, I would suggest to make a poll function that gets a context. Because we might want to inspect the state of the editor to see if dropping is supported, or support dropping in multiple types of editors.

There is a design proposal in #68935 for a more general type like `bpy.types.FileHandler` that would have this type of drag & drop functionality as a subset. We also want to be able to have a single File > Import ... menu entry that can automatically pick the right importer based on the extension. And some more advanced cases as explained in that issue. So I would suggest to change this `bpy.types.FileDrop` to a `bpy.types.FileHandler` that we can extend later. For the fields, perhaps: * `bl_idname`: same as it is now * `bl_label`: UI name for the file format, that we may need to show in the UI * `bl_file_extensions`: list of strings instead of poll function, as we might want to show that list somewhere in the UI * `bl_import_operator`: same as `bl_operator`, but leaving possibility to distinguish from an export operator Instead of a space type, I would suggest to make a poll function that gets a context. Because we might want to inspect the state of the editor to see if dropping is supported, or support dropping in multiple types of editors.
Guillermo Venegas added 3 commits 2023-09-15 16:34:34 +02:00
Guillermo Venegas added 4 commits 2023-09-16 04:12:12 +02:00

I'm a bit swamped with 4.0 work for the next few weeks and don't have time to review this in detail, but I like the overall direction.

I would like to get some input on the overall design from @ideasman42 and @mont29.

I'm a bit swamped with 4.0 work for the next few weeks and don't have time to review this in detail, but I like the overall direction. I would like to get some input on the overall design from @ideasman42 and @mont29.
Brecht Van Lommel requested changes 2023-10-24 19:52:50 +02:00
Brecht Van Lommel left a comment
Owner

Quick code review, would still like input from other developers who are more involved in this area.

Quick code review, would still like input from other developers who are more involved in this area.
@ -3502,0 +3573,4 @@
@classmethod
def poll(cls, context):
return context.area.type == "VIEW_3D"

Probably should not have both STL importers here, in the end only one will be used when dropping?

Probably should not have both STL importers here, in the end only one will be used when dropping?
Author
Contributor

In case there are 2 file handlers that can be used, you can choose which one to use with a menu

But the add-on one can include the file handle registration in its owns files

In case there are 2 file handlers that can be used, you can choose which one to use with a menu <video src="/attachments/d3ccecac-20b6-4388-a3ba-d8df936b33d4" controls></video> But the add-on one can include the file handle registration in its owns files

Ok, that makes sense. It would be better to have to just a single importer that is stable, but that's not a problem to be solved by this PR.

Ok, that makes sense. It would be better to have to just a single importer that is stable, but that's not a problem to be solved by this PR.
brecht marked this conversation as resolved
@ -3502,0 +3580,4 @@
bl_idname = "WM_FH_usd"
bl_label = "USD"
bl_import_operator = "WM_OT_usd_import"
bl_file_extensions = [{"extension": ".usd"}]

There is also usda, usdc, usdz.

There is also `usda`, `usdc`, `usdz`.
guishe marked this conversation as resolved
@ -673,0 +673,4 @@
#ifdef __cplusplus
struct bFileExtension {

This should move to its own file, BKE_file_handler.h

This should move to its own file, `BKE_file_handler.h`
guishe marked this conversation as resolved
@ -673,0 +675,4 @@
struct bFileExtension {
char extension[64];
};

Leave empty line between struct definitions.

Leave empty line between struct definitions.
guishe marked this conversation as resolved
@ -673,0 +682,4 @@
char label[BKE_ST_MAXNAME]; /* For UI text. */
char import_operator[BKE_ST_MAXNAME]; /* Import operator name, same as #OP_MAX_TYPENAME. */

Better to use OP_MAX_TYPENAME then, even if it means including a header.

Better to use `OP_MAX_TYPENAME` then, even if it means including a header.
guishe marked this conversation as resolved
@ -673,0 +685,4 @@
char import_operator[BKE_ST_MAXNAME]; /* Import operator name, same as #OP_MAX_TYPENAME. */
/* Check if file handler can be used in a context. */
bool (*poll)(const struct bContext *C, FileHandlerType *file_handle_type);

Maybe call this poll_drop to clarify that it is used for drag & drop. We could imagine adding other poll types in the future to this type.

Maybe call this `poll_drop` to clarify that it is used for drag & drop. We could imagine adding other poll types in the future to this type.
guishe marked this conversation as resolved
@ -1339,2 +1339,4 @@
}
}
static blender::Vector<FileHandlerType *> file_handlers{};

Move to new file_handler.cc file.

Move to new `file_handler.cc` file.
guishe marked this conversation as resolved
@ -0,0 +125,4 @@
for (auto *file_handler : file_handlers) {
wmOperatorType *ot = WM_operatortype_find(file_handler->import_operator, false);
PointerRNA op_props = copy_file_properties_to_operator_type_pointer(op, ot);
char *import_label = BLI_sprintfN("Import %s", file_handler->label);

Use IFACE_("Import %s") for translation.

Use `IFACE_("Import %s")` for translation.
guishe marked this conversation as resolved
@ -0,0 +139,4 @@
UI_popup_menu_end(C, pup);
return OPERATOR_INTERFACE;
}

Leave empty line between function definitions.

Leave empty line between function definitions.
guishe marked this conversation as resolved
@ -0,0 +142,4 @@
}
void WM_OT_drop_import_helper(wmOperatorType *ot)
{
ot->name = "Drop Import Herlper";

Herlper -> Helper

But I would not put "helper" in the name, lots of operators are helpers in some way and it doesn't really explain much.

Maybe "Drop to Import File"?

Herlper -> Helper But I would not put "helper" in the name, lots of operators are helpers in some way and it doesn't really explain much. Maybe "Drop to Import File"?
brecht marked this conversation as resolved
@ -0,0 +146,4 @@
ot->description =
"Helper operator that handles file drops and uses file handler information to call "
"operators that can import a file with extension.";
ot->idname = "WM_OT_drop_import_helper";

Add ot->flag = OPTYPE_INTERNAL;, this does not need to appear in search.

Add `ot->flag = OPTYPE_INTERNAL;`, this does not need to appear in search.
guishe marked this conversation as resolved
@ -0,0 +1,85 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#if defined(WITH_COLLADA) || defined(WITH_IO_GPENCIL) || defined(WITH_IO_WAVEFRONT_OBJ) || \
defined(WITH_IO_PLY) || defined(WITH_IO_STL) || defined(WITH_USD)

Remove these tests. The code may not be used if none of these are defined, but that's not a problem.

Remove these tests. The code may not be used if none of these are defined, but that's not a problem.
guishe marked this conversation as resolved
@ -0,0 +14,4 @@
int wm_io_import_invoke(bContext *C, wmOperator *op, const wmEvent *event);
void skip_filesel_props(wmOperatorType *ot, const eFileSel_Flag flag);
void files_drop_label_draw(bContext *C, wmOperator *op, int icon, const char *extension);

These names are bit generic to put in global namespace. The wm_ prefix would also imply that the function is defined in the window_manager_module.

Maybe use a io_util_ prefix?

These names are bit generic to put in global namespace. The `wm_` prefix would also imply that the function is defined in the `window_manager_module`. Maybe use a `io_util_` prefix?
guishe marked this conversation as resolved
@ -5922,3 +5922,3 @@
{
/* copy drag path to properties */
RNA_string_set(drop->ptr, "filepath", WM_drag_get_path(drag));
RNA_string_set(drop->ptr, "filepath", WM_drag_get_paths(drag)[0].c_str());

I would rename WM_drag_get_path to WM_drag_get_single_path and add WM_drag_get_paths alongside it. It's a bit ugly to have this assumption that the first path should be used duplicated in multiple places.

I would rename `WM_drag_get_path` to `WM_drag_get_single_path` and add `WM_drag_get_paths` alongside it. It's a bit ugly to have this assumption that the first path should be used duplicated in multiple places.
Author
Contributor

I made WM_drag_get_single_path return the first path it found.

Just to note, most places that use this are for drag and drop, most of which could also be converted to file handlers.

I made WM_drag_get_single_path return the first path it found. Just to note, most places that use this are for drag and drop, most of which could also be converted to file handlers.
brecht marked this conversation as resolved
@ -2081,0 +2228,4 @@
{
StructRNA *srna;
RNA_def_property_srna(cprop, "FileExtensions");

I'm not sure about this mechanism. We don't have a good way to pass a list of strings here currently, but the easier workaround to me seems to have a string of extensions separated by ; or something like that.

I'm not sure about this mechanism. We don't have a good way to pass a list of strings here currently, but the easier workaround to me seems to have a string of extensions separated by `;` or something like that.
Author
Contributor

I changed it to be just a string separated by ;, and also eliminated the IDProperty because it was only used for that purpose.

I changed it to be just a string separated by `;`, and also eliminated the `IDProperty` because it was only used for that purpose.
brecht marked this conversation as resolved
Guillermo Venegas added 3 commits 2023-10-26 20:14:19 +02:00
Author
Contributor

To note, parts of this pr is already separated in other 2 pr

Initial support for file handler registration:
#112466

Support for multiple file drag&drop
#107230

To note, parts of this pr is already separated in other 2 pr Initial support for file handler registration: #112466 Support for multiple file drag&drop #107230
Guillermo Venegas added 4 commits 2023-12-10 16:28:33 +01:00
Author
Contributor

Support to dran-n-drop #116047

Support to dran-n-drop https://projects.blender.org/blender/blender/pulls/116047

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#111242
No description provided.