1 # SPDX-FileCopyrightText: 2010-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
6 "name": "Export Pointcache Format(.pc2)",
7 "author": "Florian Meyer (tstscr)",
10 "location": "File > Export > Pointcache (.pc2)",
11 "description": "Export mesh Pointcache data (.pc2)",
13 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/pc2.html",
14 "category": "Import-Export",
19 https://developer.blender.org/T34456
20 https://developer.blender.org/T25408
25 cacheFile -pc2 1 -pcf "<insert filepath of source>" -f "<insert target filename w/o extension>" -dir "<insert directory path for target>" -format "OneFile";
30 from bpy
.props
import BoolProperty
, IntProperty
, EnumProperty
32 from bpy_extras
.io_utils
import ExportHelper
40 def get_sampled_frames(start
, end
, sampling
):
41 return [math
.modf(start
+ x
* sampling
) for x
in range(int((end
- start
) / sampling
) + 1)]
44 def do_export(context
, props
, filepath
):
45 mat_x90
= mathutils
.Matrix
.Rotation(-math
.pi
/2, 4, 'X')
46 ob
= context
.active_object
48 start
= props
.range_start
50 sampling
= float(props
.sampling
)
51 apply_modifiers
= props
.apply_modifiers
54 depsgraph
= context
.evaluated_depsgraph_get()
55 me
= ob
.evaluated_get(depsgraph
).to_mesh()
58 vertCount
= len(me
.vertices
)
59 sampletimes
= get_sampled_frames(start
, end
, sampling
)
60 sampleCount
= len(sampletimes
)
63 headerFormat
= '<12siiffi'
64 headerStr
= struct
.pack(headerFormat
, b
'POINTCACHE2\0',
65 1, vertCount
, start
, sampling
, sampleCount
)
67 file = open(filepath
, "wb")
70 for frame
in sampletimes
:
71 # stupid modf() gives decimal part first!
72 sc
.frame_set(int(frame
[1]), subframe
=frame
[0])
74 me
= ob
.evaluated_get(depsgraph
).to_mesh()
78 if len(me
.vertices
) != vertCount
:
79 bpy
.data
.meshes
.remove(me
, do_unlink
=True)
84 empty
= open(filepath
, 'w')
85 empty
.write('DUMMIFILE - export failed\n')
87 print('Export failed. Vertexcount of Object is not constant')
91 me
.transform(ob
.matrix_world
)
96 thisVertex
= struct
.pack('<fff', float(v
.co
[0]),
99 file.write(thisVertex
)
102 ob
.evaluated_get(depsgraph
).to_mesh_clear()
104 me
= ob
.to_mesh_clear()
112 class Export_pc2(bpy
.types
.Operator
, ExportHelper
):
113 """Export the active Object as a .pc2 Pointcache file"""
114 bl_idname
= "export_shape.pc2"
115 bl_label
= "Export Pointcache (.pc2)"
117 filename_ext
= ".pc2"
119 rot_x90
: BoolProperty(
120 name
="Convert to Y-up",
121 description
="Rotate 90 degrees around X to convert to y-up",
123 world_space
: BoolProperty(
124 name
="Export into Worldspace",
125 description
="Transform the Vertexcoordinates into Worldspace",
127 apply_modifiers
: BoolProperty(
128 name
="Apply Modifiers",
129 description
="Applies the Modifiers",
131 range_start
: IntProperty(
133 description
='First frame to use for Export',
135 range_end
: IntProperty(
137 description
='Last frame to use for Export',
139 sampling
: EnumProperty(
141 description
='Sampling --> frames per sample (0.1 yields 10 samples per frame)',
142 items
=(('0.01', '0.01', ''),
143 ('0.05', '0.05', ''),
146 ('0.25', '0.25', ''),
159 def poll(cls
, context
):
160 obj
= context
.active_object
163 and obj
.type in {'MESH', 'CURVE', 'SURFACE', 'FONT'}
166 def execute(self
, context
):
167 start_time
= time
.time()
168 print('\n_____START_____')
169 props
= self
.properties
170 filepath
= self
.filepath
171 filepath
= bpy
.path
.ensure_ext(filepath
, self
.filename_ext
)
173 exported
= do_export(context
, props
, filepath
)
176 print('finished export in %s seconds' %
177 ((time
.time() - start_time
)))
182 def invoke(self
, context
, event
):
183 wm
= context
.window_manager
187 wm
.fileselect_add(self
) # will run self.execute()
188 return {'RUNNING_MODAL'}
191 wm
.invoke_search_popup(self
)
192 return {'RUNNING_MODAL'}
195 return wm
.invoke_props_popup(self
, event
)
197 return self
.execute(context
)
200 def menu_func_export_button(self
, context
):
201 self
.layout
.operator(Export_pc2
.bl_idname
, text
="Pointcache (.pc2)")
211 bpy
.utils
.register_class(cls
)
213 bpy
.types
.TOPBAR_MT_file_export
.append(menu_func_export_button
)
214 #bpy.types.VIEW3D_PT_tools_objectmode.prepend(menu_func_export_button)
218 bpy
.types
.TOPBAR_MT_file_export
.remove(menu_func_export_button
)
219 #bpy.types.VIEW3D_PT_tools_objectmode.remove(menu_func_export_button)
221 bpy
.utils
.unregister_class(cls
)
224 if __name__
== "__main__":