Add more examples with typing extensions

This commit is contained in:
Xury Greer
2026-08-09 11:22:47 -07:00
parent 8caedd0973
commit e27c37318f
5 changed files with 321 additions and 1 deletions

View File

@@ -7,23 +7,31 @@ import bpy # noqa: F401, I001
from . import (
utils,
file_formats,
props,
operators,
gui,
)
if _needs_reload:
import importlib
importlib.reload(utils)
importlib.reload(file_formats)
importlib.reload(props)
importlib.reload(operators)
importlib.reload(gui)
def register() -> None:
file_formats.register()
props.register()
operators.register()
gui.register()
def unregister() -> None:
file_formats.unregister()
props.unregister()
operators.unregister()
gui.unregister()

View File

@@ -0,0 +1,123 @@
# Built-in.
from typing import (
TYPE_CHECKING,
cast,
)
# Third Party.
import bpy
from bpy.props import (
BoolProperty,
EnumProperty,
IntProperty,
StringProperty,
)
from bpy.types import (
Context,
PropertyGroup,
)
if TYPE_CHECKING:
from bpy.stub_internal.rna_enums import (
ImageTypeAllItems,
)
from extra_bpy import (
ItemsCallback,
RegisterableTypes,
)
# fmt: off
DEPTH_8: tuple["ImageTypeAllItems", ...] = ('BMP', 'JPEG', 'TARGA', 'TARGA_RAW')
DEPTH_8_16: tuple["ImageTypeAllItems", ...] = ('IRIS', 'PNG', 'TIFF')
DEPTH_8_12_16: tuple["ImageTypeAllItems", ...] = ('JPEG2000',)
DEPTH_8_10_12_16: tuple["ImageTypeAllItems", ...] = ('CINEON', 'DPX')
DEPTH_16_32: tuple["ImageTypeAllItems", ...] = ('OPEN_EXR_MULTILAYER', 'OPEN_EXR')
DEPTH_32: tuple["ImageTypeAllItems", ...] = ('HDR',)
# fmt: on
class FileFormatInfo:
"""Helper Class for managing EnumPropertyItems related to image file formats.
Read more here: https://docs.blender.org/manual/en/5.2/files/media/image_formats.html
"""
@staticmethod
def get_file_formats() -> "ItemsCallback":
# fmt: off
file_formats: list[tuple[ImageTypeAllItems, str, str]] = [
("BMP", ".bmp", ""),
("PNG", ".png", ""),
("JPEG", ".jpg", ""),
("TARGA", ".tga", ""),
("TARGA_RAW", ".tga", ""),
("OPEN_EXR_MULTILAYER", ".exr", ""),
("OPEN_EXR", ".exr", ""),
("HDR", ".hdr", ""),
("TIFF", ".tif", ""),
]
# fmt: on
return file_formats
@staticmethod
def get_color_depths(file_format: str) -> "ItemsCallback":
# fmt: off
if file_format in DEPTH_8:
return [('8', '8', "")]
if file_format in DEPTH_8_16:
return [('8', '8', ""),
('16', '16', "")]
if file_format in DEPTH_8_12_16:
return [('8', '8', ""),
('12', '12', ""),
('16', '16', "")]
if file_format in ('CINEON', 'DPX'):
return [('8', '8', ""),
('10', '10', ""),
('12', '12', ""),
('16', '16', "")]
if file_format in ('OPEN_EXR_MULTILAYER', 'OPEN_EXR'):
return [('16', '16', ""),
('32', '32', "")]
if file_format in ('HDR'):
return [('32', '32', "")]
raise KeyError
# fmt: on
def update_color_depths(self: "EXTENSION_EXAMPLE_FileFormatProperties", context: Context | None) -> "ItemsCallback":
"""This callback gets called automatically to update the enum item list."""
return FileFormatInfo.get_color_depths(cast(str, self.file_format))
class EXTENSION_EXAMPLE_FileFormatProperties(PropertyGroup):
"""Property Group for serializing file format data, and displaying it in Blender's GUI."""
format_name: StringProperty(name="Name", default="")
enabled: BoolProperty(name="Enabled", default=True)
suffix: StringProperty(name="Suffix", default="")
file_format: EnumProperty(name="File format", items=FileFormatInfo.get_file_formats(), default="PNG")
color_depth: EnumProperty(name="Color depth", items=update_color_depths)
# Not used in UI, but must be bound to a Property so its values are retained.
# 'Filmic Log', 'Filmic sRGB', 'Linear', 'Linear ACES', 'Linear ACEScg', 'Non-Color', 'Raw', 'sRGB', 'XYZ'
texture_node_color_space: StringProperty(name="Texture Node Color Space", default="")
samples: IntProperty(name="Samples", default=1, min=1)
classes: list["RegisterableTypes"] = [
EXTENSION_EXAMPLE_FileFormatProperties,
]
def register() -> None:
for class_to_register in classes:
bpy.utils.register_class(class_to_register)
def unregister() -> None:
for class_to_unregister in classes:
bpy.utils.unregister_class(class_to_unregister)

View File

