Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added Plugins/asset_3ds.py
Empty file.
11 changes: 11 additions & 0 deletions Plugins/asset_blend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pathlib


category = "asset"
supported_types = {
".blend": "blend",
}


def loader(path: pathlib.Path):
pass
Empty file added Plugins/asset_fbx.py
Empty file.
Empty file added Plugins/asset_obj.py
Empty file.
Empty file added Plugins/texture_bitmap.py
Empty file.
5 changes: 5 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
from bpy.utils import previews
from bpy.props import EnumProperty, StringProperty, BoolVectorProperty

from . import plugin

bl_info = {
"name": "YAAM",
"version": (0, 1),
Expand Down Expand Up @@ -1517,6 +1519,9 @@ def register():
for cls in classes:
bpy.utils.register_class(cls)

# TODO: Use this to handle all plugin needs.
plugin_manager = plugin.PluginManager.register()

bpy.types.Scene.assets_dir = StringProperty(
name="Asset Folder",
subtype='DIR_PATH',
Expand Down
81 changes: 81 additions & 0 deletions plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import importlib.util
import os
import pathlib
from enum import Enum
from typing import Callable, List, Mapping

TypeMap = Mapping[str, str]
Loader = Callable[[pathlib.Path], None]


class Category(Enum):
ASSET = "asset"
TEXTURE = "texture"


class Plugin(object):
def __init__(self, category: Category, supported_type_map: TypeMap,
loader: Loader):
self.__category = category
self.__supported_type_map = supported_type_map
self.__loader = loader

def category(self) -> Category:
return self.__category

def can_run(self, file_path: pathlib.Path) -> bool:
return file_path.suffix.lower() in self.__supported_type_map

def run(self, file_path: pathlib.Path):
self.__loader(file_path)


class PluginManager(object):
def __init__(self):
self.__plugins: List[Plugin] = []
self.__search_paths: List[pathlib.Path] = []

def add_search_path(self, search_path: pathlib.Path):
self.__search_paths.append(search_path)

def register_plugins(self):
category_map = {
"asset": Category.ASSET,
"texture": Category.TEXTURE,
}

for search_path in self.__search_paths:
for module_path in search_path.glob("*.py"):
name = module_path.stem
spec = importlib.util.spec_from_file_location(name, module_path)
plugin = importlib.util.module_from_spec(spec)
spec.loader.exec_module(plugin)

if not hasattr(plugin, "loader"):
continue
if not hasattr(plugin, "category"):
continue
if not hasattr(plugin, "supported_types"):
continue

if plugin.category not in category_map:
raise RuntimeError("'{}' is not a valid category".format(plugin.category))

self.__plugins.append(Plugin(category_map[plugin.category], plugin.supported_types, plugin.loader))

@classmethod
def register(cls):
manager = cls()

# Internal plugin path
internal_plugin_path = pathlib.Path(__file__).parent / "Plugins"
manager.add_search_path(internal_plugin_path)

# Environment variable pugin path
search_paths = os.environ.get("YAAM_PLUGIN_PATHS", "")

if search_paths:
for search_path in search_paths.split(os.pathsep):
manager.add_search_path(pathlib.Path(search_path))

manager.register_plugins()