Mesh Face Info Select addon: added wiki link
[blender-addons-contrib.git] / io_import_voodoo_camera.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
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.
7 #
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.
12 #
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.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 bl_info = {
20     "name": "Import Voodoo camera",
21     "author": "Fazekas Laszlo",
22     "version": (0, 8),
23     "blender": (2, 6, 3),
24     "location": "File > Import > Voodoo camera",
25     "description": "Imports a Blender (2.4x or 2.5x) Python script from the Voodoo (version 1.1 or 1.2) camera tracker software.",
26     "warning": "",
27     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
28         "Scripts/Import-Export/Voodoo_camera",
29     "tracker_url": "https://projects.blender.org/tracker/index.php?"\
30         "func=detail&aid=22510",
31     "category": "Import-Export"}
32
33 """
34 This script loads a Blender Python script from the Voodoo camera
35 tracker program into Blender 2.5x+.
36
37 It processes the script as a text file and not as a Python executable
38 because of the incompatible Python APIs of Blender 2.4x/2.5x/2.6x.
39 """
40
41 import bpy
42 from bpy.props import *
43 import mathutils
44 import os
45 import string
46 import math
47
48 def voodoo_import(infile,ld_cam,ld_points):
49
50     checktype = os.path.splitext(infile)[1]
51
52     if checktype.upper() != '.PY':
53         print ("Selected file: ",infile)
54         raise IOError("The selected input file is not a *.py file")
55         return
56
57     print ("--------------------------------------------------")
58     print ("Importing Voodoo file: ", infile)
59
60     file = open(infile,'rU')
61     scene = bpy.context.scene
62     initfr = scene.frame_current
63     b24= True
64     voodoo_import.frwas= False
65
66     dummy = bpy.data.objects.new('voodoo_scene', None)
67     dummy.location = (0.0, 0.0, 0.0)
68     dummy.rotation_euler = ( -3.141593/2, 0.0, 0.0)
69     dummy.scale = (0.2, 0.2, 0.2)
70     scene.objects.link(dummy)
71
72     if ld_cam:
73         data = bpy.data.cameras.new('voodoo_render_cam')
74         data.lens_unit= 'DEGREES'
75         vcam = bpy.data.objects.new('voodoo_render_cam', data)
76         vcam.location = (0.0, 0.0, 0.0)
77         vcam.rotation_euler = (0.0, 0.0, 0.0)
78         vcam.scale = (1.0, 1.0, 1.0)
79         data.lens = 35.0
80         data.shift_x = 0.0
81         data.shift_y = 0.0
82         data.dof_distance = 0.0
83         data.clip_start = 0.1
84         data.clip_end = 1000.0
85         data.draw_size = 0.5
86         scene.objects.link(vcam)
87         vcam.parent = dummy
88
89     if ld_points:
90         data = bpy.data.meshes.new('voodoo_FP3D_cloud')
91         mesh = bpy.data.objects.new('voodoo_FP3D_cloud', data)
92         mesh.location = (0.0, 0.0, 0.0)
93         # before 6.3
94         # mesh.rotation_euler = (3.141593/2, 0.0, 0.0)
95         # mesh.scale = (5.0, 5.0, 5.0)
96         mesh.rotation_euler = (0.0, 0.0, 0.0)
97         mesh.scale = (1.0, 1.0, 1.0)
98         scene.objects.link(mesh)
99         mesh.parent = dummy
100
101     verts = []
102
103     def stri(s):
104         try:
105             ret= int(s,10)
106
107         except ValueError :
108             ret= -1
109
110         return ret
111
112     def process_line(line):
113         lineSplit = line.split()
114
115         if (len(lineSplit) < 1): return
116
117         if (line[0] == '#'): return
118
119         if b24:
120
121             # Blender 2.4x mode
122             # process camera commands
123
124             if ld_cam:
125                 if (line[0] == 'c' and line[1] != 'r'):
126                     pos= line.find('.lens')
127
128                     if (pos != -1):
129                         fr = stri(lineSplit[0][1:pos])
130
131                         if (fr >= 0):
132                             scene.frame_current = fr
133                             vcam.data.lens= float(lineSplit[2])
134                             vcam.data.keyframe_insert('lens')
135                             return
136
137                 if (line[0] == 'o'):
138                     pos= line.find('.setMatrix')
139
140                     if (pos != -1):
141                         fr = stri(lineSplit[0][1:pos])
142
143                         if (fr >= 0):
144                             scene.frame_current = fr
145                             # for up to 2.55
146                             # vcam.matrix_world = eval('mathutils.' + line.rstrip()[pos+21:-1])
147                             # for 2.56, from Michael (Meikel) Oetjen 
148                             # vcam.matrix_world = eval('mathutils.Matrix(' + line.rstrip()[pos+28:-2].replace('[','(',4).replace(']',')',4) + ')')
149                             # for 2.57
150                             # vcam.matrix_world = eval('mathutils.Matrix([' + line.rstrip()[pos+28:-2] + '])')
151                             # for 2.63
152                             vcam.matrix_world = eval('(' + line.rstrip()[pos+27:-1] + ')')
153                             vcam.keyframe_insert('location')
154                             vcam.keyframe_insert('scale')
155                             vcam.keyframe_insert('rotation_euler')
156                             return
157
158             # process mesh commands
159
160             if ld_points:
161                 if (line[0] == 'v'):
162                     pos= line.find('NMesh.Vert')
163
164                     if (pos != -1):
165                         verts.append(eval(line[pos+10:]))
166
167             return
168
169         # Blender 2.5x mode
170         # process camera commands
171
172         if ld_cam:
173             pos= line.find('set_frame')
174
175             if (pos == -1):
176                 pos= line.find('frame_set')
177
178                 if (pos == -1):
179                     pos= lineSplit[0].find('frame_current')
180
181                     if (pos != -1):
182                         fr= stri(lineSplit[2])
183
184                         if (fr >= 0):
185                             scene.frame_current = fr
186                             voodoo_import.frwas= True
187
188                         return
189
190             if (pos != -1):
191                 fr= stri(line[pos+10:-2])
192
193                 if (fr >= 0):
194                     scene.frame_current = fr
195                     voodoo_import.frwas= True
196                     return
197
198             if voodoo_import.frwas:
199
200                 pos= line.find('data.lens')
201
202                 if (pos != -1):
203                     vcam.data.lens= float(lineSplit[2])
204                     vcam.data.keyframe_insert('lens')
205                     return
206
207                 pos= line.find('.Matrix')
208
209                 if (pos != -1):
210
211                     # for up to 2.55
212                     # vcam.matrix_world = eval('mathutils' + line[pos:])
213
214                     # for 2.56
215                     # if (line[pos+8] == '['):
216                     # # from Michael (Meikel) Oetjen
217                     #     vcam.matrix_world = eval('mathutils.Matrix((' + line.rstrip()[pos+9:-1].replace('[','(',3).replace(']',')',4) + ')')
218                     # else:
219                     #   vcam.matrix_world = eval('mathutils' + line[pos:])
220
221                     # for 2.57
222                     # vcam.matrix_world = eval('mathutils.Matrix([' + line.rstrip()[pos+8:-1] + '])')
223
224                     # for 2.63
225                     vcam.matrix_world = eval('(' + line.rstrip()[pos+7:] + ')')
226                     return
227
228                 pos= line.find('.matrix_world')
229
230                 if (pos != -1):
231                     vcam.matrix_world = eval(line.rstrip()[line.find('=')+1:])
232                     return
233
234                 pos= line.find('.location')
235
236                 if (pos != -1):
237                     vcam.location = eval(line[line.find('=')+1:])
238                     return
239
240                 pos= line.find('.rotation_euler')
241
242                 if (pos != -1):
243                     vcam.rotation_euler = eval(line[line.find('=')+1:])
244                     return
245
246                 pos= line.find('.data.keyframe_insert')
247
248                 if (pos != -1):
249                     vcam.data.keyframe_insert(eval(line[pos+22:-2]))
250                     return
251
252                 pos= line.find('.keyframe_insert')
253
254                 if (pos != -1):
255                     vcam.keyframe_insert(eval(line[pos+17:-2]))
256                     return
257
258         # process mesh commands
259
260         if ld_points:
261             pos= line.find('.append')
262
263             if (pos != -1):
264                 verts.append(eval(line[pos+8:-2]))
265
266     #read lines
267
268     for line in file.readlines():
269
270         if (b24 and (line.find('import') != -1) and (line.find('bpy') != -1)):
271             b24= False
272
273         process_line(line)
274
275     scene.frame_set(initfr)
276
277     if ld_points:
278         mesh.data.from_pydata(verts, [], [])
279
280     mesh.data.update()
281
282
283 # Operator
284 class ImportVoodooCamera(bpy.types.Operator):
285     ''''''
286     bl_idname = "import.voodoo_camera"
287     bl_label = "Import Voodoo camera"
288     bl_description = "Load a Blender export script from the Voodoo motion tracker"
289     bl_options = {'REGISTER', 'UNDO'}
290
291     filepath = StringProperty(name="File Path",
292         description="Filepath used for processing the script",
293         maxlen= 1024,default= "")
294
295     # filter_python = BoolProperty(name="Filter python",
296     # description="",default=True,options={'HIDDEN'})
297
298     load_camera = BoolProperty(name="Load camera",
299         description="Load the camera",
300         default=True)
301     load_points = BoolProperty(name="Load points",
302         description="Load the FP3D point cloud",
303         default=True)
304
305     def execute(self, context):
306         voodoo_import(self.filepath,self.load_camera,self.load_points)
307         return {'FINISHED'}
308
309     def invoke(self, context, event):
310         wm = context.window_manager
311         wm.fileselect_add(self)
312         return {'RUNNING_MODAL'}
313
314
315 # Registering / Unregister
316 def menu_func(self, context):
317     self.layout.operator(ImportVoodooCamera.bl_idname, text="Voodoo camera", icon='PLUGIN')
318
319
320 def register():
321     bpy.utils.register_module(__name__)
322     bpy.types.INFO_MT_file_import.append(menu_func)
323
324
325 def unregister():
326     bpy.utils.unregister_module(__name__)
327     bpy.types.INFO_MT_file_import.remove(menu_func)
328
329
330 if __name__ == "__main__":
331     register()