Add import hashlib and formatted with black

This commit is contained in:
Richard Brice
2026-04-20 07:26:40 -07:00
parent 61f715c148
commit c031a0edf4
+11 -9
View File
@@ -49,19 +49,17 @@ import bonsai.tool as tool
from pathlib import Path from pathlib import Path
from urllib.request import urlretrieve from urllib.request import urlretrieve
from urllib.parse import urlsplit, urlunsplit, quote from urllib.parse import urlsplit, urlunsplit, quote
import hashlib
CACHE_DIR = Path.home() / ".blender_texture_cache" CACHE_DIR = Path.home() / ".blender_texture_cache"
CACHE_DIR.mkdir(exist_ok=True) CACHE_DIR.mkdir(exist_ok=True)
def encode_url(url: str) -> str: def encode_url(url: str) -> str:
# make sure the URL is clean (e.g., special characters like spaces are encoded) # make sure the URL is clean (e.g., special characters like spaces are encoded)
parts = urlsplit(url) parts = urlsplit(url)
return urlunsplit(( return urlunsplit((parts.scheme, parts.netloc, quote(parts.path), parts.query, parts.fragment))
parts.scheme,
parts.netloc,
quote(parts.path),
parts.query,
parts.fragment
))
def get_cached_path(url: str) -> Path: def get_cached_path(url: str) -> Path:
# Hash the URL -> unique filename # Hash the URL -> unique filename
@@ -69,12 +67,14 @@ def get_cached_path(url: str) -> Path:
ext = Path(urlsplit(url).path).suffix or ".img" ext = Path(urlsplit(url).path).suffix or ".img"
return CACHE_DIR / f"{h}{ext}" return CACHE_DIR / f"{h}{ext}"
def load_image_once(path: str): def load_image_once(path: str):
for img in bpy.data.images: for img in bpy.data.images:
if img.filepath == path: if img.filepath == path:
return img return img
return bpy.data.images.load(path) return bpy.data.images.load(path)
# Progressively we'll refactor loading elements into Blender objects into this # Progressively we'll refactor loading elements into Blender objects into this
# class. This will break down the monolithic import_ifc module and allow us to # class. This will break down the monolithic import_ifc module and allow us to
# partially load and unload objects for huge models, partial model editing, and # partially load and unload objects for huge models, partial model editing, and
@@ -330,7 +330,7 @@ class Loader(bonsai.core.tool.Loader):
# IFC2X3 uses UrlReference, IFC4+ uses URLReference. # IFC2X3 uses UrlReference, IFC4+ uses URLReference.
original_image_url = texture.get("URLReference") or texture.get("UrlReference", "") original_image_url = texture.get("URLReference") or texture.get("UrlReference", "")
is_remote = original_image_url.startswith(("http://","https://")) is_remote = original_image_url.startswith(("http://", "https://"))
if is_remote: if is_remote:
try: try:
@@ -345,7 +345,9 @@ class Loader(bonsai.core.tool.Loader):
return load_image_once(str(cache_path)) return load_image_once(str(cache_path))
except Exception as e: except Exception as e:
print(f"WARNING. Failed to load remote texture {original_image_url}, it will be skipped: {e}") print(
f"WARNING. Failed to load remote texture {original_image_url}, it will be skipped: {e}"
)
return return
else: else:
is_relative = not os.path.isabs(original_image_url) is_relative = not os.path.isabs(original_image_url)