0xZohar commited on
Commit
bec2965
·
verified ·
1 Parent(s): 2c7448f

Fix: Use bpy.context instead of None in loadFromFile call

Browse files
code/cube3d/render/render_bricks_safe.py CHANGED
@@ -5,11 +5,64 @@ from pathlib import Path
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.
@@ -27,10 +80,15 @@ def _resolve_import_ldraw_dir(additional_roots: List[Path] | None = None) -> Pat
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:
@@ -167,6 +225,11 @@ def render_bricks_safe(
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 "
@@ -318,8 +381,15 @@ try:
318
  print("尝试加载LDR文件...")
319
  ldr_file = FileSystem.locate("{input_file}")
320
  print(f"找到LDR文件: {{ldr_file}}")
321
- loadFromFile(None, ldr_file)
322
- print("LDR文件加载成功")
 
 
 
 
 
 
 
323
 
324
  # 检查加载的对象
325
  mesh_objects = [obj for obj in bpy.data.objects if obj.type == 'MESH']
 
5
  import sys
6
  import os
7
  import platform
8
+ import shutil
9
+ import urllib.request
10
+ import zipfile
11
  from typing import List
12
 
13
  root_dir = Path(__file__).resolve().parents[2]
14
  sys.path.append(str(root_dir))
15
 
16
+
17
+ def _download_import_ldraw(target_root: Path) -> Path | None:
18
+ """
19
+ Download ImportLDraw from GitHub releases if it wasn't bundled in the repo.
20
+ Returns the extracted directory or None if download failed.
21
+ """
22
+ default_url = (
23
+ "https://github.com/TobyLobster/ImportLDraw/archive/refs/tags/v1.2.1.zip"
24
+ )
25
+ download_url = os.environ.get("IMPORT_LDRAW_DOWNLOAD_URL", default_url)
26
+ target_dir = target_root / "ImportLDraw_auto"
27
+
28
+ print("⚠️ 未在项目中找到 ImportLDraw,尝试从 GitHub 下载...")
29
+ print(f"🌐 下载地址: {download_url}")
30
+
31
+ try:
32
+ with tempfile.TemporaryDirectory(prefix="importldraw_dl_") as tmpdir:
33
+ tmpdir_path = Path(tmpdir)
34
+ zip_path = tmpdir_path / "importldraw.zip"
35
+ urllib.request.urlretrieve(download_url, zip_path)
36
+ print("✅ 已下载 ImportLDraw 压缩包")
37
+
38
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
39
+ zip_ref.extractall(tmpdir_path)
40
+
41
+ extracted_root = None
42
+ for child in tmpdir_path.iterdir():
43
+ if child.is_dir() and (child / "__init__.py").exists():
44
+ extracted_root = child
45
+ break
46
+ if child.is_dir():
47
+ potential = child / "ImportLDraw"
48
+ if (potential / "__init__.py").exists():
49
+ extracted_root = potential
50
+ break
51
+
52
+ if not extracted_root:
53
+ print("❌ 无法在压缩包中找到 ImportLDraw 目录")
54
+ return None
55
+
56
+ if target_dir.exists():
57
+ shutil.rmtree(target_dir)
58
+ shutil.copytree(extracted_root, target_dir)
59
+ print(f"✅ ImportLDraw 已下载到: {target_dir}")
60
+ return target_dir
61
+ except Exception as download_error:
62
+ print(f"❌ ImportLDraw 下载失败: {download_error}")
63
+
64
+ return None
65
+
66
  def _resolve_import_ldraw_dir(additional_roots: List[Path] | None = None) -> Path | None:
67
  """
68
  Attempt to locate the bundled ImportLDraw plugin directory.
 
80
 
81
  candidate_dirs.extend([
82
  code_dir / "ImportLDraw",
83
+ code_dir / "ImportLDraw_auto",
84
  repo_root / "ImportLDraw",
85
+ repo_root / "ImportLDraw_auto",
86
  repo_root / "code" / "ImportLDraw",
87
+ repo_root / "code" / "ImportLDraw_auto",
88
  Path.cwd() / "ImportLDraw",
89
+ Path.cwd() / "ImportLDraw_auto",
90
  Path.cwd() / "code" / "ImportLDraw",
91
+ Path.cwd() / "code" / "ImportLDraw_auto",
92
  ])
93
 
94
  if additional_roots:
 
225
  print(f" ⚠️ 警告:LDraw 库目录不存在!")
226
 
227
  import_ldraw_dir = _resolve_import_ldraw_dir()
228
+ if not import_ldraw_dir:
229
+ autogenerated_dir = _download_import_ldraw(root_dir)
230
+ if autogenerated_dir and (autogenerated_dir / "__init__.py").exists():
231
+ import_ldraw_dir = autogenerated_dir
232
+
233
  if not import_ldraw_dir:
234
  raise FileNotFoundError(
235
  "未能找到 ImportLDraw 插件目录。请确保 code/ImportLDraw "
 
381
  print("尝试加载LDR文件...")
382
  ldr_file = FileSystem.locate("{input_file}")
383
  print(f"找到LDR文件: {{ldr_file}}")
384
+
385
+ # 使用 bpy.context 而不是 None,避免 NoneType subscriptable 错误
386
+ try:
387
+ loadFromFile(bpy.context, ldr_file)
388
+ print("LDR文件加载成功")
389
+ except Exception as load_error:
390
+ print(f"❌ loadFromFile 失败: {{load_error}}")
391
+ traceback.print_exc()
392
+ sys.exit(1)
393
 
394
  # 检查加载的对象
395
  mesh_objects = [obj for obj in bpy.data.objects if obj.type == 'MESH']