This commit is contained in:
nicomacbookpro
2025-08-28 15:10:35 +08:00
8 changed files with 560 additions and 13 deletions

View File

@@ -3,6 +3,19 @@ import json
import mathutils
import os
import bmesh
import sys
# 获取命令行参数
def get_json_path():
"""从命令行参数获取JSON文件路径"""
if len(sys.argv) > 5: # Blender传递的参数格式: blender --background --python script.py -- json_path
# 查找 -- 分隔符后的参数
for i, arg in enumerate(sys.argv):
if arg == "--" and i + 1 < len(sys.argv):
return sys.argv[i + 1]
# 如果没有找到参数,使用默认路径
return "./e8caffb4622e03b1495bbc1ed13fce13.json"
# 清空场景
bpy.ops.wm.read_factory_settings(use_empty=True)
@@ -11,12 +24,23 @@ bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.context.scene.render.engine = 'CYCLES'
# 加载 JSON 文件
json_path = "./e8caffb4622e03b1495bbc1ed13fce13.json"
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
json_path = get_json_path()
print(f"尝试加载JSON文件{json_path}")
print(f"已加载JSON文件{json_path}")
print(f"包含 {len(data['children'])} 个对象")
try:
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
print(f"✅ 成功加载JSON文件{json_path}")
print(f"包含 {len(data['children'])} 个对象")
except FileNotFoundError:
print(f"❌ 错误找不到JSON文件 {json_path}")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"❌ 错误JSON文件格式错误 {e}")
sys.exit(1)
except Exception as e:
print(f"❌ 错误加载JSON文件时出错 {e}")
sys.exit(1)
# 创建金属材质
def create_metal_material():
@@ -69,7 +93,7 @@ nothing_mat = create_nothing_material()
emissive_mat = create_emissive_material()
# 创建更精确的抛物面
def create_parabolic_surface(face_f, size=2.0, resolution=32):
def create_parabolic_surface(face_f, size=1.0, resolution=32):
bm = bmesh.new()
# 创建抛物面网格
@@ -109,7 +133,7 @@ def create_parabolic_surface(face_f, size=2.0, resolution=32):
return obj
# 创建双曲面
def create_hyperbolic_surface(face_f, face_g, size=2.0, resolution=32):
def create_hyperbolic_surface(face_f, face_g, size=1.0, resolution=32):
bm = bmesh.new()
# 创建双曲面网格
@@ -171,28 +195,41 @@ def create_geometry(obj_data):
try:
if face_geom == "plane":
bpy.ops.mesh.primitive_plane_add(size=2)
# 创建圆形平面而不是方形
bpy.ops.mesh.primitive_circle_add(vertices=32, radius=0.5)
mesh = bpy.context.active_object
# 填充圆形
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.edge_face_add()
bpy.ops.object.mode_set(mode='OBJECT')
elif face_geom == "circle":
# 创建圆形平面
bpy.ops.mesh.primitive_circle_add(vertices=32, radius=0.5)
mesh = bpy.context.active_object
# 填充圆形
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.edge_face_add()
bpy.ops.object.mode_set(mode='OBJECT')
elif face_geom == "parabola":
if face_f < 1e9 and face_f > 0.1: # 使用精确抛物面
mesh = create_parabolic_surface(face_f)
else: # 使用简化圆锥
bpy.ops.mesh.primitive_cone_add(vertices=32, radius1=1.5, radius2=0.1, depth=2.0)
bpy.ops.mesh.primitive_cone_add(vertices=32, radius1=0.75, radius2=0.05, depth=1.0)
mesh = bpy.context.active_object
elif face_geom == "hyperbola":
if abs(face_f) > 0.1 and abs(face_g) > 0.1: # 使用精确双曲面
mesh = create_hyperbolic_surface(face_f, face_g)
else: # 使用简化环面
bpy.ops.mesh.primitive_torus_add(major_radius=1.0, minor_radius=0.3)
bpy.ops.mesh.primitive_torus_add(major_radius=0.5, minor_radius=0.15)
mesh = bpy.context.active_object
else:
# 默认立方体
bpy.ops.mesh.primitive_cube_add(size=1.0)
bpy.ops.mesh.primitive_cube_add(size=0.5)
mesh = bpy.context.active_object
except Exception as e:
print(f"创建几何体时出错: {e}")
# 备用:创建简单立方体
bpy.ops.mesh.primitive_cube_add(size=0.5)
bpy.ops.mesh.primitive_cube_add(size=0.25)
mesh = bpy.context.active_object
# 设置对象属性