armory/blender/arm/ui_icons.py
Moritz Brückner ee1b55184c Move icon code into module
This, combined with lazy loading, has the advantage of using icons in property definitions before the actual registration code runs
2021-01-15 19:26:31 +01:00

35 lines
1 KiB
Python

"""
Blender user interface icon handling.
"""
import os.path
from typing import Optional
import bpy.utils.previews
_icons_dict: Optional[bpy.utils.previews.ImagePreviewCollection] = None
"""Dictionary of all loaded icons, or `None` if not loaded"""
_icons_dir = os.path.join(os.path.dirname(__file__), "custom_icons")
"""Directory of the icon files"""
def _load_icons() -> None:
"""(Re)loads all icons"""
global _icons_dict
if _icons_dict is not None:
bpy.utils.previews.remove(_icons_dict)
_icons_dict = bpy.utils.previews.new()
_icons_dict.load("armory", os.path.join(_icons_dir, "armory.png"), 'IMAGE')
_icons_dict.load("bundle", os.path.join(_icons_dir, "bundle.png"), 'IMAGE')
_icons_dict.load("haxe", os.path.join(_icons_dir, "haxe.png"), 'IMAGE')
_icons_dict.load("wasm", os.path.join(_icons_dir, "wasm.png"), 'IMAGE')
def get_id(identifier: str) -> int:
"""Returns the icon ID from the given identifier"""
if _icons_dict is None:
_load_icons()
return _icons_dict[identifier].icon_id