~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to release/scripts/bpymodules/ai2obj.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
#----------------------------------------------
3
 
# (c) jm soler juillet 2004-juin 2005, 
4
 
#  released under Blender Artistic Licence 
5
 
#    for the Blender 2.34 Python Scripts Bundle.
6
 
#----------------------------------------------
7
 
# Page officielle :
8
 
#   http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_import_ai_en.htm
9
 
# Communiquer les problemes et erreurs sur:
10
 
#   http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender
11
 
#
12
 
# 0.1.1 : 2004/08/03, bug in boudingbox reading when Value are negative
13
 
# 0.1.2 : 2005/06/12, gmove tranformation properties
14
 
# 0.1.3 : 2005/06/25, added a __name__ test to use the script alone
15
 
# 0.1.4 : 2005/06/25, closepath improvements 
16
 
# 0.1.5 : 2005/06/25, ... 
17
 
# 0.1.6 : 2005/06/26, warning for compacted file 
18
 
                      compatibility increased up to AI 10.0 plain text 
19
 
# 0.1.7 : 2005/06/25, two more closepath improvements 
20
 
#
21
 
# 0.1.8 : 2006/07/03, two more closepath improvements 
22
 
 
23
 
"""
24
 
SHARP_IMPORT=0
25
 
SCALE=1
26
 
NOTHING_TODO=1
27
 
AI_VERSION=''
28
 
 
29
 
GSTACK          =   []
30
 
GSCALE          =   []
31
 
GTRANSLATE      =   []
32
 
 
33
 
import sys
34
 
#oldpath=sys.path
35
 
import Blender
36
 
BLversion=Blender.Get('version')
37
 
 
38
 
try:
39
 
    import nt
40
 
    os=nt
41
 
    os.sep='\\'
42
 
 
43
 
except:    
44
 
    import posix
45
 
    os=posix
46
 
    os.sep='/'
47
 
    
48
 
def isdir(path):
49
 
    try:
50
 
        st = os.stat(path)
51
 
        return 1 
52
 
    except:
53
 
        return 0
54
 
    
55
 
def split(pathname):
56
 
         if pathname.find(os.sep)!=-1:
57
 
             k0=pathname.split(os.sep)
58
 
         else:
59
 
            if os.sep=='/':
60
 
                k0=pathname.split('\\')
61
 
            else:
62
 
                k0=pathname.split('/') 
63
 
 
64
 
         directory=pathname.replace(k0[len(k0)-1],'')
65
 
         Name=k0[len(k0)-1]
66
 
         return directory, Name
67
 
        
68
 
def join(l0,l1):        
69
 
     return  l0+os.sep+l1
70
 
    
71
 
os.isdir=isdir
72
 
os.split=split
73
 
os.join=join
74
 
 
75
 
def filtreFICHIER(nom):
76
 
     f=open(nom,'rU')
77
 
     t=f.readlines()
78
 
     f.close()
79
 
     
80
 
     if len(t)>1 and t[0].find('EPSF')==-1: 
81
 
          return t   
82
 
     else:
83
 
         name = "OK?%t| Not a valid file or an empty file ... "  # if no %xN int is set, indices start from 1
84
 
         result = Blender.Draw.PupMenu(name)
85
 
          
86
 
         return 'false'
87
 
        
88
 
#===============================
89
 
# Data
90
 
#===============================
91
 
#===============================
92
 
# Blender Curve Data
93
 
#===============================
94
 
objBEZIER=0
95
 
objSURFACE=5
96
 
typBEZIER3D=1  #3D
97
 
typBEZIER2D=9  #2D
98
 
 
99
 
class Bez:
100
 
      def __init__(self):
101
 
           self.co=[]
102
 
           self.ha=[0,0]
103
 
           self.tag=''
104
 
           
105
 
class ITEM:
106
 
      def __init__(self):
107
 
               self.type        =  typBEZIER3D,         
108
 
               self.pntsUV      =  [0,0]              
109
 
               self.resolUV     =  [32,0]             
110
 
               self.orderUV     =  [0,0]              
111
 
               self.flagUV      =  [0,0]              
112
 
               self.Origine     =  [0.0,0.0]
113
 
               self.beziers_knot = []
114
 
 
115
 
class COURBE:
116
 
      def __init__(self):
117
 
              self.magic_number='3DG3'              
118
 
              self.type            =  objBEZIER     
119
 
              self.number_of_items =  0             
120
 
              self.ext1_ext2       =  [0,0]         
121
 
              self.matrix          =  """0.0 0.0 1.0 0.0
