FBX: reformat props.
[blender-addons.git] / io_export_dxf / export_dxf.py
blobfb53e4dd2e343d5b394964210e6585e46211014c
1 import os
2 import mathutils
4 DEBUG = os.environ.get('BLENDER_DEBUG', False) #activates debug mode
5 if DEBUG:
6 import sys
7 sys.path.append(os.environ['PYDEV_DEBUG_PATH'])
8 import pydevd
10 from .model.migiusModel import MigiusDXFLibDrawing
12 SUPPORTED_TYPES = ('MESH')#,'CURVE','EMPTY','TEXT','CAMERA','LAMP')
14 def exportDXF(context, filePath, settings):
15 """
16 Main entry point into export facility.
17 """
18 print("----------\nExporting to {}".format(filePath))
19 import time
20 time1 = time.clock()
22 if settings['verbose']:
23 print("Generating Object list for export... (Root parents only)")
25 scene = context.scene
27 if settings['onlySelected'] is True:
28 objects = (ob for ob in scene.objects if ob.is_visible(scene) and ob.select and ob.type in SUPPORTED_TYPES)
29 else:
30 objects = (ob for ob in scene.objects if ob.is_visible(scene) and ob.type in SUPPORTED_TYPES)
32 if DEBUG: pydevd.settrace()
33 mw = get_view_projection_matrix(context, settings)
35 try:
36 # add Entities --------------------
37 #todo: fixme: seems to be the reason for missing BLOCK-export
38 #if APPLY_MODIFIERS: tmp_me = Mesh.New('tmp')
39 #else: tmp_me = None
41 drawing = MigiusDXFLibDrawing()
42 exported = 0
43 for o in objects:
44 if _exportItem(context, o, mw, drawing, settings):
45 exported +=1
47 if not drawing.isEmpty():
48 # NOTE: Only orthographic projection used now.
49 # if PERSPECTIVE: # generate view border - passepartout
50 # from .primitive_exporters.viewborder_exporter import ViewBorderDXFExporter
51 # e = ViewBorderDXFExporter(settings)
52 # e.export(drawing, ob, mx, mw)
54 drawing.convert(filePath)
56 duration = time.clock() - time1
57 print('%s objects exported in %.2f seconds. -----DONE-----' %\
58 (exported, duration))
59 except IOError:
60 print('DXF Exporter: Write Error: ', filePath)
61 except Exception as e:
62 print('Nothing exported. Error: %s' % str(e))
64 print("Finished")
66 #-------------------------------------------------
67 def getCommons(ob, settings):
68 """set up common attributes for output style:
69 color=None
70 extrusion=None
71 layer='0',
72 lineType=None
73 lineTypeScale=None
74 lineWeight=None
75 thickness=None
76 parent=None
77 """
79 BYBLOCK=0 #DXF-attribute: assign property to BLOCK defaults
80 BYLAYER=None #256 #DXF-attribute: assign property to LAYER defaults
81 LAYERNAME_DEF='' #default layer name
82 LAYERCOLOR_DEF=7 #default layer color index
83 LAYERLTYPE_DEF=0 #'CONTINUOUS' - default layer lineType
84 ENTITYLAYER_DEF=LAYERNAME_DEF #default entity color index
85 ENTITYCOLOR_DEF=BYLAYER #default entity color index
86 ENTITYLTYPE_DEF=BYLAYER #default entity lineType
88 layers = ob.layers #gives a list e.g.[1,5,19]
89 if layers: ob_layer_nr = layers[0]
90 if DEBUG: print('ob_layer_nr=', ob_layer_nr) #--------------
92 materials = ob.material_slots
93 if materials:
94 ob_material = materials[0]
95 ob_mat_color = ob_material.material.diffuse_color
96 else: ob_mat_color, ob_material = None, None
97 if DEBUG:
98 print('ob_mat_color, ob_material=', ob_mat_color, ob_material) #--------------
100 data_materials = ob.material_slots
101 if data_materials:
102 data_material = data_materials[0]
103 data_mat_color = data_material.material.diffuse_color
104 else: data_mat_color, data_material = None, None
105 if DEBUG:
106 print('data_mat_color, data_material=', data_mat_color, data_material) #--------------
108 entitylayer = ENTITYLAYER_DEF
109 c = settings['entitylayer_from']
110 #["default_LAYER","obj.name","obj.layer","obj.material","obj.data.name","obj.data.material","..vertexgroup","..group","..map_table"]
111 if c=="default_LAYER":
112 entitylayer = LAYERNAME_DEF
113 elif c=="obj.layer" and ob_layer_nr:
114 entitylayer = 'LAYER'+ str(ob_layer_nr)
115 elif c=="obj.material" and ob_material:
116 entitylayer = ob_material.name
117 elif c=="obj.name":
118 entitylayer = ob.name
119 elif c=="obj.data.material" and ob_material:
120 entitylayer = data_material.name
121 elif c=="obj.data.name":
122 entitylayer = ob.data.name
124 entitycolor = ENTITYCOLOR_DEF
125 cfrom = settings['entitycolor_from']
126 if cfrom=="default_COLOR":
127 entitycolor = LAYERCOLOR_DEF
128 elif cfrom=="BYLAYER":
129 entitycolor = BYLAYER
130 elif cfrom=="BYBLOCK":
131 entitycolor = BYBLOCK
132 elif cfrom=="obj.layer" and ob_layer_nr:
133 entitycolor = ob_layer_nr
134 elif cfrom=="obj.color" and ob.color:
135 entitycolor = ob.color
136 elif cfrom=="obj.material" and ob_mat_color:
137 entitycolor = ob_mat_color
138 elif cfrom=="obj.data.material" and data_mat_color:
139 entitycolor = data_mat_color
141 entityltype = ENTITYLTYPE_DEF
142 etype = settings['entityltype_from']
143 if etype=="default_LTYPE":
144 entityltype = LAYERLTYPE_DEF
145 elif etype=="BYLAYER":
146 entityltype = BYLAYER
147 elif etype=="BYBLOCK":
148 entityltype = BYBLOCK
149 elif etype:
150 entityltype = etype
152 return entitylayer, entitycolor, entityltype
154 def getCameraMatrix(cam):
155 raise NotImplementedError()
156 # camProps = cam.data
157 # mc0 = act_camera.matrix.copy()
158 # #print 'deb: camera.Matrix=\n', mc0 #------------------
159 # camera = Camera.Get(act_camera.getData(name_only=True))
160 # #print 'deb: camera=', dir(camera) #------------------
161 # if camera.type=='persp': PERSPECTIVE = 1
162 # elif camera.type=='ortho': PERSPECTIVE = 0
163 # # mcp is matrix.camera.perspective
164 # clip_box, mcp = getClipBox(camera)
165 ## if PERSPECTIVE:
166 ## # get border
167 ## # lens = camera.lens
168 ## min_X1, max_X1, min_Y1, max_Y1,\
169 ## min_X2, max_X2, min_Y2, max_Y2,\
170 ## min_Z, max_Z = clip_box
171 ## verts = []
172 ## verts.append([min_X1, min_Y1, min_Z])
173 ## verts.append([max_X1, min_Y1, min_Z])
174 ## verts.append([max_X1, max_Y1, min_Z])
175 ## verts.append([min_X1, max_Y1, min_Z])
176 ## border=verts
177 # mw = mc0.copy().invert()
178 # #ViewVector = mathutils.Vector(Window.GetViewVector())
179 # #print 'deb: ViewVector=\n', ViewVector #------------------
180 # #TODO: what is Window.GetViewOffset() for?
181 # #print 'deb: Window.GetViewOffset():', Window.GetViewOffset() #---------
182 # #Window.SetViewOffset([0,0,0])
183 # mw0 = Window.GetViewMatrix()
184 # #print 'deb: mwOrtho =\n', mw0 #---------
185 # mwp = Window.GetPerspMatrix() #TODO: how to get it working?
186 # #print 'deb: mwPersp =\n', mwp #---------
187 # mw = mw0.copy()
189 projectionMapping = {
190 'TOP' : mathutils.Vector((0, 0, -1)),
191 'BOTTOM' : mathutils.Vector((0, 0, 1)),
192 'LEFT' : mathutils.Vector((0, 1, 0)),
193 'RIGHT' : mathutils.Vector((0, -1, 0)),
194 'FRONT' : mathutils.Vector((-1, 0, 0)),
195 'REAR' : mathutils.Vector((1, 0, 0))
198 #-----------------------------------------------------
199 def get_view_projection_matrix(context, settings):
201 Returns view projection matrix.
202 Projection matrix is either identity if 3d export is selected or
203 camera projection if a camera or view is selected.
204 Currently only orthographic projection is used. (Subject to discussion).
205 """
206 cam = settings['projectionThrough']
207 if cam == None:
208 mw = mathutils.Matrix()
209 mw.identity()
210 elif cam in projectionMapping.keys():
211 projection = mathutils.Matrix.OrthoProjection(projectionMapping[cam], 4)
212 mw = projection
213 else: # get camera with given name
214 c = context.scene.objects[cam]
215 mw = getCameraMatrix(c)
216 return mw
218 def _exportItem(ctx, o, mw, drawing, settings):
220 Export one item from export list.
221 mw - modelview
223 if settings['verbose']: print('Exporting %s' % o)
224 #mx = ob.matrix.copy()
225 #print 'deb: ob =', ob #---------
226 #print 'deb: ob.type =', ob.type #---------
227 #print 'deb: mx =\n', mx #---------
228 #print 'deb: mw0 =\n', mw0 #---------
229 #mx_n is trans-matrix for normal_vectors for front-side faces
230 mx = o.matrix_world
231 viewRotation = mw.to_euler().to_matrix()
232 mx_n = o.rotation_euler.to_matrix() * viewRotation
233 mx *= mw
235 #mx_inv = mx.copy().invert()
236 elayer, ecolor, eltype = getCommons(o, settings)
237 if settings['verbose']:
238 print('elayer=%s, ecolor=%s, eltype=%s' % (elayer, ecolor, eltype))
239 #TODO: use o.boundBox for drawing extends ??
241 if elayer != None and not drawing.containsLayer(elayer):
242 if ecolor!=None: tempcolor = ecolor
243 else: tempcolor = settings['layercolor_def']
244 drawing.addLayer(elayer, tempcolor)
246 if DEBUG: pydevd.settrace()
247 if (o.type == 'MESH') and settings['mesh_as']:
248 from .primitive_exporters.mesh_exporter import MeshDXFExporter
249 e = MeshDXFExporter(settings)
250 elif (o.type == 'CURVE') and settings['curve_as']:
251 from .primitive_exporters.curve_exporter import CurveDXFExporter
252 e = CurveDXFExporter(settings)
253 elif (o.type == 'EMPTY') and settings['empty_as']:
254 from .primitive_exporters.empty_exporter import EmptyDXFExporter
255 e = EmptyDXFExporter(settings)
256 elif (o.type == 'TEXT') and settings['text_as']:
257 from .primitive_exporters.text_exporter import TextDXFExporter
258 e = TextDXFExporter(settings)
259 elif (o.type == 'CAMERA') and settings['camera_as']:
260 from .primitive_exporters.camera_exporter import CameraDXFExporter
261 e = CameraDXFExporter(settings)
262 elif (o.type == 'LAMP') and settings['lamp_as']:
263 from .primitive_exporters.lamp_exporter import LampDXFExporter
264 e = LampDXFExporter(settings)
266 return e.export(ctx, drawing, o, mx, mx_n, color=ecolor, layer=elayer, lineType=eltype)