WIP: MaterialX addon #104594

Closed
Bogdan Nagirniak wants to merge 34 commits from BogdanNagirniak/blender-addons:materialx-addon into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
5 changed files with 24 additions and 21 deletions
Showing only changes of commit d2fc8f497c - Show all commits

View File

@ -18,7 +18,7 @@ bl_info = {
"category": "Material",
}
ADDON_PREFIX = "materialx"
ADDON_ALIAS = "materialx"
import bpy

View File

@ -4,13 +4,13 @@
import sys
import logging.handlers
from . import ADDON_PREFIX
from . import ADDON_ALIAS
FORMAT_STR = "%(asctime)s %(levelname)s %(name)s [%(thread)d]: %(message)s"
# root logger for the addon
logger = logging.getLogger(ADDON_PREFIX)
logger = logging.getLogger(ADDON_ALIAS)
logger.setLevel('DEBUG')
# file_handler = logging.handlers.RotatingFileHandler(PLUGIN_ROOT_DIR / 'usdhydra.log',

View File

@ -10,9 +10,7 @@ import bpy
from .. import utils
from ..node_tree import MxNodeTree
from ..utils import mx as mx_utils
from .manager import manager
#from .. import config
from ..utils import logging
log = logging.Log('matlib.ui')
@ -24,7 +22,7 @@ class MATERIAL_OP_matlib_clear_search(bpy.types.Operator):
bl_label = ""
def execute(self, context):
context.window_manager.materialx.matlib.search = ''
utils.mx_properties(context.window_manager).matlib.search = ''
return {"FINISHED"}
@ -44,23 +42,23 @@ class MATLIB_OP_import_material(bpy.types.Operator):
bl_label = "Import Material Package"
def execute(self, context):
matlib_prop = context.window_manager.materialx.matlib
matlib_prop = utils.mx_properties(context.window_manager).matlib
package = matlib_prop.package
mtlx_file = package.unzip()
# getting/creating MxNodeTree
bl_material = context.material
mx_node_tree = bl_material.materialx.mx_node_tree
if not bl_material.materialx.mx_node_tree:
mx_node_tree = utils.mx_properties(bl_material).mx_node_tree
if not mx_node_tree:
mx_node_tree = bpy.data.node_groups.new(f"MX_{bl_material.name}",
type=MxNodeTree.bl_idname)
bl_material.materialx.mx_node_tree = mx_node_tree
utils.mx_properties(bl_material).mx_node_tree = mx_node_tree
log(f"Reading: {mtlx_file}")
doc = mx.createDocument()
search_path = mx.FileSearchPath(str(mtlx_file.parent))
search_path.append(str(mx_utils.MX_LIBS_DIR))
search_path.append(str(utils.MX_LIBS_DIR))
try:
mx.readFromXmlFile(doc, str(mtlx_file), searchPath=search_path)
mx_node_tree.import_(doc, mtlx_file)
@ -78,7 +76,7 @@ class MATLIB_OP_load_package(bpy.types.Operator):
bl_label = "Download Package"
def execute(self, context):
matlib_prop = context.window_manager.materialx.matlib
matlib_prop = utils.mx_properties(context.window_manager).matlib
manager.load_package(matlib_prop.package)
return {"FINISHED"}
@ -94,7 +92,7 @@ class MATLIB_PT_matlib(bpy.types.Panel):
def draw(self, context):
layout = self.layout
matlib_prop = context.window_manager.materialx.matlib
matlib_prop = utils.mx_properties(context.window_manager).matlib
manager.check_load_materials()

View File

@ -7,7 +7,8 @@ from collections import defaultdict
import MaterialX as mx
from .. import utils
from ..utils import NODE_CLASSES_DIR
from .. import ADDON_ALIAS
from .. import logging
log = logging.Log("nodes.generate_node_classes")
@ -273,7 +274,7 @@ from bpy.props import (
PointerProperty,
FloatVectorProperty,
)
from materialx.nodes.node import MxNode
from {ADDON_ALIAS}.nodes.node import MxNode
FILE_PATH = r"{file_path}"
@ -311,7 +312,7 @@ mx_node_classes = [{', '.join(mx_node_class_names)}]
def generate_basic_classes():
gen_code_dir = NODE_CLASSES_DIR
gen_code_dir = utils.NODE_CLASSES_DIR
gen_code_dir.mkdir(exist_ok=True)
files = [

View File

@ -9,14 +9,14 @@ import shutil
import MaterialX as mx
import bpy
from . import ADDON_PREFIX
from . import ADDON_ALIAS
from . import logging
log = logging.Log('utils')
ADDON_ROOT_DIR = Path(__file__).parent
ADDON_DATA_DIR = Path(bpy.utils.user_resource("SCRIPTS", path=f"addons/{ADDON_PREFIX}", create=True))
ADDON_DATA_DIR = Path(bpy.utils.user_resource("SCRIPTS", path=f"addons/{ADDON_ALIAS}", create=True))
MX_LIBS_FOLDER = "libraries"
MX_LIBS_DIR = ADDON_ROOT_DIR / MX_LIBS_FOLDER
@ -31,7 +31,7 @@ os.environ['MATERIALX_SEARCH_PATH'] = str(MX_LIBS_DIR)
def with_prefix(name, separator='.', upper=False):
return f"{ADDON_PREFIX.upper() if upper else ADDON_PREFIX}{separator}{name}"
return f"{ADDON_ALIAS.upper() if upper else ADDON_ALIAS}{separator}{name}"
def title_str(val):
@ -396,7 +396,7 @@ class MaterialXProperties(bpy.types.PropertyGroup):
@classmethod
def register(cls):
setattr(cls.bl_type, "materialx", bpy.props.PointerProperty(
setattr(cls.bl_type, ADDON_ALIAS, bpy.props.PointerProperty(
name="MaterialX properties",
description="MaterialX properties",
type=cls,
@ -404,4 +404,8 @@ class MaterialXProperties(bpy.types.PropertyGroup):
@classmethod
def unregister(cls):
delattr(cls.bl_type, "materialx")
delattr(cls.bl_type, ADDON_ALIAS)
def mx_properties(obj):
return getattr(obj, ADDON_ALIAS)