fix [#36995] FBX Importer does not import fbx model
[blender-addons.git] / io_export_pc2.py
blob37953d531939d67f6def9a589f9c7359a86cefb7
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 bl_info = {
20 "name": "Export Pointcache Format(.pc2)",
21 "author": "Florian Meyer (tstscr)",
22 "version": (1, 0),
23 "blender": (2, 57, 0),
24 "location": "File > Export > Pointcache (.pc2)",
25 "description": "Export mesh Pointcache data (.pc2)",
26 "warning": "",
27 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
28 "Scripts/Import-Export/PC2_Pointcache_export",
29 "tracker_url": "https://projects.blender.org/tracker/index.php?"\
30 "func=detail&aid=24703",
31 "category": "Import-Export"}
33 """
34 Usage Notes:
36 in Maya Mel:
37 cacheFile -pc2 1 -pcf "<insert filepath of source>" -f "<insert target filename w/o extension>" -dir "<insert directory path for target>" -format "OneFile";
39 """
41 import bpy
42 from bpy.props import *
43 import mathutils, math, struct
44 from os import remove
45 import time
46 from bpy_extras.io_utils import ExportHelper
48 def getSampling(start, end, sampling):
49 samples = [start + x * sampling
50 for x in range(int((end - start) / sampling) + 1)]
51 return samples
53 def do_export(context, props, filepath):
54 mat_x90 = mathutils.Matrix.Rotation(-math.pi/2, 4, 'X')
55 ob = context.active_object
56 sc = context.scene
57 start = props.range_start
58 end = props.range_end
59 sampling = float(props.sampling)
60 apply_modifiers = props.apply_modifiers
61 me = ob.to_mesh(sc, apply_modifiers, 'PREVIEW')
62 vertCount = len(me.vertices)
63 sampletimes = getSampling(start, end, sampling)
64 sampleCount = len(sampletimes)
66 # Create the header
67 headerFormat='<12siiffi'
68 headerStr = struct.pack(headerFormat, b'POINTCACHE2\0',
69 1, vertCount, start, sampling, sampleCount)
71 file = open(filepath, "wb")
72 file.write(headerStr)
74 for frame in sampletimes:
75 sc.frame_set(frame)
76 me = ob.to_mesh(sc, apply_modifiers, 'PREVIEW')
78 if len(me.vertices) != vertCount:
79 file.close()
80 try:
81 remove(filepath)
82 except:
83 empty = open(filepath, 'w')
84 empty.write('DUMMIFILE - export failed\n')
85 empty.close()
86 print('Export failed. Vertexcount of Object is not constant')
87 return False
89 if props.world_space:
90 me.transform(ob.matrix_world)
91 if props.rot_x90:
92 me.transform(mat_x90)
94 for v in me.vertices:
95 thisVertex = struct.pack('<fff', float(v.co[0]),
96 float(v.co[1]),
97 float(v.co[2]))
98 file.write(thisVertex)
100 file.flush()
101 file.close()
102 return True
105 ###### EXPORT OPERATOR #######
106 class Export_pc2(bpy.types.Operator, ExportHelper):
107 """Export the active Object as a .pc2 Pointcache file"""
108 bl_idname = "export_shape.pc2"
109 bl_label = "Export Pointcache (.pc2)"
111 filename_ext = ".pc2"
113 rot_x90 = BoolProperty(name="Convert to Y-up",
114 description="Rotate 90 degrees around X to convert to y-up",
115 default=True,
117 world_space = BoolProperty(name="Export into Worldspace",
118 description="Transform the Vertexcoordinates into Worldspace",
119 default=False,
121 apply_modifiers = BoolProperty(name="Apply Modifiers",
122 description="Applies the Modifiers",
123 default=True,
125 range_start = IntProperty(name='Start Frame',
126 description='First frame to use for Export',
127 default=1,
129 range_end = IntProperty(name='End Frame',
130 description='Last frame to use for Export',
131 default=250,
133 sampling = EnumProperty(name='Sampling',
134 description='Sampling --> frames per sample (0.1 yields 10 samples per frame)',
135 items=(('0.01', '0.01', ''),
136 ('0.05', '0.05', ''),
137 ('0.1', '0.1', ''),
138 ('0.2', '0.2', ''),
139 ('0.25', '0.25', ''),
140 ('0.5', '0.5', ''),
141 ('1', '1', ''),
142 ('2', '2', ''),
143 ('3', '3', ''),
144 ('4', '4', ''),
145 ('5', '5', ''),
146 ('10', '10', ''),
148 default='1',
151 @classmethod
152 def poll(cls, context):
153 return context.active_object.type in {'MESH', 'CURVE', 'SURFACE', 'FONT'}
155 def execute(self, context):
156 start_time = time.time()
157 print('\n_____START_____')
158 props = self.properties
159 filepath = self.filepath
160 filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
162 exported = do_export(context, props, filepath)
164 if exported:
165 print('finished export in %s seconds' %((time.time() - start_time)))
166 print(filepath)
168 return {'FINISHED'}
170 def invoke(self, context, event):
171 wm = context.window_manager
173 if True:
174 # File selector
175 wm.fileselect_add(self) # will run self.execute()
176 return {'RUNNING_MODAL'}
177 elif True:
178 # search the enum
179 wm.invoke_search_popup(self)
180 return {'RUNNING_MODAL'}
181 elif False:
182 # Redo popup
183 return wm.invoke_props_popup(self, event)
184 elif False:
185 return self.execute(context)
188 ### REGISTER ###
190 def menu_func(self, context):
191 self.layout.operator(Export_pc2.bl_idname, text="Pointcache (.pc2)")
194 def register():
195 bpy.utils.register_module(__name__)
197 bpy.types.INFO_MT_file_export.append(menu_func)
198 #bpy.types.VIEW3D_PT_tools_objectmode.prepend(menu_func)
200 def unregister():
201 bpy.utils.unregister_module(__name__)
203 bpy.types.INFO_MT_file_export.remove(menu_func)
204 #bpy.types.VIEW3D_PT_tools_objectmode.remove(menu_func)
206 if __name__ == "__main__":
207 register()