summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2014-11-11 12:08:19 +0200
committerBogdan Devichev <b.devichev@samsung.com>2014-11-11 12:08:19 +0200
commit674c62c434c1ee7fb989cf18145216b69ba85d84 (patch)
tree7c8c8870934a474dd0f13175b172c0149fae105b
[Blender_plugins] Blender_edc 0.0.1.pre.alpha.
-rw-r--r--To_Blender_scripts/Blender_to_edc.py425
-rw-r--r--To_Blender_scripts/edc_geom.py56
-rw-r--r--To_Blender_scripts/green_NULL.pngbin0 -> 144 bytes
3 files changed, 481 insertions, 0 deletions
diff --git a/To_Blender_scripts/Blender_to_edc.py b/To_Blender_scripts/Blender_to_edc.py
new file mode 100644
index 0000000..5342e88
--- /dev/null
+++ b/To_Blender_scripts/Blender_to_edc.py
@@ -0,0 +1,425 @@
1bl_info = {
2 "name": "Blender_edc",
3 "category": "Import-Export",
4 "location": "File > Import/Export > EFL (.edc)"
5}
6
7import os
8import math
9import bpy
10import shutil
11import edc_geom
12from bpy_extras.io_utils import ExportHelper
13
14PLY_FOLDER = "ply_files"
15IMAGE_FOLDER = "image_files"
16PROJECT_FOLDER = "project"
17
18NULL_TEX = "/home/bdevichev/.config/blender/2.69/scripts/addons/green_NULL.png"#FIXME
19
20MESH_PARAMETERS = edc_geom.Mesh_Parameters()
21INIT_MESH_PARAMETERS = edc_geom.Mesh_Parameters()
22
23def do_matrix_3D(vec, mat):
24 res = [0.0, 0.0, 0.0]
25 for x in range(0, 3):
26 for y in range(0, 3):
27 res[x] += vec[y] * mat[x][y]
28 return res
29
30def rotation_axis_angle_matrix(axis, angle):
31 res = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
32 cos = math.cos(angle)
33 sin = math.sin(angle)
34 X = axis[0]
35 Y = axis[1]
36 Z = axis[2]
37 res[0][0] = cos + (X * X) * (1 - cos)
38 res[0][1] = X * Y * (1 - cos) - Z * sin
39 res[0][2] = X * Z * (1 - cos) + Y * sin
40 res[1][0] = Y * X * (1 - cos) + Z * sin
41 res[1][1] = cos + (Y * Y) * (1 - cos)
42 res[1][2] = Y * Z * (1 - cos) - X * sin
43 res[2][0] = Z * X * (1 - cos) - Y * sin
44 res[2][1] = Z * Y * (1 - cos) + X * sin
45 res[2][2] = cos + Z * Z * (1 - cos)
46 return res
47
48
49def vec_sum_3D(vec1, vec2):
50 return [vec1[0] + vec2[0], vec1[1] + vec2[1], vec1[2] + vec2[2]]
51
52def get_field(field_name, keyword):
53 if field_name != "" :
54 return edc_block(keyword, field_name, 2) + "\n"
55 else :
56 return ""
57
58def edc_block(word, data, num):
59 return (" " * num) + word + " {\n" + data + "\n" + (" " * num) + "}"
60
61def edc_line(word, data, num):
62 res = (num * 3 * " ") + word + ": "
63 for x in data:
64 if type(x) is float:
65 res += edc_geom.coord(x) + " "
66 else:
67 res += str(x) + " "
68 return res[0: -1] + ";"
69
70def group_header(name, size):
71 return edc_line("name", ["\"" + name + "\""], 2) + "\n" + \
72 edc_line("size", [size[1], size[0]], 2) + "\n" + \
73 edc_line("min", size, 2) + "\n" #Max waiting
74
75
76def material_write(material):
77 temp = edc_line("ambient",
78 [material.diffuse_color[0],
79 material.diffuse_color[1],
80 material.diffuse_color[2],
81 (material.ambient) / 2],
82 6) + "\n" + \
83 edc_line("diffuse",
84 [material.diffuse_color[0],
85 material.diffuse_color[1],
86 material.diffuse_color[2],
87 material.diffuse_intensity],
88 6) + "\n" + \
89 edc_line("specular",
90 [material.specular_color[0],
91 material.specular_color[1],
92 material.specular_color[2],
93 material.specular_intensity],
94 6) + "\n"
95 return temp
96
97def look(point_name, location, angle_axis):
98 angle = angle_axis[0]
99 axis = angle_axis[1:4]
100 R = rotation_axis_angle_matrix(axis, angle)
101 if point_name is "look1":
102 look = vec_sum_3D(location, do_matrix_3D([0, 0, -1], R))
103 elif point_name is "look2":
104 look = do_matrix_3D([0, 1, -1], R)
105 return look
106
107def write_all_files(context, my_filepath, edc_file):
108
109 #project pathes creating
110 file_name = my_filepath.split("/")[-1]
111 project_folder = my_filepath[0:-len(file_name)] + \
112 PROJECT_FOLDER + "_" + file_name[0: -4] + "/"
113 ply_directory = project_folder + PLY_FOLDER + "/"
114 image_directory = project_folder + IMAGE_FOLDER + "/"
115
116 if not os.path.exists(ply_directory):
117 os.makedirs(ply_directory)
118
119 for mesh in bpy.data.objects:
120 if mesh.type != 'MESH':
121 continue
122
123 bpy.context.scene.objects.active = mesh
124 MESH_PARAMETERS.get_mesh(mesh)
125 INIT_MESH_PARAMETERS.set_mesh(mesh)
126 bpy.ops.export_mesh.ply(filepath = ply_directory + \
127 mesh.name.replace(".", "_") + \
128 ".ply")
129 MESH_PARAMETERS.set_mesh(mesh)
130
131 if not os.path.exists(image_directory):
132 os.makedirs(image_directory)
133
134 for image in bpy.data.images:
135 if image.type != 'IMAGE':
136 continue
137 if image.name == 'Render Result':
138 continue
139
140 shutil.copy2(bpy.path.abspath(image.filepath),
141 image_directory + image.name.split(".")[0])
142 shutil.copy2(NULL_TEX,
143 image_directory + "/" + "green_NULL.png") #FIXME make a good path
144
145 print("running write_some_data...")
146 f = open(project_folder + file_name, 'w')
147 f.write("%s" % edc_file)
148 f.close()
149
150 return {'FINISHED'}
151
152
153class Blender_edc(bpy.types.Operator, ExportHelper):
154 """Blender/EET converter""" # blender will use this as a tooltip for menu items and buttons.
155 bl_idname = "import.blender_edc" # unique identifier for buttons and menu items to reference.
156 bl_label = "Blender_edc" # display name in the interface.
157 bl_options = {'REGISTER', 'UNDO'} # enable undo for the operator.
158# ExportHelper mixin class uses this
159 filename_ext = ".edc"
160
161 filter_glob = bpy.props.StringProperty(default = "*.edc", options = {'HIDDEN'})
162
163# This is how can be maked options for saving
164 # List of operator properties, the attributes will be assigned
165 # to the class instance from the operator settings before calling.
166 use_setting = bpy.props.BoolProperty(name = "Example Boolean",
167 description = "Example Tooltip",
168 default=True)
169
170 type = bpy.props.EnumProperty(items = (('OPT_A',
171 "First Option",
172 "Description one"),
173 ('OPT_B',
174 "Second Option",
175 "Description two."),),
176 name = "Example Enum",
177 description = "Choose between two items",
178 default = 'OPT_A')
179
180
181 def execute(self, context): # execute() is called by blender when running the operator.
182
183 obs = bpy.data.objects
184 LOG = "\n/*\n"
185 enum_obj = ""
186 enum_img = ""
187 all_parts = ""
188 all_programs = ""
189 NAME = "3d_" + bpy.context.scene.name + "_from_blender"
190
191 SIZE = [bpy.data.scenes["Scene"].render.resolution_x,
192 bpy.data.scenes["Scene"].render.resolution_y]
193 bpy.ops.object.select_all(action = 'DESELECT')
194
195 #Saving of meshes
196 for mesh in bpy.data.objects:
197 if mesh.type != 'MESH':
198 continue
199 bpy.context.scene.objects.active = mesh
200
201 model_path = "\"" + PLY_FOLDER + "/" + mesh.name.replace(".", "_") + ".ply\""
202 if (mesh.active_material != None) and \
203 (mesh.active_material.active_texture != None) and \
204 (mesh.active_material.active_texture.image != None):
205 image_path = "\"" + IMAGE_FOLDER + "/" + mesh.active_material.active_texture.image.name.split(".")[0] + "\""
206 else :
207 image_path = "\"" + IMAGE_FOLDER + "/" + "green_NULL.png\""
208
209 enum_obj += edc_line("model", [model_path], 3) + "\n"
210
211 edc_part_mesh = edc_line("assembly", ["TRIANGLES"], 6) + "\n" + \
212 edc_line("geometry", [model_path], 6)
213
214 edc_part_texture = edc_line("proxy", [image_path], 6) + "\n" + \
215 edc_line("shade", ["PHONG"], 6) + "\n" + \
216 edc_line("wrap1", ["REPEAT"], 6) + "\n" + \
217 edc_line("wrap2", ["REPEAT"], 6) + "\n" + \
218 edc_line("filter1", ["NEAREST"], 6) + "\n" + \
219 edc_line("filter2", ["NEAREST"], 6)
220
221 edc_part_properties = ""
222 if mesh.active_material != None:
223 edc_part_properties = material_write(mesh.active_material)
224 edc_part_properties += edc_line("normal", [1], 6) + "\n" + \
225 edc_line("shininess", [50.0], 6) + "\n" + \
226 edc_line("material", ["DIFFUSE"], 6)
227
228 edc_part_orientation = edc_line("angle", mesh.rotation_axis_angle[0:1], 6) + "\n" + \
229 edc_line("axis", mesh.rotation_axis_angle[1:4], 6)
230
231 edc_part_position = edc_line("point", mesh.location, 6)
232
233 part_description = edc_line("state", ["\"default\"", 0.0], 5) + "\n" + \
234 edc_line("visible", [1], 5) + "\n" + \
235 edc_block("mesh", edc_part_mesh, 5) + "\n" + \
236 edc_block("texture", edc_part_texture, 5) + "\n" + \
237 edc_block("properties", edc_part_properties, 5) + "\n" + \
238 edc_block("orientation", edc_part_orientation, 5) + "\n" + \
239 edc_block("position", edc_part_position, 5)
240
241 part_data = edc_line("name", ["\"" + mesh.name + "\""], 4) + "\n" + \
242 edc_line("type", ["MESH_NODE"], 4) + "\n" + \
243 edc_line("scale", mesh.scale, 5) + "\n" + \
244 edc_block("description", part_description, 4)
245
246 all_parts += edc_block("part", part_data, 3) + "\n"
247
248 #Saving of images
249 for image in bpy.data.images:
250 if image.type != 'IMAGE':
251 continue
252 if image.name == 'Render Result':
253 continue
254 enum_img += edc_line("image", ["\"" + IMAGE_FOLDER + "/" + image.name.split(".")[0] + "\" COMP"], 3) + "\n"
255 enum_img += edc_line("image", ["\"" + IMAGE_FOLDER + "/" + "green_NULL.png" + "\" COMP"], 3) + "\n"
256
257 #Saving of lights
258 for light in bpy.data.objects:
259 if light.type != 'LAMP':
260 continue
261
262 ROTATION_MODE = bpy.context.object.rotation_mode
263 bpy.context.object.rotation_mode = 'AXIS_ANGLE'
264 look1 = look("look1", light.location, light.rotation_axis_angle)
265 look2 = look("look2", light.location, light.rotation_axis_angle)
266 edc_light_position = edc_line("point", light.location, 6) + "\n" + \
267 edc_line("space", ["PARENT"], 6) + "\n" + \
268 edc_line("look1", look1, 6) + "\n" + \
269 edc_line("look2", look2, 6)
270 bpy.context.object.rotation_mode = ROTATION_MODE
271
272
273 edc_light_properties = edc_line("ambient",
274 [light.data.color[0],
275 light.data.color[1],
276 light.data.color[2],
277 light.data.energy],
278 6) + "\n" + \
279 edc_line("diffuse",
280 [light.data.color[0],
281 light.data.color[1],
282 light.data.color[2],
283 light.data.energy],
284 6) + "\n" + \
285 edc_line("specular",
286 [light.data.color[0],
287 light.data.color[1],
288 light.data.color[2],
289 light.data.energy],
290 6)
291
292 light_description = edc_line("state", ["\"default\"", 0.0], 5) + "\n" + \
293 edc_block("position", edc_light_position, 5) + "\n" + \
294 edc_block("properties", edc_light_properties, 5)
295
296 light_data = edc_line("name", ["\"" + light.name + "\""], 4) + "\n" + \
297 edc_line("type", ["LIGHT"], 4) + "\n" + \
298 edc_block("description", light_description, 4)
299
300 light = edc_block("part", light_data, 3)
301
302 all_parts += light + "\n"
303
304 #Saving of cameras
305 camera_data = edc_line("name", ["\"camera\""], 4) + "\n" + \
306 edc_line("type", ["CAMERA"], 4) + "\n"
307 i = 0
308 main_script = ""
309 script = ""
310 main_camera_script = edc_line("signal", ["\"mouse,down,1\""], 4) + "\n" + \
311 edc_line("source", ["\"camera\""], 4) + "\n"
312 for camera in bpy.data.objects:
313
314 if camera.type != 'CAMERA':
315 continue
316
317 if i is 0 :
318 cam_state = "\"default\""
319 else_in_script = ""
320 else :
321 cam_state = "\"additional_" + str(i) + "\""
322 else_in_script = "else "
323
324 camera_description = edc_line("state", [cam_state, 0.0], 5) + "\n"
325
326 program = edc_line("name", ["\"state_" + str(i) + "\""] , 4) + "\n" + \
327 edc_line("action", ["STATE_SET", cam_state, 0.0], 4) + "\n" + \
328 edc_line("target", ["\"camera\""], 4)
329 main_script += script.replace("not_checked", "")
330 script = " " + \
331 else_in_script + \
332 "if (get_int(camera) == not_checked" + \
333 str(i) + ") {\n"
334 script += " run_program(PROGRAM:\"state_not_checked" + \
335 str(i + 1) + "\");\n"
336 script += " set_int(camera, not_checked" + str(i + 1) + ");\n"
337 script += " }\n"
338 all_programs += edc_block("program", program, 3) + "\n"
339
340 edc_camera_position = edc_line("point", camera.location, 6) + "\n" + \
341 edc_line("space", ["PARENT"], 6) + "\n"
342
343 ROTATION_MODE = bpy.context.object.rotation_mode
344 bpy.context.object.rotation_mode = 'AXIS_ANGLE'
345 look1 = look("look1", camera.location, camera.rotation_axis_angle)
346 look2 = look("look2", camera.location, camera.rotation_axis_angle)
347 edc_camera_position += edc_line("look1", look1, 6) + "\n" + \
348 edc_line("look2", look2, 6)
349 bpy.context.object.rotation_mode = ROTATION_MODE
350
351 camera_description += edc_block("position", edc_camera_position, 5) + "\n"
352
353 LENS_UNIT = camera.data.lens_unit
354 camera.data.lens_unit = 'FOV'
355 fov_angle = camera.data.angle * 180 / math.pi
356 camera.data.lens_unit = LENS_UNIT
357
358 aspect_x = bpy.data.scenes["Scene"].render.pixel_aspect_x
359 aspect_y = bpy.data.scenes["Scene"].render.pixel_aspect_y
360
361 edc_camera_properties = edc_line("properties",
362 [fov_angle,
363 aspect_x / aspect_y,
364 camera.data.clip_start,
365 camera.data.clip_end],
366 6)
367
368 camera_description += edc_block("camera", edc_camera_properties, 5)
369 camera_data += edc_block("description", camera_description, 4) + "\n"
370 i += 1
371 script = script.replace("not_checked" + str(i), "0")
372 main_script += script.replace("not_checked", "")[0:-1]
373 camera_count = i
374 main_camera_script += edc_block("script", main_script, 4)
375 all_programs += edc_block("program", main_camera_script, 3) + "\n"
376 if camera_count > 0:
377 camera = edc_block("part", camera_data[0:-1], 3)
378 all_parts += camera + "\n"
379
380 #creating of edc
381 MODELS = enum_obj[0:-1]
382 IMAGES = enum_img
383 PARTS = all_parts[0:-1]
384
385 if camera_count > 1:
386 main_blocks = edc_block("script", " public camera;", 2) + "\n"
387 else:
388 main_blocks = ""
389 main_blocks += group_header(NAME, SIZE) + \
390 get_field(enum_img, "images") + \
391 get_field(MODELS, "models") + \
392 get_field(PARTS, "parts")[0:-1]
393 if camera_count > 1:
394 main_blocks +="\n" + edc_block("programs", all_programs[0:-1], 2)
395
396 edc_skeleton = edc_block("collections", \
397 edc_block("group", main_blocks, 1), 0)
398
399 return write_all_files(context, self.filepath, edc_skeleton)
400
401
402 @classmethod
403 def poll(cls, context):
404 return context.active_object != None
405
406def menu_func_import(self, context):
407 self.layout.operator(Blender_edc.bl_idname, text="EFL (.edc) (now is export too)")
408
409def menu_func_export(self, context):
410 self.layout.operator(Blender_edc.bl_idname, text="EFL (.edc)")
411
412def register():
413 bpy.utils.register_class(Blender_edc)
414 bpy.types.INFO_MT_file_import.append(menu_func_import)
415 bpy.types.INFO_MT_file_export.append(menu_func_export)
416
417def unregister():
418 bpy.utils.unregister_class(Blender_edc)
419 bpy.types.INFO_MT_file_import.remove(menu_func_import)
420 bpy.types.INFO_MT_file_export.remove(menu_func_export)
421
422# This allows you to run the script directly from blenders text editor
423# to test the addon without having to install it.
424if __name__ == "__main__":
425 register()
diff --git a/To_Blender_scripts/edc_geom.py b/To_Blender_scripts/edc_geom.py
new file mode 100644
index 0000000..e2bde0c
--- /dev/null
+++ b/To_Blender_scripts/edc_geom.py
@@ -0,0 +1,56 @@
1accuracy = 6;
2
3def do_matrix_3D(vec, mat):
4 res = [0.0, 0.0, 0.0]
5 for x in range(0, 3):
6 for y in range(0, 3):
7 res[x] += vec[y] * mat[x][y]
8 return res
9
10def coord(name):
11 if str(name)[0] is '-':
12 temp = accuracy + 1
13 else:
14 temp = accuracy
15 return str(name)[0:temp]
16
17def vec_copy(out_vec, in_vec, dimentionals):
18 for x in range(0, dimentionals):
19 out_vec[x] = in_vec[x]
20
21class Mesh_Parameters():
22 def __init__(self):
23 self.position = [0.0, 0.0, 0.0]
24 self.orientation = [1.0, 0.0, 0.0, 0.0]
25 self.scale = [1.0, 1.0, 1.0]
26 def get_mesh(self, mesh):
27 vec_copy(self.position, mesh.location, 3)
28 vec_copy(self.orientation, mesh.rotation_axis_angle, 4)
29 vec_copy(self.scale, mesh.scale, 3)
30 def set_mesh(self, mesh):
31 vec_copy(mesh.location, self.position, 3)
32 vec_copy(mesh.rotation_axis_angle, self.orientation, 4)
33 vec_copy(mesh.scale, self.scale, 3)
34
35def look_vec1(location, angle_axis):
36 angle = angle_axis[0]
37 axis = angle_axis[1:4]
38 R = rotation_axis_angle_matrix(axis, angle)
39 vec2 = geom.do_matrix_3D([0, 0, -1], R)
40 look1 = vec_sum_3D(location, vec2)
41 res = " look1: "
42 res += geom.coord(look1[0]) + " "
43 res += geom.coord(look1[1]) + " "
44 res += geom.coord(look1[2]) + ";\n"
45 return res
46
47def look_vec2(location, angle_axis):
48 angle = angle_axis[0]
49 axis = angle_axis[1:4]
50 R = rotation_axis_angle_matrix(axis, angle)
51 look2 = geom.do_matrix_3D([0, 1, -1], R)
52 res = " look2: "
53 res += geom.coord(look2[0]) + " "
54 res += geom.coord(look2[1]) + " "
55 res += geom.coord(look2[2]) + ";"
56 return res
diff --git a/To_Blender_scripts/green_NULL.png b/To_Blender_scripts/green_NULL.png
new file mode 100644
index 0000000..061cab7
--- /dev/null
+++ b/To_Blender_scripts/green_NULL.png
Binary files differ