Skip to content

Commit

Permalink
feat: Make Vivy materials be sourced from a user defined path
Browse files Browse the repository at this point in the history
Signed-off-by: Mahid Sheikh <[email protected]>
  • Loading branch information
StandingPadAnimations committed Sep 6, 2024
1 parent c1f470b commit 610aaf3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
9 changes: 4 additions & 5 deletions MCprep_addon/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,15 @@ def __init__(self):
self.vivy_cache = None

# The JSON file for Vivy's materials
self.vivy_material_json: Optional[Dict] = None
self.reload_vivy_json() # Get latest JSON data
self.vivy_material_json: Dict = {}

# State for name changes in the Vivy config
#
# This is reverse, so the new name refers to the previous name
self.vivy_name_changes: Dict[str, str] = {}
self.vivy_name_changes: Dict[str, str] = {}

def reload_vivy_json(self) -> None:
json_path = Path(os.path.join(os.path.dirname(__file__), "MCprep_resources", "vivy_materials.json"))
def reload_vivy_json(self, path: Path) -> None:
json_path = Path(path, "vivy_materials.json")
if not json_path.exists():
json_path.touch()
self.vivy_material_json = {}
Expand Down
7 changes: 5 additions & 2 deletions MCprep_addon/materials/vivy_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ def set_material(context: Context, material: Material, options: VivyOptions) ->

def get_vivy_blend() -> Path:
"""Return the path of the Vivy material library"""
return Path(os.path.join(env.json_path.parent, "vivy_materials.blend"))
try:
return Path(os.path.join(bpy.context.scene.vivy_file_path, "vivy_materials.blend"))
except Exception:
return Path("")

def get_vivy_json() -> Path:
"""Return the path of the Vivy JSON file"""
return Path(os.path.join(env.json_path.parent, "vivy_materials.json"))
return Path(os.path.join(bpy.context.scene.vivy_file_path, "vivy_materials.json"))

def generate_vivy_materials(self, context, options: VivyOptions):
# Sync file stuff.
Expand Down
36 changes: 33 additions & 3 deletions MCprep_addon/mcprep_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# ##### END GPL LICENSE BLOCK #####

import os
from pathlib import Path
import time

# library imports
Expand Down Expand Up @@ -473,6 +474,11 @@ def change_verbose(self, context):
description="Enable Vivy material templating features",
default=False,
)
exp_vivy_file_path: bpy.props.StringProperty(
name="Vivy Folder",
description="Folder to source Vivy materials",
subtype='DIR_PATH',
default='//')

# addon updater preferences

Expand Down Expand Up @@ -567,6 +573,16 @@ def draw(self, context):
row = box.row()
row.label(text=env._("Effects folder not found"), icon="ERROR")

if util.is_vivy_enabled(context):
split = util.layout_split(box, factor=factor_width)
col = split.column()
col.label(text=env._("Vivy Path"))
col = split.column()
col.prop(self, "exp_vivy_file_path", text="")
if not os.path.isdir(bpy.path.abspath(self.exp_vivy_file_path)):
row = box.row()
row.label(text=env._("Vivy folder not found"), icon="ERROR")

row = layout.row()
row.scale_y = 0.7
row.label(text=env._("Texture / Resource packs"))
Expand Down Expand Up @@ -787,8 +803,6 @@ def draw(self, context):
col.label(text="Vivy tools")
col.operator("vivy.prep_materials", text="Prep materials")
col.operator("mcprep.open_file", text="Edit Vivy Material Library").file=str(vivy_materials.get_vivy_blend())
col.operator("vivy.export_library", text="Export Vivy Material Library")
col.operator("vivy.import_library", text="Import Previous Export")
else:
col.label(text=env._("MCprep tools"))
col.operator("mcprep.prep_materials", text=env._("Prep Materials"))
Expand Down Expand Up @@ -856,6 +870,13 @@ def draw(self, context):
row.prop(context.scene, "mcprep_texturepack_path", text="")
row.operator("mcprep.reset_texture_path", text="", icon=LOAD_FACTORY)

if util.is_vivy_enabled(context):
b_row = box.row()
b_col = b_row.column(align=False)
b_col.label(text=env._("Vivy Path"))
row = b_col.row(align=True)
row.prop(context.scene, "vivy_file_path", text="")

b_row = box.row()
b_col = b_row.column(align=True)
sync_row = b_col.row(align=True)
Expand Down Expand Up @@ -1921,6 +1942,9 @@ def mcprep_image_tools(self, context: Context) -> None:
else:
row.operator("mcprep.spawn_item", text=txt).filepath = path

def update_vivy_json(self, context: Context) -> None:
path = Path(bpy.path.abspath(context.scene.vivy_file_path))
env.reload_vivy_json(path)

# -----------------------------------------------
# Addon wide properties (aside from user preferences)
Expand Down Expand Up @@ -2077,7 +2101,12 @@ def register():
"with material prepping"),
update=update_mcprep_texturepack_path,
default=addon_prefs.custom_texturepack_path)

bpy.types.Scene.vivy_file_path = bpy.props.StringProperty(
name="Vivy Folder",
description="Folder to source Vivy materials",
subtype='DIR_PATH',
update=update_vivy_json,
default=addon_prefs.exp_vivy_file_path)
env.verbose = addon_prefs.verbose
if hasattr(bpy.types, "VIEW3D_MT_add"): # 2.8
bpy.types.VIEW3D_MT_add.append(draw_mcprepadd)
Expand Down Expand Up @@ -2106,3 +2135,4 @@ def unregister():
del bpy.types.Scene.entity_path
del bpy.types.Scene.mcprep_skin_path
del bpy.types.Scene.mcprep_texturepack_path
del bpy.types.Scene.vivy_file_path

0 comments on commit 610aaf3

Please sign in to comment.