@@ -0,0 +1,103 @@
# Built-in.
from typing import TYPE_CHECKING
# Third Party.
import bpy
from bpy.types import (
Context,
Menu,
Panel,
Scene,
UILayout,
)
# First Party.
from .operators import (
EXTENSION_EXAMPLE_OT_example_operator,
)
from .props import (
EXTENSION_EXAMPLE_SettingsProperties,
)
if TYPE_CHECKING:
from extra_bpy import (
RegisterableTypes,
Scene,
)
class EXTENSION_EXAMPLE_PT_example_settings_panel(Panel):
"""Create a panel UI in the render section of the Properties editor"""
bl_label = "Example settings panel"
bl_idname = "EXTENSION_EXAMPLE_PT_example_settings_panel"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "render"
bl_category = "bake"
def draw(self, context: Context) -> None:
if self.layout is None:
return
if context.scene is None:
return
scene: Scene = context.scene
settings: EXTENSION_EXAMPLE_SettingsProperties = scene.example_settings
row: UILayout = self.layout.row()
row.prop(settings, "name")
row = self.layout.row()
row.prop(settings, "file_format")
row = self.layout.row()
row.prop(settings, "color_depth")
class EXTENSION_EXAMPLE_MT_example_menu(Menu):
"""Example menu for displaying operators"""
bl_idname = "EXAMPLE_MT_example_menu"
bl_label = "Example Menu"
def draw(self, context: Context) -> None:
if self.layout is None:
return
self.layout.operator(
EXTENSION_EXAMPLE_OT_example_operator.bl_idname,
text=EXTENSION_EXAMPLE_OT_example_operator.bl_label,
)
def extension_menu_callback(self: Menu, context: Context) -> None:
if self.layout is None:
return
self.layout.menu(EXTENSION_EXAMPLE_MT_example_menu.bl_idname)
classes: list["RegisterableTypes"] = [
EXTENSION_EXAMPLE_MT_example_menu,
EXTENSION_EXAMPLE_PT_example_settings_panel,
]
def register() -> None:
for class_to_register in classes:
bpy.utils.register_class(class_to_register)
bpy.types.VIEW3D_MT_editor_menus.append(
extension_menu_callback, # pyright: ignore[reportArgumentType]
)
def unregister() -> None:
for class_to_unregister in classes:
bpy.utils.unregister_class(class_to_unregister)
bpy.types.VIEW3D_MT_editor_menus.remove(
extension_menu_callback, # pyright: ignore[reportArgumentType]
)

View File

@@ -0,0 +1,54 @@
# Built-in.
from typing import TYPE_CHECKING
# Third Party.
import bpy
from bpy.props import (
EnumProperty,
PointerProperty,
StringProperty,
)
from bpy.types import (
PropertyGroup,
Scene,
)
from .file_formats import (
FileFormatInfo,
update_color_depths,
)
if TYPE_CHECKING:
from extra_bpy import (
RegisterableTypes,
Scene, # noqa: TC004
)
class EXTENSION_EXAMPLE_SettingsProperties(PropertyGroup):
"""Example Property Group for serializing data, and displaying it in Blender's GUI."""
name: StringProperty(name="Name", default="")
file_format: EnumProperty(name="File format", items=FileFormatInfo.get_file_formats(), default="PNG")
color_depth: EnumProperty(name="Color depth", items=update_color_depths)
classes: list["RegisterableTypes"] = [
EXTENSION_EXAMPLE_SettingsProperties,
]
def register() -> None:
for class_to_register in classes:
bpy.utils.register_class(class_to_register)
# Add the settings to the scene.
Scene.example_settings = PointerProperty(type=EXTENSION_EXAMPLE_SettingsProperties) # type: ignore
def unregister() -> None:
for class_to_unregister in classes:
bpy.utils.unregister_class(class_to_unregister)
# Remove the settings from the scene.
del Scene.example_settings

View File

@@ -1,4 +1,9 @@
"""First party stubs for extra Blender type hints."""
"""First party stubs for extra Blender type hints.
Extends Blender's types with custom attributes that are added by this add-on.
"""
# Built-in.
from collections.abc import Iterable
# Third Party.
from bpy.types import AddonPreferences as _AddonPreferences
@@ -16,8 +21,14 @@ from bpy.types import Operator as _Operator
from bpy.types import Panel as _Panel
from bpy.types import PropertyGroup as _PropertyGroup
from bpy.types import RenderEngine as _RenderEngine
from bpy.types import Scene as _Scene
from bpy.types import UIList as _UIList
# First Party.
from ..src.blender_extension_template.props import (
EXTENSION_EXAMPLE_SettingsProperties as _EXTENSION_EXAMPLE_SettingsProperties,
)
# Classes that can be registered/unregistered.
type RegisterableTypes = type[
_Panel
@@ -37,3 +48,24 @@ type RegisterableTypes = type[
| _Gizmo
| _GizmoGroup
]
# Callback return type for EnumPropertyItems.
type ItemsCallback = Iterable[
# identifier, name, description
tuple[str, str, str]
# identifier, name, description, value
| tuple[str, str, str, int]
# identifier, name, description, icon, value
| tuple[str, str, str, str, int]
| None
]
# Override SettingsProperties to use Python native types.
class EXTENSION_EXAMPLE_SettingsProperties(_EXTENSION_EXAMPLE_SettingsProperties):
name: str
file_format: str
color_depth: str
# Override Scene type to include custom settings.
class Scene(_Scene):
example_settings: EXTENSION_EXAMPLE_SettingsProperties