minor changes to be compatible with 2.57
[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, 7),
23     "blender": (2, 5, 7),
24     "api": 36339,
25     "location": "File > Import > Voodoo camera",
26     "description": "Imports a Blender (2.4x or 2.5x) Python script from the Voodoo camera tracker software.",
27     "warning": "",
28     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
29         "Scripts/Import-Export/Voodoo_camera",
30     "tracker_url": "https://projects.blender.org/tracker/index.php?"\
31         "func=detail&aid=22510",
32     "category": "Import-Export"}
33
34 """
35 This script loads a Blender Python script from the Voodoo camera
36 tracker program into Blender 2.5x.
37
38 It processes the script as a text file and not as a Python executable
39 because of the incompatible Python APIs of Blender 2.4x and 2.5x.
40 """
41
42 import bpy
43 from bpy.props import *
44 import mathutils
45 import os
46 import string
47 import math
48
49 def voodoo_import(infile,ld_cam,ld_points):
50
51     checktype = os.path.splitext(infile)[1]
52
53     if checktype.upper() != '.PY':
54         print ("Selected file: ",infile)
55         raise IOError("The selected input file is not a *.py file")
56         return
57
58     print ("--------------------------------------------------")
59     print ("Importing Voodoo file: ", infile)
60
61     file = open(infile,'rU')
62     scene = bpy.context.scene
63     initfr = scene.frame_current
64     b24= True
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         vcam = bpy.data.objects.new('voodoo_render_cam', data)
75         vcam.location = (0.0, 0.0, 0.0)
76         vcam.rotation_euler = (0.0, 0.0, 0.0)
77         vcam.scale = (1.0, 1.0, 1.0)
78         data.lens = 35.0
79         data.shift_x = 0.0
80         data.shift_y = 0.0
81         data.dof_distance = 0.0
82         data.clip_start = 0.1
83         data.clip_end = 1000.0
84         data.draw_size = 0.5
85         scene.objects.link(vcam)
86         vcam.parent = dummy
87
88     if ld_points:
89         data = bpy.data.meshes.new('voodoo_FP3D_cloud')
90         mesh = bpy.data.objects.new('voodoo_FP3D_cloud', data)
91         mesh.location = (0.0, 0.0, 0.0)
92         mesh.rotation_euler = (3.141593/2, 0.0, 0.0)
93         mesh.scale = (5.0, 5.0, 5.0)
94         scene.objects.link(mesh)
95         mesh.parent = dummy
96
97     verts = []
98
99     def process_line(line):
100         lineSplit = line.split()
101
102         if (line[0] == '#'): return
103
104         if b24:
105
106             # Blender 2.4x mode
107             # process camera commands
108
109             if ld_cam:
110                 if (line[0] == 'c' and line[1] != 'r'):
111                     pos= line.find('.lens')
112
113                     if (pos != -1):
114                         fr = int(lineSplit[0][1:pos],10)
115                         scene.frame_set(fr)
116                         vcam.data.lens= float(lineSplit[2])
117                         vcam.data.keyframe_insert('lens')
118                         return
119
120                 if (line[0] == 'o'):
121                     pos= line.find('.setMatrix')
122
123                     if (pos != -1):
124                         fr = int(lineSplit[0][1:pos],10)
125                         scene.frame_set(fr)
126                         # for up to 2.55
127                         # vcam.matrix_world = eval('mathutils.' + line.rstrip()[pos+21:-1])
128                         # for 2.56, from Michael (Meikel) Oetjen 
129                         # vcam.matrix_world = eval('mathutils.Matrix(' + line.rstrip()[pos+28:-2].replace('[','(',4).replace(']',')',4) + ')')
130                         # for 2.57
131                         vcam.matrix_world = eval('mathutils.Matrix([' + line.rstrip()[pos+28:-2] + '])')
132                         vcam.keyframe_insert('location')
133                         vcam.keyframe_insert('scale')
134                         vcam.keyframe_insert('rotation_euler')
135                         return
136
137             # process mesh commands
138
139             if ld_points:
140                 if (line[0] == 'v'):
141                     pos= line.find('NMesh.Vert')
142
143                     if (pos != -1):
144                         verts.append(eval(line[pos+10:]))
145
146             return
147
148         # Blender 2.5x mode
149         # process camera commands
150
151         if ld_cam:
152             pos= line.find('set_frame')
153
154             if (pos == -1):
155                 pos= line.find('frame_set')
156
157             if (pos != -1):
158                 scene.frame_set(eval(line[pos+9:]))
159                 return
160
161             pos= line.find('vcam.data.lens')
162
163             if (pos != -1):
164                 vcam.data.lens= float(lineSplit[2])
165                 vcam.data.keyframe_insert('lens')
166                 return
167
168             pos= line.find('.Matrix')
169
170             if (pos != -1):
171
172                 # for up to 2.55
173                 # vcam.matrix_world = eval('mathutils' + line[pos:])
174
175                 # for 2.56
176                 # if (line[pos+8] == '['):
177                 #   # from Michael (Meikel) Oetjen
178                 #     vcam.matrix_world = eval('mathutils.Matrix((' + line.rstrip()[pos+9:-1].replace('[','(',3).replace(']',')',4) + ')')
179                 # else:
180                 #   vcam.matrix_world = eval('mathutils' + line[pos:])
181
182                 # for 2.57
183                 vcam.matrix_world = eval('mathutils.Matrix([' + line.rstrip()[pos+8:-1] + '])')
184                 vcam.keyframe_insert('location')
185                 vcam.keyframe_insert('scale')
186                 vcam.keyframe_insert('rotation_euler')
187                 return
188
189         # process mesh commands
190
191         if ld_points:
192             pos= line.find('.append')
193
194             if (pos != -1):
195                 verts.append(eval(line[pos+8:-2]))
196
197     #read lines
198
199     for line in file.readlines():
200
201         if (b24 and (line.find('import') != -1) and (line.find('bpy') != -1)):
202                 b24= False
203
204         process_line(line)
205
206     scene.frame_set(initfr)
207
208     if ld_points:
209         mesh.data.from_pydata(verts, [], [])
210
211     mesh.data.update()
212
213
214 # Operator
215 class ImportVoodooCamera(bpy.types.Operator):
216     ''''''
217     bl_idname = "import.voodoo_camera"
218     bl_label = "Import Voodoo camera"
219     bl_description = "Load a Blender export script from the Voodoo motion tracker"
220     bl_options = {'REGISTER', 'UNDO'}
221
222     filepath = StringProperty(name="File Path",
223         description="Filepath used for processing the script",
224         maxlen= 1024,default= "")
225
226 #    filter_python = BoolProperty(name="Filter python",
227 #    description="",default=True,options={'HIDDEN'})
228
229     load_camera = BoolProperty(name="Load camera",
230         description="Load the camera",
231         default=True)
232     load_points = BoolProperty(name="Load points",
233         description="Load the FP3D point cloud",
234         default=True)
235
236     def execute(self, context):
237         voodoo_import(self.filepath,self.load_camera,self.load_points)
238         return {'FINISHED'}
239
240     def invoke(self, context, event):
241         wm = context.window_manager
242         wm.fileselect_add(self)
243         return {'RUNNING_MODAL'}
244
245
246 # Registering / Unregister
247 def menu_func(self, context):
248     self.layout.operator(ImportVoodooCamera.bl_idname, text="Voodoo camera", icon='PLUGIN')
249
250
251 def register():
252     bpy.utils.register_module(__name__)
253     bpy.types.INFO_MT_file_import.append(menu_func)
254
255
256 def unregister():
257     bpy.utils.unregister_module(__name__)
258     bpy.types.INFO_MT_file_import.remove(menu_func)
259
260
261 if __name__ == "__main__":
262     register()