Skip to content

Commit 21ecdd2

Browse files
committed
Refactor logging for asset thread management, enhance Blender binary path retrieval, and improve target format handling in GLTF generation
1 parent 220df48 commit 21ecdd2

File tree

6 files changed

+55
-16
lines changed

6 files changed

+55
-16
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"args": [
2020
"check",
2121
"--exclude", "scripts,tests,_debug,_bck,*.tmp,__pycache__,build,.venv,*.pyi,pic_lib.py",
22-
".",
22+
"."
2323
],
2424
"console": "integratedTerminal",
2525
"cwd": "${workspaceFolder}"

blenderkit_server_utils/concurrency.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ def _thread_target(
8181
) from e
8282

8383
try:
84-
logger.debug("Starting thread for asset %s", asset)
8584
asset_keys_snapshot = (
8685
asset.get("id", ""),
8786
asset.get("assetType", ""),
8887
asset.get("name", "N/A"),
8988
)
89+
90+
logger.debug("Starting thread for asset %s", asset_keys_snapshot)
91+
9092
t = threading.Thread(
9193
target=_thread_target,
9294
args=(tuple(call_args), static_kwargs, asset_keys_snapshot),

blenderkit_server_utils/send_to_bg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ def _run_blender(command: list[str], verbosity_level: int) -> int:
206206
"""Run Blender with the given command and stream output per verbosity."""
207207
stdout_val, stderr_val = subprocess.PIPE, subprocess.PIPE
208208
logger.info("Running Blender command: %s", command)
209+
logger.debug("Raw command: %s", " ".join(command))
209210
with subprocess.Popen(command, stdout=stdout_val, stderr=stderr_val, creationflags=get_process_flags()) as proc:
210211
if verbosity_level == VERBOSITY_ALL:
211212
stdout_thread = threading.Thread(
@@ -333,6 +334,7 @@ def send_to_bg( # noqa: PLR0913
333334
Process return code from Blender.
334335
"""
335336
binary_path = _select_binary_path(binary_path, asset_data, asset_file_path=asset_file_path, binary_type=binary_type)
337+
336338
temp_folder, own_temp_folder = _ensure_temp_folder(temp_folder)
337339
payload = DataPayload(
338340
file_path=asset_file_path,

blenderkit_server_utils/utils.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,25 @@ def get_all_blender_versions(blenders_path: str | None) -> list[tuple[float, str
486486
continue
487487
try:
488488
version = version_to_float(fn)
489+
# add also full executable in in the directory
490+
if sys.platform == "win32":
491+
exe_path = os.path.join(blenders_path, fn, "blender.exe")
492+
elif sys.platform == "darwin":
493+
exe_path = os.path.join(blenders_path, fn, "blender.app", "Contents", "MacOS", "blender")
494+
else: # assume linux
495+
exe_path = os.path.join(blenders_path, fn, "blender")
496+
497+
if not os.path.exists(exe_path):
498+
continue
499+
489500
blenders.append((version, fn))
490501
except ValueError:
491502
continue
492503
return blenders
493504

494505

495-
def get_latest_blender_path(blenders_path: str | None) -> str | None:
496-
"""Get the path to the latest Blender version in the specified directory.
506+
def get_latest_blender_binary_path(blenders_path: str | None) -> str | None:
507+
"""Get the path to the latest Blender binary version in the specified directory.
497508
498509
Args:
499510
blenders_path: Directory containing Blender installations. If None, uses config.BLENDERS_PATH.
@@ -513,8 +524,22 @@ def get_latest_blender_path(blenders_path: str | None) -> str | None:
513524
logger.error("No Blender versions found in %s", blenders_path)
514525
return None
515526

527+
latest_blender = all_blenders[0][1]
528+
# Handle different OS paths
529+
if sys.platform == "darwin": # macOS
530+
binary = os.path.join(
531+
blenders_path,
532+
latest_blender,
533+
"Contents",
534+
"MacOS",
535+
"Blender",
536+
)
537+
else: # Windows and Linux
538+
ext = ".exe" if sys.platform == "win32" else ""
539+
binary = os.path.join(blenders_path, latest_blender, f"blender{ext}")
540+
516541
# Return the path to the latest Blender version
517-
out = os.path.join(blenders_path, all_blenders[0][1])
542+
out = binary if os.path.exists(binary) else None
518543
return out
519544

520545

generate_gltf.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,43 @@
3131
# get latest version from there
3232
if not config.BLENDER_PATH and config.BLENDERS_PATH:
3333
logger.error("BLENDER_PATH not set, checking BLENDERS_PATH, result may be tainted.")
34-
latest_version = utils.get_latest_blender_path(config.BLENDERS_PATH)
34+
latest_version = utils.get_latest_blender_binary_path(config.BLENDERS_PATH)
3535
if latest_version:
3636
config.BLENDER_PATH = latest_version
3737

3838
if not config.BLENDER_PATH:
3939
logger.error("At least one of BLENDER_PATH & BLENDERS_PATH must be set.")
4040
sys.exit(1)
4141

42+
# Constants
43+
AVAILABLE_TARGET_FORMATS = ["gltf", "gltf_godot"]
44+
DEFAULT_TARGET_FORMAT: str = "gltf_godot"
4245

46+
# argparse for target format
4347
args = argparse.ArgumentParser()
44-
args.add_argument("--target_format", type=str, default="gltf_godot", help="Target export format")
48+
args.add_argument(
49+
"--target_format",
50+
type=str,
51+
choices=AVAILABLE_TARGET_FORMATS,
52+
default=DEFAULT_TARGET_FORMAT,
53+
help="Target export format",
54+
)
4555

4656

47-
# Constants
57+
# Determine target format from args, env var, or default
4858
TARGET_FORMAT = args.parse_args().target_format
4959
if TARGET_FORMAT:
5060
logger.info("Using target format from args: %s", TARGET_FORMAT)
5161
if not TARGET_FORMAT:
5262
# use env var or default
5363
TARGET_FORMAT = os.getenv("TARGET_FORMAT", None)
54-
if not TARGET_FORMAT:
55-
logger.info("No target format specified, defaulting to 'gltf_godot'")
5664

5765
if not TARGET_FORMAT:
58-
logger.error("No target format specified, defaulting to 'gltf_godot'")
59-
TARGET_FORMAT = "gltf_godot"
66+
logger.error("No target format specified, defaulting to %s", DEFAULT_TARGET_FORMAT)
67+
TARGET_FORMAT = DEFAULT_TARGET_FORMAT
6068

6169
# target mode can be only one of these 2 now
62-
if TARGET_FORMAT not in ["gltf", "gltf_godot"]:
70+
if TARGET_FORMAT not in AVAILABLE_TARGET_FORMATS:
6371
logger.error("Invalid target format specified: %s", TARGET_FORMAT)
6472
sys.exit(1)
6573

@@ -87,7 +95,7 @@ def generate_gltf(asset_data: dict[str, Any], api_key: str, binary_path: str, ta
8795
asset_data: Asset metadata returned from the search API.
8896
api_key: API key used for authenticated operations.
8997
binary_path: Absolute path to the Blender binary used for background operations.
90-
target_format: The target export format, e.g., 'gltf_godot'.
98+
target_format: The target export format, e.g., '{DEFAULT_TARGET_FORMAT=gltf_godot}'.
9199
92100
Returns:
93101
True when the GLTF was generated and uploaded successfully; False otherwise.
@@ -144,9 +152,10 @@ def generate_gltf(asset_data: dict[str, Any], api_key: str, binary_path: str, ta
144152

145153
if SKIP_UPDATE:
146154
logger.info("SKIP_UPDATE is set, not patching the asset.")
147-
opened = utils.open_folder(os.path.dirname(files[0]["path"]))
155+
logger.debug("Generated files: %s", files)
156+
opened = utils.open_folder(os.path.dirname(files[0]["file_path"]))
148157
if not opened:
149-
logger.error("Failed to open folder %s", os.path.dirname(files[0]["path"]))
158+
logger.error("Failed to open folder %s", os.path.dirname(files[0]["file_path"]))
150159
utils.cleanup_temp(temp_folder)
151160

152161
return False

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ target-version = "py311"
6161
line-length = 120
6262
include = ["pyproject.toml",
6363
"**/*.py",
64+
"*.py"
6465
]
6566
exclude = [
6667
"_debug/**",

0 commit comments

Comments
 (0)