0xZohar commited on
Commit
d687b4f
·
verified ·
1 Parent(s): 5b58e07

Add code/cube3d/render/render_fbx_cycle.py

Browse files
code/cube3d/render/render_fbx_cycle.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import bpy
2
+ import os
3
+ import math
4
+ from mathutils import Vector
5
+
6
+ # 输入 .fbx 文件夹路径和输出图片路径
7
+ fbx_folder = "/public/home/wangshuo/gap/assembly/data/ldr2fbx/"
8
+ output_folder = "/public/home/wangshuo/gap/assembly/data/ldr2fbx/output_images/"
9
+ angle_step = 30 # 每隔多少角度渲染一张
10
+
11
+ if not os.path.exists(output_folder):
12
+ os.makedirs(output_folder)
13
+
14
+ fbx_files = [f for f in os.listdir(fbx_folder) if f.endswith('.fbx')]
15
+
16
+ for fbx_file in fbx_files:
17
+ fbx_path = os.path.join(fbx_folder, fbx_file)
18
+ model_name = os.path.splitext(fbx_file)[0]
19
+ model_output_folder = os.path.join(output_folder, model_name)
20
+ os.makedirs(model_output_folder, exist_ok=True)
21
+
22
+ if not os.path.exists(fbx_path):
23
+ print(f"FBX file not found at {fbx_path}")
24
+ continue
25
+
26
+ # 清空场景
27
+ bpy.ops.object.select_all(action='SELECT')
28
+ bpy.ops.object.delete()
29
+
30
+ # 导入 .fbx 文件
31
+ bpy.ops.import_scene.fbx(filepath=fbx_path)
32
+
33
+ target_object = bpy.context.scene.objects[0]
34
+
35
+ bbox = [target_object.matrix_world @ Vector(corner) for corner in target_object.bound_box]
36
+ min_x = min(v.x for v in bbox)
37
+ max_x = max(v.x for v in bbox)
38
+ min_y = min(v.y for v in bbox)
39
+ max_y = max(v.y for v in bbox)
40
+ min_z = min(v.z for v in bbox)
41
+ max_z = max(v.z for v in bbox)
42
+ center = ((min_x + max_x) / 2, (min_y + max_y) / 2, (min_z + max_z) / 2)
43
+ size = max(max_x - min_x, max_y - min_y, max_z - min_z)
44
+
45
+ # 添加太阳光源
46
+ bpy.ops.object.light_add(type='SUN', location=(5, -5, 10))
47
+ light = bpy.context.object
48
+ light.data.energy = 10
49
+
50
+ # 设置背景
51
+ scene = bpy.context.scene
52
+ scene.render.engine = "CYCLES"
53
+ scene.cycles.samples = 128
54
+ scene.render.resolution_x = 1024
55
+ scene.render.resolution_y = 1024
56
+ scene.world.use_nodes = True
57
+ bg_node = scene.world.node_tree.nodes.get("Background")
58
+ if bg_node:
59
+ bg_node.inputs["Color"].default_value = (1, 1, 1, 1)
60
+
61
+ # 添加相机并设置为场景主相机
62
+ bpy.ops.object.camera_add()
63
+ camera = bpy.context.object
64
+ scene.camera = camera
65
+
66
+ # 开启 GPU 渲染
67
+ scene.cycles.device = 'GPU'
68
+
69
+ # 环绕渲染
70
+ for angle_deg in range(0, 360, angle_step):
71
+ angle_rad = math.radians(angle_deg)
72
+ radius = size * 3
73
+
74
+ cam_x = center[0] + radius * math.cos(angle_rad)
75
+ cam_y = center[1] + radius * math.sin(angle_rad)
76
+ cam_z = center[2] + size * 0.5 # 稍高一点
77
+
78
+ camera.location = (cam_x, cam_y, cam_z)
79
+
80
+ # 删除已有约束
81
+ for c in camera.constraints:
82
+ camera.constraints.remove(c)
83
+
84
+ # 使用约束朝向目标
85
+ constraint = camera.constraints.new(type='TRACK_TO')
86
+ constraint.target = target_object
87
+ constraint.track_axis = 'TRACK_NEGATIVE_Z'
88
+ constraint.up_axis = 'UP_Y'
89
+
90
+ # 设置输出文件名
91
+ frame_output = os.path.join(model_output_folder, f"{model_name}_{angle_deg:03d}.png")
92
+ scene.render.filepath = frame_output
93
+
94
+ # 渲染当前角度
95
+ bpy.ops.render.render(write_still=True)
96
+ print(f"Rendered {frame_output}")