Spaces:
Paused
Paused
Upload code/cube3d/render/render_bricks_safe.py with huggingface_hub
Browse files
code/cube3d/render/render_bricks_safe.py
CHANGED
|
@@ -63,61 +63,43 @@ def _download_import_ldraw(target_root: Path) -> Path | None:
|
|
| 63 |
|
| 64 |
return None
|
| 65 |
|
| 66 |
-
def _resolve_import_ldraw_dir(
|
| 67 |
"""
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
"""
|
| 71 |
-
|
| 72 |
-
code_dir = script_path.parents[2]
|
| 73 |
-
repo_root = code_dir.parent
|
| 74 |
|
|
|
|
| 75 |
env_override = os.environ.get("IMPORT_LDRAW_DIR")
|
| 76 |
-
candidate_dirs: List[Path] = []
|
| 77 |
-
|
| 78 |
if env_override:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
if not candidate:
|
| 101 |
-
continue
|
| 102 |
-
try:
|
| 103 |
-
candidate = candidate.resolve()
|
| 104 |
-
except FileNotFoundError:
|
| 105 |
-
candidate = candidate.expanduser()
|
| 106 |
-
if candidate in seen:
|
| 107 |
-
continue
|
| 108 |
-
seen.append(candidate)
|
| 109 |
-
if (candidate / "__init__.py").exists():
|
| 110 |
-
return candidate
|
| 111 |
-
|
| 112 |
-
search_base = repo_root if repo_root.exists() else code_dir
|
| 113 |
-
try:
|
| 114 |
-
for sub in search_base.rglob("ImportLDraw"):
|
| 115 |
-
if (sub / "__init__.py").exists():
|
| 116 |
-
return sub
|
| 117 |
-
except Exception:
|
| 118 |
-
pass
|
| 119 |
-
|
| 120 |
-
return None
|
| 121 |
|
| 122 |
# 根据操作系统自动检测Blender路径
|
| 123 |
def _get_blender_path():
|
|
@@ -238,30 +220,17 @@ def render_bricks_safe(
|
|
| 238 |
|
| 239 |
import_ldraw_dir = import_ldraw_dir.resolve()
|
| 240 |
import_ldraw_parent = import_ldraw_dir.parent
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
root_dir,
|
| 245 |
-
root_dir.parent,
|
| 246 |
-
Path.cwd(),
|
| 247 |
-
Path.cwd() / "code"
|
| 248 |
-
]:
|
| 249 |
-
if not candidate:
|
| 250 |
-
continue
|
| 251 |
-
candidate_str = str(candidate.resolve())
|
| 252 |
-
if os.path.exists(candidate_str) and candidate_str not in extra_sys_paths:
|
| 253 |
-
extra_sys_paths.append(candidate_str)
|
| 254 |
|
| 255 |
print(f"🔌 ImportLDraw目录: {import_ldraw_dir}")
|
| 256 |
-
print(f"
|
| 257 |
|
|
|
|
| 258 |
blender_env = os.environ.copy()
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
if existing_pythonpath:
|
| 262 |
-
pythonpath_entries.append(existing_pythonpath)
|
| 263 |
-
blender_env["PYTHONPATH"] = os.pathsep.join(pythonpath_entries)
|
| 264 |
-
print(f"PYTHONPATH 将被设置为: {blender_env['PYTHONPATH']}")
|
| 265 |
|
| 266 |
# 修复布尔值格式化问题
|
| 267 |
blender_script = '''
|
|
@@ -271,55 +240,24 @@ import os
|
|
| 271 |
import sys
|
| 272 |
from pathlib import Path
|
| 273 |
import traceback
|
| 274 |
-
import importlib.util
|
| 275 |
|
| 276 |
print("=== Blender脚本开始执行 ===")
|
| 277 |
|
| 278 |
-
#
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
print(f"预设ImportLDraw搜索路径: {{IMPORT_LDRAW_SEARCH_PATHS}}")
|
| 283 |
-
print(f"ImportLDraw模块目录: {{IMPORT_LDRAW_MODULE_DIR}}")
|
| 284 |
-
|
| 285 |
-
for candidate in IMPORT_LDRAW_SEARCH_PATHS:
|
| 286 |
-
abs_candidate = os.path.abspath(candidate)
|
| 287 |
-
if os.path.exists(abs_candidate) and abs_candidate not in sys.path:
|
| 288 |
-
sys.path.insert(0, abs_candidate)
|
| 289 |
-
print(f"已注入sys.path: {{abs_candidate}}")
|
| 290 |
-
|
| 291 |
-
def _manual_import_importldraw(module_dir: Path):
|
| 292 |
-
init_file = module_dir / "__init__.py"
|
| 293 |
-
if not init_file.exists():
|
| 294 |
-
raise FileNotFoundError(f"ImportLDraw __init__.py 不存在: {{init_file}}")
|
| 295 |
-
spec = importlib.util.spec_from_file_location(
|
| 296 |
-
"ImportLDraw",
|
| 297 |
-
str(init_file),
|
| 298 |
-
submodule_search_locations=[str(module_dir)]
|
| 299 |
-
)
|
| 300 |
-
module = importlib.util.module_from_spec(spec)
|
| 301 |
-
sys.modules["ImportLDraw"] = module
|
| 302 |
-
spec.loader.exec_module(module)
|
| 303 |
-
return module
|
| 304 |
|
|
|
|
| 305 |
try:
|
| 306 |
-
print("
|
| 307 |
import ImportLDraw
|
| 308 |
from ImportLDraw.loadldraw.loadldraw import Options, Configure, loadFromFile, FileSystem
|
| 309 |
-
print("ImportLDraw
|
| 310 |
-
print(f"ImportLDraw文件位置: {{ImportLDraw.__file__}}")
|
| 311 |
except ImportError as import_error:
|
| 312 |
-
print(f"ImportLDraw
|
|
|
|
| 313 |
traceback.print_exc()
|
| 314 |
-
|
| 315 |
-
print("尝试手动加载 ImportLDraw...")
|
| 316 |
-
ImportLDraw = _manual_import_importldraw(IMPORT_LDRAW_MODULE_DIR)
|
| 317 |
-
from ImportLDraw.loadldraw.loadldraw import Options, Configure, loadFromFile, FileSystem
|
| 318 |
-
print("✅ 手动加载 ImportLDraw 成功")
|
| 319 |
-
except Exception as manual_error:
|
| 320 |
-
print(f"❌ 手动加载 ImportLDraw 失败: {{manual_error}}")
|
| 321 |
-
traceback.print_exc()
|
| 322 |
-
sys.exit(1)
|
| 323 |
|
| 324 |
try:
|
| 325 |
plugin_path = Path(ImportLDraw.__file__).parent
|
|
@@ -441,12 +379,11 @@ except Exception as e:
|
|
| 441 |
# 写入临时脚本 - 修复布尔值格式化
|
| 442 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 443 |
script_content = blender_script.format(
|
| 444 |
-
|
| 445 |
-
import_ldraw_module_dir=repr(str(import_ldraw_dir)),
|
| 446 |
ldraw_path=ldraw_lib_path,
|
| 447 |
-
instructions_look_bool=str(instructions_look),
|
| 448 |
-
reposition_camera_bool=str(reposition_camera),
|
| 449 |
-
square_image_bool=str(square_image),
|
| 450 |
fov=fov,
|
| 451 |
img_resolution=img_resolution,
|
| 452 |
input_file=in_file,
|
|
@@ -507,12 +444,15 @@ except Exception as e:
|
|
| 507 |
'--server-args', '-screen 0 1024x768x24 -ac +extension GLX +extension RENDER +render -noreset',
|
| 508 |
BLENDER_PATH,
|
| 509 |
'--background',
|
|
|
|
| 510 |
'--python', script_path
|
| 511 |
]
|
| 512 |
print("✅ 使用 xvfb-run 进行无头渲染(GLX 扩展已启用)")
|
| 513 |
else:
|
| 514 |
cmd = [
|
| 515 |
-
BLENDER_PATH,
|
|
|
|
|
|
|
| 516 |
'--python', script_path
|
| 517 |
]
|
| 518 |
print("⚠️ 直接运行 Blender(无 xvfb)")
|
|
|
|
| 63 |
|
| 64 |
return None
|
| 65 |
|
| 66 |
+
def _resolve_import_ldraw_dir() -> Path:
|
| 67 |
"""
|
| 68 |
+
Locate ImportLDraw plugin directory (simplified for Docker deployment).
|
| 69 |
+
|
| 70 |
+
Checks only 3 deterministic locations:
|
| 71 |
+
1. Environment variable IMPORT_LDRAW_DIR (for local dev override)
|
| 72 |
+
2. {repo}/code/ImportLDraw (Docker deployment path)
|
| 73 |
+
3. {repo}/code/ImportLDraw_auto (auto-downloaded fallback)
|
| 74 |
+
|
| 75 |
+
Raises FileNotFoundError if not found in any location.
|
| 76 |
"""
|
| 77 |
+
code_dir = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# 1. Environment variable override (local development)
|
| 80 |
env_override = os.environ.get("IMPORT_LDRAW_DIR")
|
|
|
|
|
|
|
| 81 |
if env_override:
|
| 82 |
+
path = Path(env_override).expanduser()
|
| 83 |
+
if (path / "__init__.py").exists():
|
| 84 |
+
print(f"✓ Using IMPORT_LDRAW_DIR: {path}")
|
| 85 |
+
return path
|
| 86 |
+
else:
|
| 87 |
+
print(f"⚠️ IMPORT_LDRAW_DIR set but invalid: {path}")
|
| 88 |
+
|
| 89 |
+
# 2. Docker deployment path / Repository bundled path
|
| 90 |
+
for subdir in ["ImportLDraw", "ImportLDraw_auto"]:
|
| 91 |
+
path = code_dir / subdir
|
| 92 |
+
if (path / "__init__.py").exists():
|
| 93 |
+
print(f"✓ Found ImportLDraw at: {path}")
|
| 94 |
+
return path
|
| 95 |
+
|
| 96 |
+
# Not found - fail fast with clear error
|
| 97 |
+
raise FileNotFoundError(
|
| 98 |
+
f"ImportLDraw not found in:\n"
|
| 99 |
+
f" - {code_dir / 'ImportLDraw'}\n"
|
| 100 |
+
f" - {code_dir / 'ImportLDraw_auto'}\n"
|
| 101 |
+
f"Set IMPORT_LDRAW_DIR environment variable or ensure Docker build succeeded."
|
| 102 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# 根据操作系统自动检测Blender路径
|
| 105 |
def _get_blender_path():
|
|
|
|
| 220 |
|
| 221 |
import_ldraw_dir = import_ldraw_dir.resolve()
|
| 222 |
import_ldraw_parent = import_ldraw_dir.parent
|
| 223 |
+
|
| 224 |
+
# Simplified: Single deterministic path for sys.path
|
| 225 |
+
import_ldraw_path = str(import_ldraw_parent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
print(f"🔌 ImportLDraw目录: {import_ldraw_dir}")
|
| 228 |
+
print(f"📍 ImportLDraw父目录: {import_ldraw_path}")
|
| 229 |
|
| 230 |
+
# Set PYTHONPATH for Blender (will work with --python-use-system-env flag)
|
| 231 |
blender_env = os.environ.copy()
|
| 232 |
+
blender_env["PYTHONPATH"] = import_ldraw_path
|
| 233 |
+
print(f"✓ PYTHONPATH 设置为: {import_ldraw_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
# 修复布尔值格式化问题
|
| 236 |
blender_script = '''
|
|
|
|
| 240 |
import sys
|
| 241 |
from pathlib import Path
|
| 242 |
import traceback
|
|
|
|
| 243 |
|
| 244 |
print("=== Blender脚本开始执行 ===")
|
| 245 |
|
| 246 |
+
# Simplified: Single deterministic sys.path (demo.zip style)
|
| 247 |
+
sys.path.append("{import_ldraw_path}")
|
| 248 |
+
print(f"✓ Added to sys.path: {{sys.path[-1]}}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
+
# Import ImportLDraw (fail fast if not found)
|
| 251 |
try:
|
| 252 |
+
print("Importing ImportLDraw...")
|
| 253 |
import ImportLDraw
|
| 254 |
from ImportLDraw.loadldraw.loadldraw import Options, Configure, loadFromFile, FileSystem
|
| 255 |
+
print(f"✓ ImportLDraw loaded from: {{ImportLDraw.__file__}}")
|
|
|
|
| 256 |
except ImportError as import_error:
|
| 257 |
+
print(f"❌ ImportLDraw import failed: {{import_error}}")
|
| 258 |
+
print(f" sys.path: {{sys.path}}")
|
| 259 |
traceback.print_exc()
|
| 260 |
+
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
try:
|
| 263 |
plugin_path = Path(ImportLDraw.__file__).parent
|
|
|
|
| 379 |
# 写入临时脚本 - 修复布尔值格式化
|
| 380 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 381 |
script_content = blender_script.format(
|
| 382 |
+
import_ldraw_path=import_ldraw_path, # Simplified: single path
|
|
|
|
| 383 |
ldraw_path=ldraw_lib_path,
|
| 384 |
+
instructions_look_bool=str(instructions_look),
|
| 385 |
+
reposition_camera_bool=str(reposition_camera),
|
| 386 |
+
square_image_bool=str(square_image),
|
| 387 |
fov=fov,
|
| 388 |
img_resolution=img_resolution,
|
| 389 |
input_file=in_file,
|
|
|
|
| 444 |
'--server-args', '-screen 0 1024x768x24 -ac +extension GLX +extension RENDER +render -noreset',
|
| 445 |
BLENDER_PATH,
|
| 446 |
'--background',
|
| 447 |
+
'--python-use-system-env', # CRITICAL: Enable PYTHONPATH support
|
| 448 |
'--python', script_path
|
| 449 |
]
|
| 450 |
print("✅ 使用 xvfb-run 进行无头渲染(GLX 扩展已启用)")
|
| 451 |
else:
|
| 452 |
cmd = [
|
| 453 |
+
BLENDER_PATH,
|
| 454 |
+
'--background',
|
| 455 |
+
'--python-use-system-env', # CRITICAL: Enable PYTHONPATH support
|
| 456 |
'--python', script_path
|
| 457 |
]
|
| 458 |
print("⚠️ 直接运行 Blender(无 xvfb)")
|