File size: 3,145 Bytes
d687b4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import bpy
import os
import math
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/"
angle_step = 30  # 每隔多少角度渲染一张

if not os.path.exists(output_folder):
    os.makedirs(output_folder)

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)
    model_name = os.path.splitext(fbx_file)[0]
    model_output_folder = os.path.join(output_folder, model_name)
    os.makedirs(model_output_folder, exist_ok=True)

    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]

    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)

    # 添加太阳光源
    bpy.ops.object.light_add(type='SUN', location=(5, -5, 10))
    light = bpy.context.object
    light.data.energy = 10

    # 设置背景
    scene = bpy.context.scene
    scene.render.engine = "CYCLES"
    scene.cycles.samples = 128
    scene.render.resolution_x = 1024
    scene.render.resolution_y = 1024
    scene.world.use_nodes = True
    bg_node = scene.world.node_tree.nodes.get("Background")
    if bg_node:
        bg_node.inputs["Color"].default_value = (1, 1, 1, 1)

    # 添加相机并设置为场景主相机
    bpy.ops.object.camera_add()
    camera = bpy.context.object
    scene.camera = camera

    # 开启 GPU 渲染
    scene.cycles.device = 'GPU'

    # 环绕渲染
    for angle_deg in range(0, 360, angle_step):
        angle_rad = math.radians(angle_deg)
        radius = size * 3

        cam_x = center[0] + radius * math.cos(angle_rad)
        cam_y = center[1] + radius * math.sin(angle_rad)
        cam_z = center[2] + size * 0.5  # 稍高一点

        camera.location = (cam_x, cam_y, cam_z)

        # 删除已有约束
        for c in camera.constraints:
            camera.constraints.remove(c)

        # 使用约束朝向目标
        constraint = camera.constraints.new(type='TRACK_TO')
        constraint.target = target_object
        constraint.track_axis = 'TRACK_NEGATIVE_Z'
        constraint.up_axis = 'UP_Y'

        # 设置输出文件名
        frame_output = os.path.join(model_output_folder, f"{model_name}_{angle_deg:03d}.png")
        scene.render.filepath = frame_output

        # 渲染当前角度
        bpy.ops.render.render(write_still=True)
        print(f"Rendered {frame_output}")