Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Migration and Refactoring for 3.6.0 #549

Merged
merged 9 commits into from
May 1, 2024
8 changes: 7 additions & 1 deletion MCprep_addon/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self):
# list of material names, each is a string. None by default to indicate
# that no reading has occurred. If lib not found, will update to [].
# If ever changing the resource pack, should also reset to None.
self.material_sync_cache = []
self.material_sync_cache: Optional[List] = []

# Whether we use PO files directly or use the converted form
self.use_direct_i18n = False
Expand Down Expand Up @@ -303,10 +303,16 @@ class MCprepError(object):
Path of file the exception object
was created in. The preferred way
to get this is __file__

msg: Optional[str]
Optional message to display for an
exception. Use this if the exception
type may not be so clear cut
"""
err_type: BaseException
line: int
file: str
msg: Optional[str] = None

env = MCprepEnv()

Expand Down
219 changes: 0 additions & 219 deletions MCprep_addon/materials/default_materials.py

This file was deleted.

68 changes: 40 additions & 28 deletions MCprep_addon/materials/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# ##### END GPL LICENSE BLOCK #####

import os
from typing import Dict, Optional, List, Any, Tuple, Union
from typing import Dict, Optional, List, Any, Tuple, Union, cast
from pathlib import Path
from dataclasses import dataclass
from enum import Enum
Expand All @@ -26,7 +26,7 @@
from bpy.types import Context, Material, Image, Texture, Nodes, NodeLinks, Node

from .. import util
from ..conf import env, Form
from ..conf import MCprepError, env, Form

AnimatedTex = Dict[str, int]

Expand Down Expand Up @@ -125,43 +125,51 @@ def get_mc_canonical_name(name: str) -> Tuple[str, Optional[Form]]:
return canon, form


def find_from_texturepack(blockname: str, resource_folder: Optional[Path]=None) -> Path:
def find_from_texturepack(blockname: str, resource_folder: Optional[Path]=None) -> Union[Path, MCprepError]:
"""Given a blockname (and resource folder), find image filepath.

Finds textures following any pack which should have this structure, and
the input folder or default resource folder could target at any of the
following sublevels above the <subfolder> level.
//pack_name/assets/minecraft/textures/<subfolder>/<blockname.png>

Returns:
- Path if successful
- MCprepError if error occurs (may return with a message)
"""
if not resource_folder:
if resource_folder is None:
# default to internal pack
resource_folder = bpy.path.abspath(bpy.context.scene.mcprep_texturepack_path)
resource_folder = Path(cast(
str,
bpy.path.abspath(bpy.context.scene.mcprep_texturepack_path)
))

if not os.path.isdir(resource_folder):
if not resource_folder.exists() or not resource_folder.is_dir():
env.log("Error, resource folder does not exist")
return
line, file = env.current_line_and_file()
return MCprepError(FileNotFoundError(), line, file, f"Resource pack folder at {resource_folder} does not exist!")

# Check multiple paths, picking the first match (order is important),
# goal of picking out the /textures folder.
check_dirs = [
os.path.join(resource_folder, "textures"),
os.path.join(resource_folder, "minecraft", "textures"),
os.path.join(resource_folder, "assets", "minecraft", "textures")]
Path(resource_folder, "textures"),
Path(resource_folder, "minecraft", "textures"),
Path(resource_folder, "assets", "minecraft", "textures")]
for path in check_dirs:
if os.path.isdir(path):
if path.exists():
resource_folder = path
break

search_paths = [
resource_folder,
# Both singular and plural shown below as it has varied historically.
os.path.join(resource_folder, "blocks"),
os.path.join(resource_folder, "block"),
os.path.join(resource_folder, "items"),
os.path.join(resource_folder, "item"),
os.path.join(resource_folder, "entity"),
os.path.join(resource_folder, "models"),
os.path.join(resource_folder, "model"),
Path(resource_folder, "blocks"),
Path(resource_folder, "block"),
Path(resource_folder, "items"),
Path(resource_folder, "item"),
Path(resource_folder, "entity"),
Path(resource_folder, "models"),
Path(resource_folder, "model"),
]
res = None

Expand All @@ -170,32 +178,36 @@ def find_from_texturepack(blockname: str, resource_folder: Optional[Path]=None)
if "/" in blockname:
newpath = blockname.replace("/", os.path.sep)
for ext in extensions:
if os.path.isfile(os.path.join(resource_folder, newpath + ext)):
res = os.path.join(resource_folder, newpath + ext)
if Path(resource_folder, newpath + ext).exists():
res = Path(resource_folder, newpath + ext)
return res
newpath = os.path.basename(blockname) # case where goes into other subpaths
for ext in extensions:
if os.path.isfile(os.path.join(resource_folder, newpath + ext)):
res = os.path.join(resource_folder, newpath + ext)
if Path(resource_folder, newpath + ext).exists():
res = Path(resource_folder, newpath + ext)
return res

# fallback (more common case), wide-search for
for path in search_paths:
if not os.path.isdir(path):
if not path.is_dir():
continue
for ext in extensions:
check_path = os.path.join(path, blockname + ext)
if os.path.isfile(check_path):
res = os.path.join(path, blockname + ext)
check_path = Path(path, blockname + ext)
if check_path.exists() and check_path.is_file():
res = Path(path, blockname + ext)
return res

# Mineways fallback
for suffix in ["-Alpha", "-RGB", "-RGBA"]:
if blockname.endswith(suffix):
res = os.path.join(
res = Path(
resource_folder, "mineways_assets", f"mineways{suffix}.png")
if os.path.isfile(res):
if res.exists() and res.is_file():
return res

if res is None:
line, file = env.current_line_and_file()
return MCprepError(FileNotFoundError(), line, file)
return res


Expand Down
1 change: 0 additions & 1 deletion MCprep_addon/mcprep_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,6 @@ def draw(self, context):
return

row = layout.row()
# row.operator("mcprep.create_default_material")
split = layout.split()
col = split.column(align=True)

Expand Down
Loading