d83d33e0bbc9b3e45ee863633f3e2190ded4221f
[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_addon_info = {
20     "name": "Import: Voodoo camera",
21     "author": "Fazekas Laszlo",
22     "version": (0, 4),
23     "blender": (2, 5, 3),
24     "api": 32516,
25     "location": "File > Import ",
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/",
30     "tracker_url": "https://projects.blender.org/tracker/index.php?"\
31         "func=detail&aid=22510&group_id=153&atid=468",
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 = (0.0, 0.0, 0.0)
93         mesh.scale = (1.0, 1.0, 1.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                         vcam.matrix_world = eval('mathutils.' + line.rstrip()[pos+21:-1])
127                         vcam.keyframe_insert('location')
128                         vcam.keyframe_insert('scale')
129                         vcam.keyframe_insert('rotation_euler')
130                         return
131
132             # process mesh commands
133
134             if ld_points:
135                 if (line[0] == 'v'):
136                     pos= line.find('NMesh.Vert')
137
138                     if (pos != -1):
139                         verts.append(eval(line[pos+10:]))
140
141             return
142
143         # Blender 2.5x mode
144         # process camera commands
145
146         if ld_cam:
147             pos= line.find('set_frame')
148
149             if (pos == -1):
150                 pos= line.find('frame_set')
151
152             if (pos != -1):
153                 scene.frame_set(eval(line[pos+9:]))
154                 return
155
156             pos= line.find('vcam.data.lens')
157
158             if (pos != -1):
159                 vcam.data.lens= float(lineSplit[2])
160                 vcam.data.keyframe_insert('lens')
161                 return
162
163             pos= line.find('.Matrix')
164
165             if (pos != -1):
166                 vcam.matrix_world = eval('mathutils' + line[pos:])
167                 vcam.keyframe_insert('location')
168                 vcam.keyframe_insert('scale')
169                 vcam.keyframe_insert('rotation_euler')
170                 return
171
172         # process mesh commands
173
174         if ld_points:
175             pos= line.find('.append')
176
177             if (pos != -1):
178                 verts.append(eval(line[pos+8:-2]))
179
180     #read lines
181
182     for line in file.readlines():
183
184         if (b24 and (line.find('import') != -1) and (line.find('bpy') != -1)):
185                 b24= False
186
187         process_line(line)
188
189     scene.frame_set(initfr)
190
191     if ld_points:
192         mesh.data.from_pydata(verts, [], [])
193
194     mesh.data.update()
195
196
197 # Operator
198 class ImportVoodooCamera(bpy.types.Operator):
199     ''''''
200     bl_idname = "import.voodoo_camera"
201     bl_label = "Import Voodoo camera"
202     bl_description = "Load a Blender export" \
203         " script from the Voodoo motion tracker."
204     bl_options = {'REGISTER', 'UNDO'}
205
206     filepath = StringProperty(name="File Path",
207         description="Filepath used for processing the script",
208         maxlen= 1024,default= "")
209     filter_python = BoolProperty(name="Filter python",
210         description="",default=True,options={'HIDDEN'})
211
212     load_camera = BoolProperty(name="Load camera",
213         description="Load the camera",
214         default=True)
215     load_points = BoolProperty(name="Load points",
216         description="Load the FP3D point cloud",
217         default=True)
218
219     def execute(self, context):
220         voodoo_import(self.properties.filepath,self.load_camera,self.load_points)
221         return {'FINISHED'}
222
223     def invoke(self, context, event):
224         wm = bpy.context.window_manager
225         wm.fileselect_add(self)
226         return {'RUNNING_MODAL'}
227
228
229 # Registering / Unregister
230 def menu_func(self, context):
231     self.layout.operator(ImportVoodooCamera.bl_idname, text="Voodoo camera", icon='PLUGIN')
232
233
234 def register():
235     bpy.types.INFO_MT_file_import.append(menu_func)
236
237
238 def unregister():
239     bpy.types.INFO_MT_file_import.remove(menu_func)
240
241
242 if __name__ == "__main__":
243     register()