0xZohar's picture
Add code/cube3d/render/render_fbx.py
a706a8f verified
import bpy
import os
from mathutils import Vector
# 输入 .fbx 文件所在文件夹路径
fbx_folder = "/public/home/wangshuo/gap/assembly/data/ldr2fbx/" # 替换为你的文件夹路径
output_folder = "/public/home/wangshuo/gap/assembly/data/ldr2fbx/output_images/" # 输出图片的文件夹路径
# 创建输出文件夹(如果不存在)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 获取文件夹中所有的 .fbx 文件
fbx_files = [f for f in os.listdir(fbx_folder) if f.endswith('.fbx')]
for fbx_file in fbx_files:
fbx_path = os.path.join(fbx_folder, fbx_file)
# 获取文件名前缀,用作图片文件名
output_png = os.path.join(output_folder, f"{os.path.splitext(fbx_file)[0]}.png")
# 检查文件是否存在
if not os.path.exists(fbx_path):
print(f"FBX file not found at {fbx_path}")
continue
# 清空场景
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
# 导入 .fbx 文件
bpy.ops.import_scene.fbx(filepath=fbx_path)
# 获取场景中的第一个物体
target_object = bpy.context.scene.objects[0] # 获取场景中的第一个物体
# 计算物体的边界框(bounding box)
bbox = [target_object.matrix_world @ Vector(corner) for corner in target_object.bound_box]
min_x = min(v.x for v in bbox)
max_x = max(v.x for v in bbox)
min_y = min(v.y for v in bbox)
max_y = max(v.y for v in bbox)
min_z = min(v.z for v in bbox)
max_z = max(v.z for v in bbox)
# 计算物体的中心点和大小
center = (min_x + max_x) / 2, (min_y + max_y) / 2, (min_z + max_z) / 2
size = max(max_x - min_x, max_y - min_y, max_z - min_z)
# 确保场景中有一个相机
camera = bpy.context.scene.camera
if not camera:
bpy.ops.object.camera_add(location=(0, -8, 5))
camera = bpy.context.object # 设置相机为当前场景的相机
bpy.context.scene.camera = camera # 设置当前相机
# 设置相机位置(确保相机能看到整个物体)
camera.location = (center[0], center[1] - size * 6, center[2] + size) # 距离物体足够远
camera.rotation_euler = (1.1, 0, 0) # 使相机朝向物体
# 使用 Track To 约束让相机始终朝向物体
track_to_constraint = camera.constraints.new(type='TRACK_TO')
track_to_constraint.target = target_object
track_to_constraint.up_axis = 'UP_Y' # 确保相机正确对齐
track_to_constraint.track_axis = 'TRACK_NEGATIVE_Z' # 改为 track_axis
# # 更新相机,确保 Track To 约束生效后才进行旋转
# bpy.context.view_layer.update()
# # 在 Track To 约束生效后,绕 Z 轴旋转相机 45 度
# camera.rotation_euler[2] += 3.14 / 4 # 让相机绕 Z 轴旋转 45 度
# 设置渲染引擎(可以使用 Cycles 或 Eevee)
scene = bpy.context.scene
scene.render.engine = "CYCLES" # 或 "BLENDER_EEVEE"
scene.cycles.samples = 128 # 增加样本数
scene.render.resolution_x = 1024 # 设置分辨率
scene.render.resolution_y = 1024
scene.render.filepath = output_png
scene.cycles.device = 'GPU' # 设置为 GPU
if bpy.context.scene.cycles.device == 'GPU':
print("GPU rendering enabled")
else:
print("GPU not available, using CPU instead")
# 添加太阳光源
bpy.ops.object.light_add(type='SUN', location=(5, -5, 10))
light = bpy.context.object
light.data.energy = 10 # 增加光源强度
# 设置背景色(白色背景)
if scene.world is None:
bpy.ops.world.new() # 创建一个新的世界背景设置
scene.world.color = (1, 1, 1) # 设置背景颜色为白色
# 启用环境光
scene.world.use_nodes = True
world_nodes = scene.world.node_tree.nodes
background_node = world_nodes.get("Background")
background_node.inputs["Color"].default_value = (1, 1, 1, 1) # 设置背景为白色
# 渲染
bpy.ops.render.render(write_still=True)
print(f"Rendering complete for {fbx_file}, saved as {output_png}")