2025-07-01
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""
|
||||
Copyright (C) 2023 Adobe.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
# file: shader/callbacks.py
|
||||
# brief: Callbacks for shader presets parameters and outputs
|
||||
# author Adobe - 3D & Immersive
|
||||
# copyright 2023 Adobe Inc. All rights reserved.
|
||||
|
||||
|
||||
from ..utils import SUBSTANCE_Utils
|
||||
|
||||
|
||||
class SUBSTANCE_ShaderCallbacks():
|
||||
@staticmethod
|
||||
def on_shader_changed(self, context):
|
||||
_, _, _selected_preset = SUBSTANCE_Utils.get_selected_shader(context)
|
||||
_selected_preset.modified = True
|
||||
|
||||
@staticmethod
|
||||
def on_input_changed(self, context):
|
||||
SUBSTANCE_ShaderCallbacks.on_shader_changed(self, context)
|
||||
|
||||
@staticmethod
|
||||
def on_output_changed(self, context):
|
||||
SUBSTANCE_ShaderCallbacks.on_shader_changed(self, context)
|
||||
@@ -0,0 +1,113 @@
|
||||
"""
|
||||
Copyright (C) 2023 Adobe.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under theshad terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
# file: shader/manager.py
|
||||
# brief: Shader presets operation manager
|
||||
# author Adobe - 3D & Immersive
|
||||
# copyright 2023 Adobe Inc. All rights reserved.
|
||||
|
||||
|
||||
import os
|
||||
import traceback
|
||||
|
||||
from .shader import ShaderPreset
|
||||
from ..utils import SUBSTANCE_Utils
|
||||
from ..factory.shader import SUBSTANCE_ShaderFactory
|
||||
from ..common import (
|
||||
Code_Response,
|
||||
ADDON_ROOT
|
||||
)
|
||||
|
||||
|
||||
class SUBSTANCE_ShaderManager():
|
||||
def __init__(self):
|
||||
self.shaders = {}
|
||||
|
||||
def initialize(self, addon_prefs):
|
||||
_shaders_dir = os.path.join(ADDON_ROOT, "_presets/default").replace('\\', '/')
|
||||
for _filepath in os.listdir(_shaders_dir):
|
||||
# Load the shader presets
|
||||
_result = self.load(_filepath)
|
||||
if _result[0] != Code_Response.success:
|
||||
SUBSTANCE_Utils.log_data(
|
||||
"ERROR",
|
||||
"Substance file [{}] could not be loaded".format(
|
||||
_filepath),
|
||||
display=True)
|
||||
continue
|
||||
_shader_preset = ShaderPreset(_filepath, _result[1])
|
||||
_new_shader = addon_prefs.shaders.add()
|
||||
_new_shader.init(_shader_preset)
|
||||
self.register(_shader_preset)
|
||||
SUBSTANCE_Utils.log_data("INFO", "Shader Presets [{}] initialized...".format(_filepath))
|
||||
return (Code_Response.success, None)
|
||||
|
||||
def load(self, filename):
|
||||
try:
|
||||
_path = SUBSTANCE_Utils.get_shader_file(filename)
|
||||
_data = SUBSTANCE_Utils.get_json(_path)
|
||||
|
||||
return (Code_Response.success, _data)
|
||||
except Exception:
|
||||
return (Code_Response.shader_preset_load_error, None)
|
||||
|
||||
def register(self, shader):
|
||||
self.shaders[shader.filename] = shader
|
||||
_result = SUBSTANCE_ShaderFactory.register_class(shader)
|
||||
return _result
|
||||
|
||||
def remove_presets(self, shader_presets):
|
||||
try:
|
||||
|
||||
return Code_Response.success
|
||||
except Exception:
|
||||
SUBSTANCE_Utils.log_data("ERROR", "Exception - Shader Preset removal error:")
|
||||
SUBSTANCE_Utils.log_traceback(traceback.format_exc())
|
||||
return Code_Response.shader_preset_remove_error
|
||||
|
||||
def save_presets(self, shader_preset):
|
||||
try:
|
||||
_default_shader_file = os.path.join(
|
||||
ADDON_ROOT,
|
||||
"_presets/default/" + shader_preset["filename"]).replace('\\', '/')
|
||||
_custom_shader_file = os.path.join(
|
||||
ADDON_ROOT,
|
||||
"_presets/custom/" + shader_preset["filename"]).replace('\\', '/')
|
||||
_custom_shader_path = os.path.join(
|
||||
ADDON_ROOT,
|
||||
"_presets/custom").replace('\\', '/')
|
||||
|
||||
# Read Original Shader Preset
|
||||
if not os.path.exists(_default_shader_file):
|
||||
return Code_Response.shader_preset_default_not_exist_error
|
||||
|
||||
_data = SUBSTANCE_Utils.get_json(_default_shader_file)
|
||||
|
||||
_data["inputs"] = shader_preset["inputs"]
|
||||
_data["outputs"] = shader_preset["outputs"]
|
||||
|
||||
# Write Custom Shader Preset
|
||||
if not os.path.exists(_custom_shader_path):
|
||||
os.mkdir(_custom_shader_path)
|
||||
|
||||
SUBSTANCE_Utils.set_json(_custom_shader_file, _data)
|
||||
|
||||
return Code_Response.success
|
||||
|
||||
except Exception:
|
||||
SUBSTANCE_Utils.log_data("ERROR", "Exception - Shader Preset saving error:")
|
||||
SUBSTANCE_Utils.log_traceback(traceback.format_exc())
|
||||
return Code_Response.shader_preset_save_error
|
||||
@@ -0,0 +1,106 @@
|
||||
"""
|
||||
Copyright (C) 2023 Adobe.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
# file: shader/shader.py
|
||||
# brief: Shader preset class object definition
|
||||
# author Adobe - 3D & Immersive
|
||||
# copyright 2023 Adobe Inc. All rights reserved.
|
||||
|
||||
|
||||
from ..common import CLASS_SHADER_INPUTS
|
||||
|
||||
|
||||
class ShaderPreset_Output():
|
||||
def __init__(self, output):
|
||||
self.id = output["id"]
|
||||
self.label = output["label"]
|
||||
self.enabled = output["enabled"]
|
||||
self.optional = output["optional"]
|
||||
self.colorspace = output["colorspace"]
|
||||
self.format = output["format"]
|
||||
self.bitdepth = output["bitdepth"]
|
||||
self.normal = output["normal"] if "normal" in output else False
|
||||
|
||||
def to_json(self):
|
||||
_obj = {
|
||||
"id": self.id,
|
||||
"label": self.label,
|
||||
"enabled": self.enabled,
|
||||
"optional": self.optional,
|
||||
"colorspace": self.colorspace,
|
||||
"format": self.format,
|
||||
"bitdepth": self.bitdepth
|
||||
}
|
||||
if self.normal:
|
||||
_obj["normal"] = True
|
||||
return _obj
|
||||
|
||||
|
||||
class ShaderPreset_Input():
|
||||
def __init__(self, input):
|
||||
self.id = input["id"]
|
||||
self.label = input["label"]
|
||||
self.type = input["type"]
|
||||
self.default = input["default"]
|
||||
self.min = input["min"] if "min" in input else None
|
||||
self.max = input["max"] if "max" in input else None
|
||||
|
||||
def to_json(self):
|
||||
_obj = {
|
||||
"id": self.id,
|
||||
"label": self.label,
|
||||
"type": self.type,
|
||||
"default": self.default
|
||||
}
|
||||
if self.min is not None:
|
||||
_obj["min"] = self.min
|
||||
if self.max is not None:
|
||||
_obj["max"] = self.max
|
||||
return _obj
|
||||
|
||||
|
||||
class ShaderPreset():
|
||||
def __init__(self, filename, data):
|
||||
self.filename = filename
|
||||
self.name = filename.replace(".json", "")
|
||||
self.label = data["label"]
|
||||
self.inputs = {}
|
||||
self.outputs = {}
|
||||
self.inputs_class_name = CLASS_SHADER_INPUTS.format(filename.replace(".json", "").replace(" ", "_"))
|
||||
|
||||
for _key, _input in data["inputs"].items():
|
||||
_item = ShaderPreset_Input(_input)
|
||||
self.inputs[_key] = _item
|
||||
|
||||
for _key, _output in data["outputs"].items():
|
||||
_item = ShaderPreset_Output(_output)
|
||||
self.outputs[_key] = _item
|
||||
|
||||
def to_json(self):
|
||||
_obj = {
|
||||
"label": self.label,
|
||||
"inputs": {},
|
||||
"outputs": {},
|
||||
"inputs_class_name": self.inputs_class_name
|
||||
}
|
||||
|
||||
for _key, _input in self.inputs.items():
|
||||
_obj["inputs"][_key] = _input.to_json()
|
||||
|
||||
for _key, _output in self.outputs.items():
|
||||
_obj["outputs"][_key] = _output.to_json()
|
||||
|
||||
return _obj
|
||||
Reference in New Issue
Block a user