-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio_radiance_grid_export.py
201 lines (160 loc) · 6.15 KB
/
io_radiance_grid_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
bl_info = {
"name": "Export Radiance Calculation Grids",
"description": "Export Radiance Calculation Grids",
"author": "Francesco Anselmo",
"version": (0, 1),
"blender": (2, 57, 0),
"location": "File > Export > Export Radiance Calculation Grids (.pnt)",
"warning": "",
"wiki_url":"",
"category": "Import-Export",
}
import bpy
import os
import bmesh
from mathutils import Vector
from os.path import splitext
from math import degrees
def writeVtkPolydata(filename, verts, faces):
#print verts[1]
#print faces[1]
vtkfile = open(filename, "w")
#VTK legacy file header
vtkfile.write("# vtk DataFile Version 2.0\n")
vtkfile.write(filename+"\n")
vtkfile.write("ASCII\n")
vtkfile.write("DATASET POLYDATA\n")
vtkfile.write("POINTS "+ str(len(verts)) + " float\n")
#export points
print("exporting points")
for vert in verts:
#print "exporting " + str(vert)
line = ""
for coord in vert.co:
line += str(coord) + " "
#vtkfile.write(str(vert.co[0]) + " " + str(vert.co[1]) + " " + str(vert.co[2]) + "\n")
vtkfile.write(line + "\n")
#status message
print("points exported")
#export polygons
print("exporting polygons")
total = 0
fips = [(face, index) for index, face in enumerate(faces)] # face index polygons
for fip in fips:
#if len(face.v) > 2:
total += (len(fip[0].vertices)+1)
vtkfile.write("POLYGONS "+ str(len(fips)) + " " + str(total) + "\n")
for fip in fips:
#print "exporting " + str(face)
num = len(fip[0].vertices)
line = str(num) + " "
#if len(face.v) > 2:
for vertex in fip[0].vertices:
line += str(vertex) + " "
vtkfile.write(line + "\n")
vtkfile.close()
def write_grids(context, filepath, frame_start, frame_end, only_selected=False):
data_attrs = (
'lens',
'shift_x',
'shift_y',
'dof_distance',
'clip_start',
'clip_end',
'draw_size',
)
obj_attrs = (
'hide_render',
)
fn = splitext(filepath)[0]
scene = bpy.context.scene
grids = []
for obj in scene.objects:
if only_selected and not obj.select:
continue
if obj.type != 'MESH':
continue
grids.append((obj, obj.data))
frame_range = range(frame_start, frame_end + 1)
for obj, obj_data in grids:
gfn = '%s_%s' % (fn,obj.name)
grid = obj
name = obj.name
if name[:len("Grid")] == "Grid" or name[:len("Grid")] == "grid":
fn = gfn # file name
fnpnt = fn+".pnt"
fnvtk = fn+".vtk"
f = open(fnpnt,'w') # open file
if obj.type=='MESH':
od = obj.data # get object data if it is a mesh
# export vertices and normals
me = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
#me = od
me.transform(obj.matrix_world) # use world coordinates rather than local coordinate
for v in me.vertices:
x,y,z = v.co
nx, ny, nz = v.normal
print(x,y,z,nx,ny,nz)
f.write('%s %s %s %s %s %s\n' % (x,y,z,nx,ny,nz))
f.close()
#bpy.ops.export_scene.obj(filepath=fn + ".obj", use_selection=True, axis_forward='Y', axis_up='Z')
## Can be used for multiple formats
# bpy.ops.export_scene.x3d(filepath=fn + ".x3d", use_selection=True)
#obj.select = False
print("grid file written:", fn)
writeVtkPolydata(fnvtk,me.vertices,me.polygons)
from bpy.props import StringProperty, IntProperty, BoolProperty
from bpy_extras.io_utils import ExportHelper
class GridExporter(bpy.types.Operator, ExportHelper):
"""Export Radiance Calculation Grids"""
bl_idname = "export.radiance_grids"
bl_label = "Export Radiance Calculation Grids"
filename_ext = ".pnt"
filter_glob = StringProperty(default="*.pnt", options={'HIDDEN'})
frame_start = IntProperty(name="Start Frame",
description="Start frame for export",
default=1, min=1, max=300000)
frame_end = IntProperty(name="End Frame",
description="End frame for export",
default=1, min=1, max=300000)
only_selected = BoolProperty(name="Only Selected",
default=True)
def execute(self, context):
write_grids(context, self.filepath, self.frame_start, self.frame_start, self.only_selected)
return {'FINISHED'}
def invoke(self, context, event):
self.frame_start = context.scene.frame_start
self.frame_end = context.scene.frame_end
wm = context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
def menu_export(self, context):
import os
default_path = os.path.splitext(bpy.data.filepath)[0] + ".py"
self.layout.operator(GridExporter.bl_idname, text="Radiance Calculation Grids (.pnt)").filepath = default_path
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_export.append(menu_export)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_export.remove(menu_export)
if __name__ == "__main__":
register()