ImportHelper: Common methods for FileHandler drag and drop support #119774

Merged
Jesse Yurkovich merged 2 commits from deadpin/blender:bpy_extras into main 2024-03-29 01:23:02 +01:00

Provide common implementations for two operations that all Python
FileHandlers will typically use.

  • poll_file_object_drop To be used inside the FileHandler poll_drop callback
  • invoke_popup To be used inside the Operator invoke callback

The above code closely mirrors what is currently done inside the existing Blender c++ FileHandlers.

Provide common implementations for two operations that all Python FileHandlers will typically use. - `poll_file_object_drop` To be used inside the FileHandler `poll_drop` callback - `invoke_popup` To be used inside the Operator `invoke` callback The above code closely mirrors what is currently done inside the existing Blender c++ FileHandlers.
Jesse Yurkovich added 1 commit 2024-03-22 09:47:02 +01:00
Jesse Yurkovich requested review from Bastien Montagne 2024-03-22 09:51:49 +01:00
Author
Member

CC @guishe as FYI

CC @guishe as FYI
Damien Picard reviewed 2024-03-25 01:54:21 +01:00
Damien Picard left a comment
Member

I’ve got a few remarks in addition to the ones noted inline:

  • The filepath property should have the 'HIDDEN' option to prevent it from being shown in the file handler popup.
  • I think it would be nice to show how to use this in the docs. Here is an updated example file:
    bpy.types.FileHandler.2.py
I’ve got a few remarks in addition to the ones noted inline: - The `filepath` property should have the 'HIDDEN' option to prevent it from being shown in the file handler popup. - I think it would be nice to show how to use this in the docs. Here is an updated example file: [bpy.types.FileHandler.2.py](/attachments/ed54f9bf-db1e-413a-9598-35b56ba37672)
@ -102,6 +102,16 @@ class ImportHelper:
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def invoke_popup(self, context, confirm_text="Ok"):
Member

I think it would be better to follow the c++ implementation and use bl_label as confirmation text by default rather than “Ok”:

    def invoke_popup(self, context, confirm_text=""):
        if not confirm_text:
            confirm_text = self.bl_label
I think it would be better to follow the c++ implementation and use `bl_label` as confirmation text by default rather than “Ok”: ```python def invoke_popup(self, context, confirm_text=""): if not confirm_text: confirm_text = self.bl_label ```
deadpin marked this conversation as resolved
@ -105,0 +106,4 @@
if self.properties.is_property_set("filepath"):
title = self.filepath
if len(self.files) > 1:
title = "Import %d files" % len(self.files)
Member

This should be translated:

@@ -23,7 +23,10 @@ from bpy.props import (
     EnumProperty,
     StringProperty,
 )
-from bpy.app.translations import pgettext_data as data_
+from bpy.app.translations import (
+    pgettext_iface as iface_,
+    pgettext_data as data_,
+    )
 
 
 def _check_axis_conversion(op):
@@ -106,9 +109,11 @@ class ImportHelper:
         if self.properties.is_property_set("filepath"):
             title = self.filepath
             if len(self.files) > 1:
-                title = "Import %d files" % len(self.files)
-            context.window_manager.invoke_props_dialog(self, confirm_text=confirm_text, title=title)
-            return {'RUNNING_MODAL'}
+                title = iface_("Import {} files").format(len(self.files))
+            confirm_text = iface_(confirm_text)
+            return context.window_manager.invoke_props_dialog(
+                self, confirm_text=confirm_text, title=title, translate=False
+            )
         context.window_manager.fileselect_add(self)
         return {'RUNNING_MODAL'}

I’m using str.format() simply to reuse the existing translation message from the c++ implementation.

This should be translated: ```diff @@ -23,7 +23,10 @@ from bpy.props import ( EnumProperty, StringProperty, ) -from bpy.app.translations import pgettext_data as data_ +from bpy.app.translations import ( + pgettext_iface as iface_, + pgettext_data as data_, + ) def _check_axis_conversion(op): @@ -106,9 +109,11 @@ class ImportHelper: if self.properties.is_property_set("filepath"): title = self.filepath if len(self.files) > 1: - title = "Import %d files" % len(self.files) - context.window_manager.invoke_props_dialog(self, confirm_text=confirm_text, title=title) - return {'RUNNING_MODAL'} + title = iface_("Import {} files").format(len(self.files)) + confirm_text = iface_(confirm_text) + return context.window_manager.invoke_props_dialog( + self, confirm_text=confirm_text, title=title, translate=False + ) context.window_manager.fileselect_add(self) return {'RUNNING_MODAL'} ``` I’m using `str.format()` simply to reuse the existing translation message from the c++ implementation.
deadpin marked this conversation as resolved
Jesse Yurkovich added 1 commit 2024-03-25 03:53:50 +01:00
Author
Member

Aye, thanks for the feedback. I've added a slightly modified variation of bpy.types.FileHandler.2.py now too.

Aye, thanks for the feedback. I've added a slightly modified variation of `bpy.types.FileHandler.2.py` now too.
Bastien Montagne approved these changes 2024-03-28 03:50:55 +01:00
Bastien Montagne left a comment
Owner

LGTM

LGTM
Jesse Yurkovich merged commit af7a34dee7 into main 2024-03-29 01:23:02 +01:00
Jesse Yurkovich deleted branch bpy_extras 2024-03-29 01:23:09 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
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
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#119774
No description provided.