122
 
0.0 1.0 0.0 0.0
123
 
0.0 0.0 1.0 0.0
124
 
0.0 0.0 0.0 1.0 """ 
125
 
              self.ITEM            = {}
126
 
 
127
 
courbes=COURBE()
128
 
 
129
 
PATTERN={}
130
 
 
131
 
BOUNDINGBOX={'rec':[],'coef':1.0}
132
 
npat=0
133
 
#=====================================================================
134
 
#======== name of the curve in teh courbes dictionnary ===============
135
 
#=====================================================================
136
 
n0=0
137
 
 
138
 
#=====================================================================
139
 
#====================== current Point ================================
140
 
#=====================================================================
141
 
CP=[0.0,0.0] #currentPoint
142
 
 
143
 
 
144
 
# modifs 12/06/2005       
145
 
#=====================================================================
146
 
#====================== current transform ============================
147
 
#=====================================================================
148
 
class transform:
149
 
      def __init__(self,matrix=[1,0,01],x=0.0,y=0.0):
150
 
          self.matrix=matrix[:]
151
 
          self.xy=[x,y]          
152
 
 
153
 
def G_move(l,a):
154
 
    global GSCALE, GTRANSLATE, GSTACK
155
 
    #print GSCALE, GTRANSLATE, GSTACK
156
 
    return str((float(l)+GTRANSLATE[a]+GSTACK[-1].xy[a])*GSCALE[a])
157
 
# modifs 12/06/2005
158
 
 
159
 
 
160
 
#=====================================================================
161
 
#===== to compare last position to the original move to displacement =
162
 
#=====  needed for cyclic efinition  =================================
163
 
#=====================================================================
164
 
def test_egalitedespositions(f1,f2):
165
 
    if f1[0]==f2[0] and f1[1]==f2[1]:
166
 
       return Blender.TRUE
167
 
    else:
168
 
       return Blender.FALSE
169
 
 
170
 
 
171
 
def Open_GEOfile(dir,nom):
172
 
    if BLversion>=233:
173
 
       in_editmode = Blender.Window.EditMode()
174
 
       if in_editmode: Blender.Window.EditMode(0)
175
 
       Blender.Load(dir+nom+'OOO.obj', 1)
176
 
       BO=Blender.Object.Get()
177
 
       BO[-1].RotY=0.0
178
 
       BO[-1].RotX=1.57
179
 
       BO[-1].makeDisplayList() 
180
 
       Blender.Window.RedrawAll()
181
 
    else:
182
 
       print "Not yet implemented"
183
 
 
184
 
def create_GEOtext(courbes):
185
 
    global SCALE, B, BOUNDINGBOX
186
 
    r=BOUNDINGBOX['rec']
187
 
 
188
 
    if SCALE==1:
189
 
       SCALE=1.0
190
 
    elif SCALE==2:
191
 
       SCALE=r[2]-r[0]
192
 
    elif SCALE==3:
193
 
       SCALE=r[3]-r[1]
194
 
 
195
 
    t=[]
196
 
    t.append(courbes.magic_number+'\n')
197
 
    t.append(str(courbes.type)+'\n')
198
 
    t.append(str(courbes.number_of_items)+'\n')
199
 
    t.append(str(courbes.ext1_ext2[0])+' '+str(courbes.ext1_ext2[1])+'\n')
200
 
    t.append(courbes.matrix+'\n')
201
 
    
202
 
    for k in courbes.ITEM.keys():
203
 
        if len(courbes.ITEM[k].beziers_knot)>1 :
204
 
            t.append("%s\n"%courbes.ITEM[k].type)
205
 
            t.append("%s %s \n"%(courbes.ITEM[k].pntsUV[0],courbes.ITEM[k].pntsUV[1]))
206
 
            t.append("%s %s \n"%(courbes.ITEM[k].resolUV[0],courbes.ITEM[k].resolUV[1]))
207
 
            t.append("%s %s \n"%(courbes.ITEM[k].orderUV[0],courbes.ITEM[k].orderUV[1]))
208
 
            t.append("%s %s \n"%(courbes.ITEM[k].flagUV[0],courbes.ITEM[k].flagUV[1]))
209
 
 
210
 
            flag =courbes.ITEM[k].flagUV[0]
211
 
 
212
 
            for k2 in range(len(courbes.ITEM[k].beziers_knot)):
213
 
               #print k2 
214
 
               k1 =courbes.ITEM[k].beziers_knot[k2]
215
 
               t.append("%4f 0.0 %4f \n"%(float(k1.co[2])/SCALE,float(k1.co[3])/SCALE))               
216
 
               t.append("%4f 0.0 %4f \n"%(float(k1.co[4])/SCALE,float(k1.co[5])/SCALE))
217
 
               t.append("%4f 0.0 %4f \n"%(float(k1.co[0])/SCALE,float(k1.co[1])/SCALE))
218
 
               
219
 
               t.append(str(k1.ha[0])+' '+str(k1.ha[1])+'\n')
220
 
    return t
221
 
 
222
 
def save_GEOfile(dir,nom,t):
223
 
     f=open(dir+nom+'OOO.obj','w')
224
 
     f.writelines(t)
225
 
     f.close()
226
 
     #warning = "REMINDER : %t | Do not forget to rename your blender file NOW ! %x1"
227
 
     #result = Blender.Draw.PupMenu(warning)
228
 
 
229
 
 
230
 
#=====================================================================
231
 
#=====      AI format   :  DEBUT             =========================
232
 
#=====================================================================
233
 
def mouvement_vers(l,n0,CP):
234
 
    if n0 in courbes.ITEM.keys():
235
 
       n0+=1
236
 
 
237
 
    CP=[l[-3].replace('d',''),l[-2]] 
238
 
    courbes.ITEM[n0]=ITEM() 
239
 
    courbes.ITEM[n0].Origine=[l[-3].replace('d',''),l[-2]] 
240
 
    
241
 
    B=Bez()
242
 
    B.co=[CP[0],CP[1],CP[0],CP[1],CP[0],CP[1]]
243
 
    B.ha=[0,0]
244
 
    B.tag=l[-1]
245
 
 
246
 
    courbes.ITEM[n0].beziers_knot.append(B)       
247
 
 
248
 
    return  courbes,n0,CP     
249
 
       
250
 
def courbe_vers_c(l,l2, n0,CP): #c,C
251
 
 
252
 
    B=Bez()
253
 
    B.co=[l[4],l[5],l[2],l[3],l[4],l[5]]
254
 
    B.tag=l[-1]
255
 
    B.ha=[0,0]
256
 
        
257
 
    BP=courbes.ITEM[n0].beziers_knot[-1]
258
 
    
259
 
    BP.co[0]=l[0]
260
 
    BP.co[1]=l[1]
261
 
 
262
 
    courbes.ITEM[n0].beziers_knot.append(B)
263
 
 
264
 
    CP=[B.co[4],B.co[5]]
265
 
    return  courbes,n0,CP
266
 
     
267
 
     
268
 
def courbe_vers_v(l,n0,CP): #v-V
269
 
 
270
 
    B=Bez()
271
 
    B.tag=l[-1]
272
 
    B.co=[l[2],l[3],l[0],l[1],l[2],l[3]]
273
 
    B.ha=[0,0]
274
 
 
275
 
    courbes.ITEM[n0].beziers_knot.append(B) 
276
 
   
277
 
    CP=[B.co[4],B.co[5]]    
278
 
    return  courbes,n0,CP
279
 
         
280
 
def courbe_vers_y(l,n0,CP): #y
281
 
    B=Bez()
282
 
    B.tag=l[-1]
283
 
    B.co=[l[2],l[3],l[2],l[3],l[2],l[3]]
284
 
    B.ha=[0,0]
285
 
        
286
 
    BP=courbes.ITEM[n0].beziers_knot[-1]
287
 
    BP.co[0]=l[0]
288
 
    BP.co[1]=l[1]   
289
 
     
290
 
    courbes.ITEM[n0].beziers_knot.append(B)    
291
 
    CP=[B.co[4],B.co[5]] 
292
 
    return  courbes,n0,CP
293
 
     
294
 
      
295
 
def ligne_tracee_l(l,n0,CP):
296
 
    B=Bez()
297
 
    B.tag=l[-1]
298
 
    B.co=[l[0],l[1],l[0],l[1],l[0],l[1]]
299
 
    B.ha=[0,0]
300
 
 
301
 
    BP=courbes.ITEM[n0].beziers_knot[-1]    
302
 
 
303
 
    courbes.ITEM[n0].beziers_knot.append(B)
304
 
    CP=[B.co[4],B.co[5]]
305
 
    return  courbes,n0,CP    
306
 
 
307
 
def ligne_fermee(l,n0,CP):
308
 
    courbes.ITEM[n0].flagUV[0]=1
309
 
    
310
 
    if len(courbes.ITEM[n0].beziers_knot)>1:
311
 
        BP=courbes.ITEM[n0].beziers_knot[-1]
312
 
        BP0=courbes.ITEM[n0].beziers_knot[0]
313
 
           
314
 
        if BP.tag not in ['l','L']: 
315
 
           BP.co[0]=BP0.co[0]  #4-5 point prec
316
 
           BP.co[1]=BP0.co[1]
317
 
        
318
 
    del courbes.ITEM[n0].beziers_knot[0]
319
 
    return  courbes,n0,CP    
320
 
 
321
 
def passe(l,n0,CP):
322
 
    return  courbes,n0,CP
323
 
 
324
 
Actions=   {     "C" : courbe_vers_c,
325
 
                 "c" : courbe_vers_c,
326
 
                 "V" : courbe_vers_v,
327
 
                 "v" : courbe_vers_v,
328
 
                 "Y" : courbe_vers_y,
329
 
                 "y" : courbe_vers_y,
330
 
                 "m" : mouvement_vers,
331
 
                 "l" : ligne_tracee_l,
332
 
                 "L" : ligne_tracee_l,
333
 
                 "F" :  passe,                      
334
 
                 "f" : ligne_fermee,
335
 
                 "B" :  passe,
336
 
                 "b" : ligne_fermee,
337
 
                       "S" :    passe,
338
 
                 "s" : ligne_fermee,
339
 
                 "N" : ligne_fermee,
340
 
                       "n" :    passe,
341
 
                 }
342
 
     
343
 
TAGcourbe=Actions.keys()
344
 
                 
345
 
def pik_pattern(t,l):
346
 
    global npat, PATTERN, BOUNDINGBOX, AI_VERSION
347
 
    while t[l].find('%%EndSetup')!=0:
348
 
          if t[l].find('%%Creator: Adobe Illustrator(R)')!=-1:
349
 
               print  t[l]
350
 
               AI_VERSION=t[l].split()[-1]
351
 
               print  AI_VERSION
352
 
                        
353
 
          if t[l].find('%%BoundingBox:')!=-1:
354
 
             t[l]=t[l][t[l].find(':')+1:]    
355
 
             l0=t[l].split()
356
 
             BOUNDINGBOX['rec']=[float(l0[-4]),float(l0[-3]),float(l0[-2]),float(l0[-1])]
357
 
             r=BOUNDINGBOX['rec']
358
 
             BOUNDINGBOX['coef']=(r[3]-r[1])/(r[2]-r[0])
359
 
          #print l,
360
 
          if  t[l].find('BeginPattern')!=-1:
361
 
              nomPattern=t[l][t[l].find('(')+1:t[l].find(')')]
362
 
              PATTERN[nomPattern]={}
363
 
 
364
 
          if  t[l].find('BeginPatternLayer')!=-1:
365
 
               npat+=1
366
 
               PATTERN[nomPattern][npat]=[]
367
 
               while t[l].find('EndPatternLayer')==-1:
368
 
                     #print t[l] 
369
 
                     PATTERN[nomPattern][npat].append(l) 
370
 
                     l+=1   
371
 
          if l+1<len(t):
372
 
             l=l+1
373
 
          else:
374
 
             return 1,l
375
 
    return 1,l
376
 
             
377
 
def scan_FILE(nom):
378
 
  global CP, courbes, SCALE, NOTHING_TODO
379
 
  dir,name=split(nom)
380
 
  name=name.split('.')
381
 
  n0=0
382
 
  result=0
383
 
  t=filtreFICHIER(nom)
384
 
  
385
 
  if nom.upper().find('.AI')!=-1 and t!='false':
386
 
      if not SHARP_IMPORT:
387
 
            warning = "Select Size : %t| As is %x1 | Scale on Height %x2| Scale on Width %x3" 
388
 
            SCALE = Blender.Draw.PupMenu(warning)
389
 
         
390
 
      npat=0
391
 
      l=0
392
 
      do=0
393
 
      while l <len(t)-1 :
394
 
           if not do:
395
 
              do,l=pik_pattern(t,l)
396
 
           #print 'len(t)',len(t)        
397
 
           t[l].replace('\n','')
398
 
           if t[l].find('%%EOF')==0:
399
 
              break 
400
 
           if t[l][0]!='%':
401
 
                l0=t[l].split()
402
 
                #print l0  
403
 
                if l0[0][0] in ['F','f','N','n','B','b']:
404
 
                   l3=l0[0][0]
405
 
                   courbes,n0,CP=Actions[l3](l3,n0,CP)
406
 
                   l0[0]=l0[1:]
407
 
        
408
 
                if l0[-1] in TAGcourbe:
409
 
                    NOTHING_TODO=0
410
 
                    if l0[-1] in ['C','c']:
411
 
                       l2=t[l+1].split()
412
 
                       courbes,n0,CP=Actions[l0[-1]](l0,l2,n0,CP)
413
 
                    else: 
414
 
                       courbes,n0,CP=Actions[l0[-1]](l0,n0,CP)
415
 
           l=l+1; #print l                    
416
 
      t=[]
417
 
      
418
 
            
419
 
      courbes.number_of_items=len(courbes.ITEM.keys())      
420
 
      for k in courbes.ITEM.keys():
421
 
              courbes.ITEM[k].pntsUV[0] =len(courbes.ITEM[k].beziers_knot)
422
 
              
423
 
 
424
 
      if courbes.number_of_items>0:
425
 
         if len(PATTERN.keys() )>0:
426
 
            #print len(PATTERN.keys() )
427
 
            warning = "Pattern list (for info not used): %t| "
428
 
            p0=1
429
 
            for P in PATTERN.keys():
430
 
                warning+="%s %%x%s|"%(P,p0)
431
 
                p0+=1 
432
 
            Padd = Blender.Draw.PupMenu(warning)
433
 
            
434
 
         t=create_GEOtext(courbes)
435
 
         save_GEOfile(dir,name[0],t)
436
 
 
437
 
         # 0.1.8 ---------------------------------  
438
 
         # [O.select(0) for O in Blender.Scene.getCurrent().getChildren()]
439
 
         # 0.1.8 ---------------------------------
440
 
 
441
 
         Open_GEOfile(dir,name[0])
442
 
 
443
 
         # 0.1.8 ---------------------------------
444
 
         Blender.Object.Get()[-1].setName(name[0])
445
 
         # 0.1.8 --------------------------------- 
446
 
 
447
 
      else:
448
 
          pass
449
 
#=====================================================================
450
 
#====================== AI format mouvements =========================
451
 
#=====================================================================
452
 
#=========================================================          
453
 
# une sorte de contournement qui permet d'utiliser la fonction
454
 
# et de documenter les variables Window.FileSelector
455
 
#=========================================================
456
 
def fonctionSELECT(nom):
457
 
    global NOTHING_TODO,AI_VERSION
458
 
    scan_FILE(nom)
459
 
    if NOTHING_TODO==1:
460
 
            warning = "AI  %s compatible file "%AI_VERSION+" but nothing to do ? %t| Perhaps a compacted file ... "
461
 
            NOTHING = Blender.Draw.PupMenu(warning)
462
 
 
463
 
if __name__=="__main__":
464
 
    Blender.Window.FileSelector (fonctionSELECT, 'SELECT AI FILE')
465
 
#sys.path=oldpath