-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportAnimationAsSTLs.py
78 lines (57 loc) · 2.38 KB
/
ExportAnimationAsSTLs.py
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
import os
import bpy
bl_info = {
"name": "Export animation frames as mesh files",
"category": "Import-Export",
"blender": (2, 90, 0),
"location": "File > Export",
}
class frameExportMesh(bpy.types.Operator):
"""Export animation frames as mesh files."""
bl_idname = "frame.export_mesh"
bl_label = "Export frames as mesh files"
bl_options = {'REGISTER'}
def _get_camera(self, context):
# TEST ! context = bpy.context
camera = context.scene.camera
render = context.scene.render
# Get the two components to calculate M
modelview_matrix = camera.matrix_world.inverted()
projection_matrix = camera.calc_matrix_camera(
# bpy.data.scenes["Scene"].view_layers["RenderLayer"].depsgraph,
context.scene.view_layers[0].depsgraph,
x=render.resolution_x,
y=render.resolution_y,
scale_x=render.pixel_aspect_x,
scale_y=render.pixel_aspect_y,
)
# print(projection_matrix * modelview_matrix)
# Compute P’ = M * P
return projection_matrix @ modelview_matrix
def execute(self, context):
the_path = bpy.path.abspath('/tmp')
scene = context.scene
for frame in range(scene.frame_start, scene.frame_end):
scene.frame_set(frame)
# To rename a scene, just hit F2 on it
fname = f"{scene.name}_{scene.frame_current:06}.ply"
# https://docs.blender.org/api/current/bpy.ops.export_mesh.html
# https://developer.blender.org/diffusion/BA/browse/master/io_mesh_ply/export_ply.py
bpy.ops.export_mesh.ply(filepath=os.path.join(the_path, fname),
use_ascii=True, use_normals=True)
fname = f"{scene.name}_{scene.frame_current:06}.cam"
matrix = self._get_camera(context)
with open(os.path.join(the_path, fname), "w") as fout:
for i in range(4):
fout.write(f"{matrix[i][:]}\n")
return {'FINISHED'}
def menu_draw(self, context):
self.layout.operator("frame.export_mesh")
def register():
bpy.utils.register_class(frameExportMesh)
bpy.types.TOPBAR_MT_file_export.append(menu_draw)
def unregister():
bpy.utils.unregister_class(frameExportMesh)
bpy.types.TOPBAR_MT_file_export.remove(menu_draw)
if __name__ == "__main__":
register()