~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to release/scripts/xfig_export.py

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!BPY
 
2
"""
 
3
Name: 'xfig export (.fig)'
 
4
Blender: 244
 
5
Group: 'Export'
 
6
Tooltip: 'Export selected mesh to xfig Format (.fig)'
 
7
"""
 
8
 
 
9
__author__ = 'Dino Ghilardi',  'Campbell Barton AKA Ideasman42'
 
10
__url__ = ("blender", "blenderartists.org")
 
11
__version__ = "1.1"
 
12
 
 
13
__bpydoc__ = """\
 
14
                This script exports the selected mesh to xfig (www.xfig.org) file format (i.e.: .fig)
 
15
 
 
16
                The starting point of this script was Anthony D'Agostino's raw triangle format export.
 
17
                (some code is still here and there, cut'n pasted from his script)
 
18
 
 
19
                Usage:<br>
 
20
                        Select the mesh to be exported and run this script from "File->Export" menu.
 
21
                        The toggle button 'export 3 files' enables the generation of 4 files: one global
 
22
                        and three with the three different views of the object.
 
23
                        This script is licensed under the GPL license. (c) Dino Ghilardi, 2005
 
24
                        
 
25
"""
 
26
 
 
27
# .fig export, mostly brutally cut-n pasted from the 
 
28
# 'Raw triangle export' (Anthony D'Agostino, http://www.redrival.com/scorpius)|
 
29
 
 
30
import Blender
 
31
from Blender import Draw
 
32
import BPyObject
 
33
#, meshtools
 
34
import sys
 
35
import bpy
 
36
#import time
 
37
 
 
38
# =================================
 
39
# === Write xfig Format.===
 
40
# =================================
 
41
 
 
42
def collect_edges(edges):
 
43
        """Gets the max-min coordinates of the mesh"""
 
44
        
 
45
        """Getting the extremes of the mesh to be exported"""
 
46
        
 
47
        maxX=maxY=maxZ = -1000000000
 
48
        minX=minY=minZ =  1000000000
 
49
        
 
50
        FGON= Blender.Mesh.EdgeFlags.FGON
 
51
        
 
52
        me = bpy.data.meshes.new()
 
53
        for ob_base in bpy.data.scenes.active.objects.context:
 
54
                for ob in BPyObject.getDerivedObjects(ob_base):
 
55
                        me.verts = None
 
56
                        try:    me.getFromObject(ob[0])
 
57
                        except: pass
 
58
                        
 
59
                        if me.edges:
 
60
                                me.transform(ob[1])
 
61
                                
 
62
                                for ed in me.edges:
 
63
                                        if not ed.flag & FGON:
 
64
                                                x,y,z = v1 = tuple(ed.v1.co)
 
65
                                                maxX = max(maxX, x)
 
66
                                                maxY = max(maxY, y)
 
67
                                                maxZ = max(maxZ, z)
 
68
                                                minX = min(minX, x)
 
69
                                                minY = min(minY, y)
 
70
                                                minZ = min(minZ, z)
 
71
                                                
 
72
                                                x,y,z = v2 = tuple(ed.v2.co)
 
73
                                                maxX = max(maxX, x)
 
74
                                                maxY = max(maxY, y)
 
75
                                                maxZ = max(maxZ, z)
 
76
                                                minX = min(minX, x)
 
77
                                                minY = min(minY, y)
 
78
                                                minZ = min(minZ, z)
 
79
                                                
 
80
                                                edges.append( (v1, v2) )
 
81
                                        
 
82
        me.verts = None # free memory
 
83
        return maxX,maxY,maxZ,minX,minY,minZ
 
84
 
 
85
def xfigheader(file):
 
86
        file.write('#FIG 3.2  Produced by xfig version 3.2.5-alpha5\n')
 
87
        file.write('Landscape\n')
 
88
        file.write('Center\n')
 
89
        file.write('Metric\n')
 
90
        file.write('A4\n')
 
91
        file.write('100.00\n')
 
92
        file.write('Single\n')
 
93
        file.write('-2\n')
 
94
        file.write('1200 2\n')
 
95
 
 
96
def figdata(file, edges, expview, bounds, scale, space):
 
97
        maxX,maxY,maxZ,minX,minY,minZ = bounds
 
98
        
 
99
        def xytransform(ed):
 
100
                """gives the face vertexes coordinates in the xfig format/translation (view xy)"""      
 
101
                x1,y1,z1 = ed[0]
 
102
                x2,y2,z2 = ed[1]
 
103
                y1=-y1; y2=-y2
 
104
                return x1,y1,z1,x2,y2,z2
 
105
 
 
106
        def xztransform(ed):
 
107
                """gives the face vertexes coordinates in the xfig format/translation (view xz)"""
 
108
                x1,y1,z1 = ed[0]
 
109
                x2,y2,z2 = ed[1]
 
110
                y1=-y1
 
111
                y2=-y2
 
112
                
 
113
                z1=-z1+maxZ-minY +space
 
114
                z2=-z2+maxZ-minY +space
 
115
                return x1,y1,z1,x2,y2,z2
 
116
 
 
117
        def yztransform(ed):
 
118
                """gives the face vertexes coordinates in the xfig format/translation (view xz)"""
 
119
                x1,y1,z1 = ed[0]
 
120
                x2,y2,z2 = ed[1]
 
121
                y1=-y1; y2=-y2
 
122
                z1=-(z1-maxZ-maxX-space)
 
123
                z2=-(z2-maxZ-maxX-space)
 
124
                return x1,y1,z1,x2,y2,z2
 
