Spaces:
Paused
Paused
Upload code/cube3d/render/render_bricks_safe.py with huggingface_hub
Browse files
code/cube3d/render/render_bricks_safe.py
CHANGED
|
@@ -5,10 +5,62 @@ from pathlib import Path
|
|
| 5 |
import sys
|
| 6 |
import os
|
| 7 |
import platform
|
|
|
|
| 8 |
|
| 9 |
root_dir = Path(__file__).resolve().parents[2]
|
| 10 |
sys.path.append(str(root_dir))
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# 根据操作系统自动检测Blender路径
|
| 13 |
def _get_blender_path():
|
| 14 |
"""自动检测Blender路径(支持Mac/Linux)"""
|
|
@@ -114,6 +166,40 @@ def render_bricks_safe(
|
|
| 114 |
else:
|
| 115 |
print(f" ⚠️ 警告:LDraw 库目录不存在!")
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# 修复布尔值格式化问题
|
| 118 |
blender_script = '''
|
| 119 |
import bpy
|
|
@@ -122,12 +208,36 @@ import os
|
|
| 122 |
import sys
|
| 123 |
from pathlib import Path
|
| 124 |
import traceback
|
|
|
|
| 125 |
|
| 126 |
print("=== Blender脚本开始执行 ===")
|
| 127 |
|
| 128 |
-
# 添加ImportLDraw
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
try:
|
| 133 |
print("尝试导入ImportLDraw...")
|
|
@@ -138,7 +248,15 @@ try:
|
|
| 138 |
except ImportError as import_error:
|
| 139 |
print(f"ImportLDraw导入失败: {{import_error}}")
|
| 140 |
traceback.print_exc()
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
try:
|
| 144 |
plugin_path = Path(ImportLDraw.__file__).parent
|
|
@@ -258,7 +376,8 @@ except Exception as e:
|
|
| 258 |
# 写入临时脚本 - 修复布尔值格式化
|
| 259 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 260 |
script_content = blender_script.format(
|
| 261 |
-
|
|
|
|
| 262 |
ldraw_path=ldraw_lib_path,
|
| 263 |
instructions_look_bool=str(instructions_look), # 直接使用Python布尔值
|
| 264 |
reposition_camera_bool=str(reposition_camera), # 直接使用Python布尔值
|
|
@@ -300,7 +419,13 @@ except Exception as e:
|
|
| 300 |
test_cmd = [BLENDER_PATH, '--version']
|
| 301 |
print(f"测试Blender: {' '.join(test_cmd)}")
|
| 302 |
try:
|
| 303 |
-
test_result = subprocess.run(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
print(f"Blender测试返回码: {test_result.returncode}")
|
| 305 |
if test_result.stdout:
|
| 306 |
print(f"Blender版本信息: {test_result.stdout.strip()}")
|
|
@@ -336,7 +461,8 @@ except Exception as e:
|
|
| 336 |
stdout=subprocess.PIPE,
|
| 337 |
stderr=subprocess.PIPE,
|
| 338 |
text=True,
|
| 339 |
-
timeout=300
|
|
|
|
| 340 |
)
|
| 341 |
|
| 342 |
# 输出Blender日志
|
|
@@ -424,4 +550,4 @@ def main():
|
|
| 424 |
print(f'渲染失败: {e}')
|
| 425 |
|
| 426 |
if __name__ == '__main__':
|
| 427 |
-
main()
|
|
|
|
| 5 |
import sys
|
| 6 |
import os
|
| 7 |
import platform
|
| 8 |
+
from typing import List
|
| 9 |
|
| 10 |
root_dir = Path(__file__).resolve().parents[2]
|
| 11 |
sys.path.append(str(root_dir))
|
| 12 |
|
| 13 |
+
def _resolve_import_ldraw_dir(additional_roots: List[Path] | None = None) -> Path | None:
|
| 14 |
+
"""
|
| 15 |
+
Attempt to locate the bundled ImportLDraw plugin directory.
|
| 16 |
+
Returns the directory containing ImportLDraw/__init__.py or None if not found.
|
| 17 |
+
"""
|
| 18 |
+
script_path = Path(__file__).resolve()
|
| 19 |
+
code_dir = script_path.parents[2]
|
| 20 |
+
repo_root = code_dir.parent
|
| 21 |
+
|
| 22 |
+
env_override = os.environ.get("IMPORT_LDRAW_DIR")
|
| 23 |
+
candidate_dirs: List[Path] = []
|
| 24 |
+
|
| 25 |
+
if env_override:
|
| 26 |
+
candidate_dirs.append(Path(env_override).expanduser())
|
| 27 |
+
|
| 28 |
+
candidate_dirs.extend([
|
| 29 |
+
code_dir / "ImportLDraw",
|
| 30 |
+
repo_root / "ImportLDraw",
|
| 31 |
+
repo_root / "code" / "ImportLDraw",
|
| 32 |
+
Path.cwd() / "ImportLDraw",
|
| 33 |
+
Path.cwd() / "code" / "ImportLDraw",
|
| 34 |
+
])
|
| 35 |
+
|
| 36 |
+
if additional_roots:
|
| 37 |
+
for root in additional_roots:
|
| 38 |
+
candidate_dirs.append(root / "ImportLDraw")
|
| 39 |
+
|
| 40 |
+
seen: List[Path] = []
|
| 41 |
+
for candidate in candidate_dirs:
|
| 42 |
+
if not candidate:
|
| 43 |
+
continue
|
| 44 |
+
try:
|
| 45 |
+
candidate = candidate.resolve()
|
| 46 |
+
except FileNotFoundError:
|
| 47 |
+
candidate = candidate.expanduser()
|
| 48 |
+
if candidate in seen:
|
| 49 |
+
continue
|
| 50 |
+
seen.append(candidate)
|
| 51 |
+
if (candidate / "__init__.py").exists():
|
| 52 |
+
return candidate
|
| 53 |
+
|
| 54 |
+
search_base = repo_root if repo_root.exists() else code_dir
|
| 55 |
+
try:
|
| 56 |
+
for sub in search_base.rglob("ImportLDraw"):
|
| 57 |
+
if (sub / "__init__.py").exists():
|
| 58 |
+
return sub
|
| 59 |
+
except Exception:
|
| 60 |
+
pass
|
| 61 |
+
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
# 根据操作系统自动检测Blender路径
|
| 65 |
def _get_blender_path():
|
| 66 |
"""自动检测Blender路径(支持Mac/Linux)"""
|
|
|
|
| 166 |
else:
|
| 167 |
print(f" ⚠️ 警告:LDraw 库目录不存在!")
|
| 168 |
|
| 169 |
+
import_ldraw_dir = _resolve_import_ldraw_dir()
|
| 170 |
+
if not import_ldraw_dir:
|
| 171 |
+
raise FileNotFoundError(
|
| 172 |
+
"未能找到 ImportLDraw 插件目录。请确保 code/ImportLDraw "
|
| 173 |
+
"已包含在部署包中,或设置环境变量 IMPORT_LDRAW_DIR 指向插件位置。"
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
import_ldraw_dir = import_ldraw_dir.resolve()
|
| 177 |
+
import_ldraw_parent = import_ldraw_dir.parent
|
| 178 |
+
extra_sys_paths: List[str] = []
|
| 179 |
+
for candidate in [
|
| 180 |
+
import_ldraw_parent,
|
| 181 |
+
root_dir,
|
| 182 |
+
root_dir.parent,
|
| 183 |
+
Path.cwd(),
|
| 184 |
+
Path.cwd() / "code"
|
| 185 |
+
]:
|
| 186 |
+
if not candidate:
|
| 187 |
+
continue
|
| 188 |
+
candidate_str = str(candidate.resolve())
|
| 189 |
+
if os.path.exists(candidate_str) and candidate_str not in extra_sys_paths:
|
| 190 |
+
extra_sys_paths.append(candidate_str)
|
| 191 |
+
|
| 192 |
+
print(f"🔌 ImportLDraw目录: {import_ldraw_dir}")
|
| 193 |
+
print(f"🔍 ImportLDraw可搜索路径: {extra_sys_paths}")
|
| 194 |
+
|
| 195 |
+
blender_env = os.environ.copy()
|
| 196 |
+
pythonpath_entries = extra_sys_paths.copy()
|
| 197 |
+
existing_pythonpath = blender_env.get("PYTHONPATH")
|
| 198 |
+
if existing_pythonpath:
|
| 199 |
+
pythonpath_entries.append(existing_pythonpath)
|
| 200 |
+
blender_env["PYTHONPATH"] = os.pathsep.join(pythonpath_entries)
|
| 201 |
+
print(f"PYTHONPATH 将被设置为: {blender_env['PYTHONPATH']}")
|
| 202 |
+
|
| 203 |
# 修复布尔值格式化问题
|
| 204 |
blender_script = '''
|
| 205 |
import bpy
|
|
|
|
| 208 |
import sys
|
| 209 |
from pathlib import Path
|
| 210 |
import traceback
|
| 211 |
+
import importlib.util
|
| 212 |
|
| 213 |
print("=== Blender脚本开始执行 ===")
|
| 214 |
|
| 215 |
+
# 添加ImportLDraw候选路径
|
| 216 |
+
IMPORT_LDRAW_SEARCH_PATHS = {import_ldraw_search_paths}
|
| 217 |
+
IMPORT_LDRAW_MODULE_DIR = Path({import_ldraw_module_dir})
|
| 218 |
+
|
| 219 |
+
print(f"预设ImportLDraw搜索路径: {{IMPORT_LDRAW_SEARCH_PATHS}}")
|
| 220 |
+
print(f"ImportLDraw模块目录: {{IMPORT_LDRAW_MODULE_DIR}}")
|
| 221 |
+
|
| 222 |
+
for candidate in IMPORT_LDRAW_SEARCH_PATHS:
|
| 223 |
+
abs_candidate = os.path.abspath(candidate)
|
| 224 |
+
if os.path.exists(abs_candidate) and abs_candidate not in sys.path:
|
| 225 |
+
sys.path.insert(0, abs_candidate)
|
| 226 |
+
print(f"已注入sys.path: {{abs_candidate}}")
|
| 227 |
+
|
| 228 |
+
def _manual_import_importldraw(module_dir: Path):
|
| 229 |
+
init_file = module_dir / "__init__.py"
|
| 230 |
+
if not init_file.exists():
|
| 231 |
+
raise FileNotFoundError(f"ImportLDraw __init__.py 不存在: {{init_file}}")
|
| 232 |
+
spec = importlib.util.spec_from_file_location(
|
| 233 |
+
"ImportLDraw",
|
| 234 |
+
str(init_file),
|
| 235 |
+
submodule_search_locations=[str(module_dir)]
|
| 236 |
+
)
|
| 237 |
+
module = importlib.util.module_from_spec(spec)
|
| 238 |
+
sys.modules["ImportLDraw"] = module
|
| 239 |
+
spec.loader.exec_module(module)
|
| 240 |
+
return module
|
| 241 |
|
| 242 |
try:
|
| 243 |
print("尝试导入ImportLDraw...")
|
|
|
|
| 248 |
except ImportError as import_error:
|
| 249 |
print(f"ImportLDraw导入失败: {{import_error}}")
|
| 250 |
traceback.print_exc()
|
| 251 |
+
try:
|
| 252 |
+
print("尝试手动加载 ImportLDraw...")
|
| 253 |
+
ImportLDraw = _manual_import_importldraw(IMPORT_LDRAW_MODULE_DIR)
|
| 254 |
+
from ImportLDraw.loadldraw.loadldraw import Options, Configure, loadFromFile, FileSystem
|
| 255 |
+
print("✅ 手动加载 ImportLDraw 成功")
|
| 256 |
+
except Exception as manual_error:
|
| 257 |
+
print(f"❌ 手动加载 ImportLDraw 失败: {{manual_error}}")
|
| 258 |
+
traceback.print_exc()
|
| 259 |
+
sys.exit(1)
|
| 260 |
|
| 261 |
try:
|
| 262 |
plugin_path = Path(ImportLDraw.__file__).parent
|
|
|
|
| 376 |
# 写入临时脚本 - 修复布尔值格式化
|
| 377 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 378 |
script_content = blender_script.format(
|
| 379 |
+
import_ldraw_search_paths=repr(extra_sys_paths),
|
| 380 |
+
import_ldraw_module_dir=repr(str(import_ldraw_dir)),
|
| 381 |
ldraw_path=ldraw_lib_path,
|
| 382 |
instructions_look_bool=str(instructions_look), # 直接使用Python布尔值
|
| 383 |
reposition_camera_bool=str(reposition_camera), # 直接使用Python布尔值
|
|
|
|
| 419 |
test_cmd = [BLENDER_PATH, '--version']
|
| 420 |
print(f"测试Blender: {' '.join(test_cmd)}")
|
| 421 |
try:
|
| 422 |
+
test_result = subprocess.run(
|
| 423 |
+
test_cmd,
|
| 424 |
+
capture_output=True,
|
| 425 |
+
text=True,
|
| 426 |
+
timeout=10,
|
| 427 |
+
env=blender_env
|
| 428 |
+
)
|
| 429 |
print(f"Blender测试返回码: {test_result.returncode}")
|
| 430 |
if test_result.stdout:
|
| 431 |
print(f"Blender版本信息: {test_result.stdout.strip()}")
|
|
|
|
| 461 |
stdout=subprocess.PIPE,
|
| 462 |
stderr=subprocess.PIPE,
|
| 463 |
text=True,
|
| 464 |
+
timeout=300,
|
| 465 |
+
env=blender_env
|
| 466 |
)
|
| 467 |
|
| 468 |
# 输出Blender日志
|
|
|
|
| 550 |
print(f'渲染失败: {e}')
|
| 551 |
|
| 552 |
if __name__ == '__main__':
|
| 553 |
+
main()
|