125
 
 
126
        def transform(ed, expview, scale):
 
127
                if              expview=='xy':
 
128
                        x1,y1,z1,x2,y2,z2 = xytransform(ed)
 
129
                        return int(x1*scale),int(y1*scale),int(x2*scale),int(y2*scale)
 
130
                elif    expview=='xz':
 
131
                        x1,y1,z1,x2,y2,z2 = xztransform(ed)
 
132
                        return int(x1*scale),int(z1*scale),int(x2*scale),int(z2*scale)
 
133
                elif    expview=='yz':
 
134
                        x1,y1,z1,x2,y2,z2 = yztransform(ed)
 
135
                        return int(z1*scale),int(y1*scale),int(z2*scale),int(y2*scale)
 
136
        
 
137
        
 
138
        """Prints all the xfig data (no header)"""
 
139
        for ed in edges:
 
140
                file.write('2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2\n')
 
141
                file.write('\t %i %i %i %i\n' % transform(ed, expview, scale))
 
142
 
 
143
def writexy(edges, bounds, filename, scale, space):
 
144
        """writes the x-y view file exported"""
 
145
        
 
146
        file = open(filename, 'wb')
 
147
        xfigheader(file)
 
148
        figdata(file, edges, 'xy', bounds, scale, space)
 
149
        file.close()
 
150
        print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
151
 
 
152
def writexz(edges, bounds, filename, scale, space):
 
153
        """writes the x-z view file exported"""
 
154
        #start = time.clock()
 
155
        file = open(filename, 'wb')
 
156
        xfigheader(file)
 
157
        figdata(file, edges, 'xz', bounds, scale, space)
 
158
        file.close()
 
159
        print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
160
 
 
161
def writeyz(edges, bounds, filename, scale, space):
 
162
        """writes the y-z view file exported"""
 
163
        
 
164
        #start = time.clock()
 
165
        file = open(filename, 'wb')
 
166
        xfigheader(file)
 
167
        figdata(file, edges, 'yz', bounds, scale, space)
 
168
        file.close()
 
169
        #end = time.clock()
 
170
        #seconds = " in %.2f %s" % (end-start, "seconds")
 
171
        print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
172
 
 
173
def writeall(edges, bounds, filename, scale=450, space=2.0):
 
174
        """writes all 3 views
 
175
        
 
176
        Every view is a combined object in the resulting xfig. file."""
 
177
        
 
178
        maxX,maxY,maxZ,minX,minY,minZ = bounds
 
179
        
 
180
        file = open(filename, 'wb')
 
181
 
 
182
        xfigheader(file)
 
183
        file.write('#upper view (7)\n')
 
184
        file.write('6 % i % i % i % i ')
 
185
        file.write('%.6f %.6f %.6f %.6f\n' % (minX, minY, maxX, maxY))
 
186
        
 
187
        figdata(file, edges, 'xy', bounds, scale, space)
 
188
        file.write('-6\n')
 
189
        file.write('#bottom view (1)\n')
 
190
        file.write('6 %i %i %i %i ')
 
191
        file.write('%.6f %.6f %.6f %.6f\n' % (minX, -minZ+maxZ-minY +space, maxX,-maxZ+maxZ-minY +space))
 
192
        
 
193
        figdata(file, edges, 'xz', bounds, scale, space)
 
194
        file.write('-6\n')
 
195
        
 
196
        file.write('#right view (3)\n')
 
197
        file.write('6 %i %i %i %i ')
 
198
        file.write('%.6f %.6f %.6f %.6f\n' % (minX, -minZ+maxZ-minY +space, maxX,-maxZ+maxZ-minY +space))
 
199
        figdata(file, edges, 'yz', bounds, scale, space)
 
200
        file.write('-6\n')
 
201
        
 
202
        file.close()
 
203
        print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
204
 
 
205
import BPyMessages
 
206
 
 
207
def write_ui(filename):
 
208
        if filename.lower().endswith('.fig'): filename = filename[:-4]
 
209
        
 
210
        PREF_SEP= Draw.Create(0)
 
211
        PREF_SCALE= Draw.Create(1200)
 
212
        PREF_SPACE= Draw.Create(2.0)
 
213
        
 
214
        block = [\
 
215
                ("Separate Files", PREF_SEP, "Export each view axis as a seperate file"),\
 
216
                ("Space: ", PREF_SPACE, 0.0, 10.0, "Space between views in blender units"),\
 
217
                ("Scale: ", PREF_SCALE, 10, 100000, "Scale, 1200 is a good default")]
 
218
        
 
219
        if not Draw.PupBlock("Export FIG", block):
 
220
                return
 
221
        
 
222
        edges = []
 
223
        bounds = collect_edges(edges)
 
224
        
 
225
        if PREF_SEP.val:
 
226
                writexy(edges, bounds, filename + '_XY.fig', PREF_SCALE.val, PREF_SPACE.val)
 
227
                writexz(edges, bounds, filename + '_XZ.fig', PREF_SCALE.val, PREF_SPACE.val)
 
228
                writeyz(edges, bounds, filename + '_YZ.fig', PREF_SCALE.val, PREF_SPACE.val)
 
229
        
 
230
        writeall(edges, bounds, filename + '.fig', PREF_SCALE.val, PREF_SPACE.val)
 
231
 
 
232
if __name__ == '__main__':
 
233
        Blender.Window.FileSelector(write_ui, 'Export XFIG', Blender.sys.makename(ext='.fig'))
 
234
        
 
 
b'\\ No newline at end of file'