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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
"""
#----------------------------------------------
# (c) jm soler juillet 2004-juin 2005,
# released under Blender Artistic Licence
# for the Blender 2.34 Python Scripts Bundle.
#----------------------------------------------
# Page officielle :
# http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_import_ai_en.htm
# Communiquer les problemes et erreurs sur:
# http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender
#
# 0.1.1 : 2004/08/03, bug in boudingbox reading when Value are negative
# 0.1.2 : 2005/06/12, gmove tranformation properties
# 0.1.3 : 2005/06/25, added a __name__ test to use the script alone
# 0.1.4 : 2005/06/25, closepath improvements
# 0.1.5 : 2005/06/25, ...
# 0.1.6 : 2005/06/26, warning for compacted file
compatibility increased up to AI 10.0 plain text
# 0.1.7 : 2005/06/25, two more closepath improvements
#
# 0.1.8 : 2006/07/03, two more closepath improvements
"""
SHARP_IMPORT=0
SCALE=1
NOTHING_TODO=1
AI_VERSION=''
GSTACK = []
GSCALE = []
GTRANSLATE = []
import sys
#oldpath=sys.path
import Blender
BLversion=Blender.Get('version')
try:
import nt
os=nt
os.sep='\\'
except:
import posix
os=posix
os.sep='/'
def isdir(path):
try:
st = os.stat(path)
return 1
except:
return 0
def split(pathname):
if pathname.find(os.sep)!=-1:
k0=pathname.split(os.sep)
else:
if os.sep=='/':
k0=pathname.split('\\')
else:
k0=pathname.split('/')
directory=pathname.replace(k0[len(k0)-1],'')
Name=k0[len(k0)-1]
return directory, Name
def join(l0,l1):
return l0+os.sep+l1
os.isdir=isdir
os.split=split
os.join=join
def filtreFICHIER(nom):
f=open(nom,'rU')
t=f.readlines()
f.close()
if len(t)>1 and t[0].find('EPSF')==-1:
return t
else:
name = "OK?%t| Not a valid file or an empty file ... " # if no %xN int is set, indices start from 1
result = Blender.Draw.PupMenu(name)
return 'false'
#===============================
# Data
#===============================
#===============================
# Blender Curve Data
#===============================
objBEZIER=0
objSURFACE=5
typBEZIER3D=1 #3D
typBEZIER2D=9 #2D
class Bez:
def __init__(self):
self.co=[]
self.ha=[0,0]
self.tag=''
class ITEM:
def __init__(self):
self.type = typBEZIER3D,
self.pntsUV = [0,0]
self.resolUV = [32,0]
self.orderUV = [0,0]
self.flagUV = [0,0]
self.Origine = [0.0,0.0]
self.beziers_knot = []
class COURBE:
def __init__(self):
self.magic_number='3DG3'
self.type = objBEZIER
self.number_of_items = 0
self.ext1_ext2 = [0,0]
self.matrix = """0.0 0.0 1.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0 """
self.ITEM = {}
courbes=COURBE()
PATTERN={}
BOUNDINGBOX={'rec':[],'coef':1.0}
npat=0
#=====================================================================
#======== name of the curve in teh courbes dictionnary ===============
#=====================================================================
n0=0
#=====================================================================
#====================== current Point ================================
#=====================================================================
CP=[0.0,0.0] #currentPoint
# modifs 12/06/2005
#=====================================================================
#====================== current transform ============================
#=====================================================================
class transform:
def __init__(self,matrix=[1,0,01],x=0.0,y=0.0):
self.matrix=matrix[:]
self.xy=[x,y]
def G_move(l,a):
global GSCALE, GTRANSLATE, GSTACK
#print GSCALE, GTRANSLATE, GSTACK
return str((float(l)+GTRANSLATE[a]+GSTACK[-1].xy[a])*GSCALE[a])
# modifs 12/06/2005
#=====================================================================
#===== to compare last position to the original move to displacement =
#===== needed for cyclic efinition =================================
#=====================================================================
def test_egalitedespositions(f1,f2):
if f1[0]==f2[0] and f1[1]==f2[1]:
return Blender.TRUE
else:
return Blender.FALSE
def Open_GEOfile(dir,nom):
if BLversion>=233:
in_editmode = Blender.Window.EditMode()
if in_editmode: Blender.Window.EditMode(0)
Blender.Load(dir+nom+'OOO.obj', 1)
BO=Blender.Object.Get()
BO[-1].RotY=0.0
BO[-1].RotX=1.57
BO[-1].makeDisplayList()
Blender.Window.RedrawAll()
else:
print "Not yet implemented"
def create_GEOtext(courbes):
global SCALE, B, BOUNDINGBOX
r=BOUNDINGBOX['rec']
if SCALE==1:
SCALE=1.0
elif SCALE==2:
SCALE=r[2]-r[0]
elif SCALE==3:
SCALE=r[3]-r[1]
t=[]
t.append(courbes.magic_number+'\n')
t.append(str(courbes.type)+'\n')
t.append(str(courbes.number_of_items)+'\n')
t.append(str(courbes.ext1_ext2[0])+' '+str(courbes.ext1_ext2[1])+'\n')
t.append(courbes.matrix+'\n')
for k in courbes.ITEM.keys():
if len(courbes.ITEM[k].beziers_knot)>1 :
t.append("%s\n"%courbes.ITEM[k].type)
t.append("%s %s \n"%(courbes.ITEM[k].pntsUV[0],courbes.ITEM[k].pntsUV[1]))
t.append("%s %s \n"%(courbes.ITEM[k].resolUV[0],courbes.ITEM[k].resolUV[1]))
t.append("%s %s \n"%(courbes.ITEM[k].orderUV[0],courbes.ITEM[k].orderUV[1]))
t.append("%s %s \n"%(courbes.ITEM[k].flagUV[0],courbes.ITEM[k].flagUV[1]))
flag =courbes.ITEM[k].flagUV[0]
for k2 in range(len(courbes.ITEM[k].beziers_knot)):
#print k2
k1 =courbes.ITEM[k].beziers_knot[k2]
t.append("%4f 0.0 %4f \n"%(float(k1.co[2])/SCALE,float(k1.co[3])/SCALE))
t.append("%4f 0.0 %4f \n"%(float(k1.co[4])/SCALE,float(k1.co[5])/SCALE))
t.append("%4f 0.0 %4f \n"%(float(k1.co[0])/SCALE,float(k1.co[1])/SCALE))
t.append(str(k1.ha[0])+' '+str(k1.ha[1])+'\n')
return t
def save_GEOfile(dir,nom,t):
f=open(dir+nom+'OOO.obj','w')
f.writelines(t)
f.close()
#warning = "REMINDER : %t | Do not forget to rename your blender file NOW ! %x1"
#result = Blender.Draw.PupMenu(warning)
#=====================================================================
#===== AI format : DEBUT =========================
#=====================================================================
def mouvement_vers(l,n0,CP):
if n0 in courbes.ITEM.keys():
n0+=1
CP=[l[-3].replace('d',''),l[-2]]
courbes.ITEM[n0]=ITEM()
courbes.ITEM[n0].Origine=[l[-3].replace('d',''),l[-2]]
B=Bez()
B.co=[CP[0],CP[1],CP[0],CP[1],CP[0],CP[1]]
B.ha=[0,0]
B.tag=l[-1]
courbes.ITEM[n0].beziers_knot.append(B)
return courbes,n0,CP
def courbe_vers_c(l,l2, n0,CP): #c,C
B=Bez()
B.co=[l[4],l[5],l[2],l[3],l[4],l[5]]
B.tag=l[-1]
B.ha=[0,0]
BP=courbes.ITEM[n0].beziers_knot[-1]
BP.co[0]=l[0]
BP.co[1]=l[1]
courbes.ITEM[n0].beziers_knot.append(B)
CP=[B.co[4],B.co[5]]
return courbes,n0,CP
def courbe_vers_v(l,n0,CP): #v-V
B=Bez()
B.tag=l[-1]
B.co=[l[2],l[3],l[0],l[1],l[2],l[3]]
B.ha=[0,0]
courbes.ITEM[n0].beziers_knot.append(B)
CP=[B.co[4],B.co[5]]
return courbes,n0,CP
def courbe_vers_y(l,n0,CP): #y
B=Bez()
B.tag=l[-1]
B.co=[l[2],l[3],l[2],l[3],l[2],l[3]]
B.ha=[0,0]
BP=courbes.ITEM[n0].beziers_knot[-1]
BP.co[0]=l[0]
BP.co[1]=l[1]
courbes.ITEM[n0].beziers_knot.append(B)
CP=[B.co[4],B.co[5]]
return courbes,n0,CP
def ligne_tracee_l(l,n0,CP):
B=Bez()
B.tag=l[-1]
B.co=[l[0],l[1],l[0],l[1],l[0],l[1]]
B.ha=[0,0]
BP=courbes.ITEM[n0].beziers_knot[-1]
courbes.ITEM[n0].beziers_knot.append(B)
CP=[B.co[4],B.co[5]]
return courbes,n0,CP
def ligne_fermee(l,n0,CP):
courbes.ITEM[n0].flagUV[0]=1
if len(courbes.ITEM[n0].beziers_knot)>1:
BP=courbes.ITEM[n0].beziers_knot[-1]
BP0=courbes.ITEM[n0].beziers_knot[0]
if BP.tag not in ['l','L']:
BP.co[0]=BP0.co[0] #4-5 point prec
BP.co[1]=BP0.co[1]
del courbes.ITEM[n0].beziers_knot[0]
return courbes,n0,CP
def passe(l,n0,CP):
return courbes,n0,CP
Actions= { "C" : courbe_vers_c,
"c" : courbe_vers_c,
"V" : courbe_vers_v,
"v" : courbe_vers_v,
"Y" : courbe_vers_y,
"y" : courbe_vers_y,
"m" : mouvement_vers,
"l" : ligne_tracee_l,
"L" : ligne_tracee_l,
"F" : passe,
"f" : ligne_fermee,
"B" : passe,
"b" : ligne_fermee,
"S" : passe,
"s" : ligne_fermee,
"N" : ligne_fermee,
"n" : passe,
}
TAGcourbe=Actions.keys()
def pik_pattern(t,l):
global npat, PATTERN, BOUNDINGBOX, AI_VERSION
while t[l].find('%%EndSetup')!=0:
if t[l].find('%%Creator: Adobe Illustrator(R)')!=-1:
print t[l]
AI_VERSION=t[l].split()[-1]
print AI_VERSION
if t[l].find('%%BoundingBox:')!=-1:
t[l]=t[l][t[l].find(':')+1:]
l0=t[l].split()
BOUNDINGBOX['rec']=[float(l0[-4]),float(l0[-3]),float(l0[-2]),float(l0[-1])]
r=BOUNDINGBOX['rec']
BOUNDINGBOX['coef']=(r[3]-r[1])/(r[2]-r[0])
#print l,
if t[l].find('BeginPattern')!=-1:
nomPattern=t[l][t[l].find('(')+1:t[l].find(')')]
PATTERN[nomPattern]={}
if t[l].find('BeginPatternLayer')!=-1:
npat+=1
PATTERN[nomPattern][npat]=[]
while t[l].find('EndPatternLayer')==-1:
#print t[l]
PATTERN[nomPattern][npat].append(l)
l+=1
if l+1<len(t):
l=l+1
else:
return 1,l
return 1,l
def scan_FILE(nom):
global CP, courbes, SCALE, NOTHING_TODO
dir,name=split(nom)
name=name.split('.')
n0=0
result=0
t=filtreFICHIER(nom)
if nom.upper().find('.AI')!=-1 and t!='false':
if not SHARP_IMPORT:
warning = "Select Size : %t| As is %x1 | Scale on Height %x2| Scale on Width %x3"
SCALE = Blender.Draw.PupMenu(warning)
npat=0
l=0
do=0
while l <len(t)-1 :
if not do:
do,l=pik_pattern(t,l)
#print 'len(t)',len(t)
t[l].replace('\n','')
if t[l].find('%%EOF')==0:
break
if t[l][0]!='%':
l0=t[l].split()
#print l0
if l0[0][0] in ['F','f','N','n','B','b']:
l3=l0[0][0]
courbes,n0,CP=Actions[l3](l3,n0,CP)
l0[0]=l0[1:]
if l0[-1] in TAGcourbe:
NOTHING_TODO=0
if l0[-1] in ['C','c']:
l2=t[l+1].split()
courbes,n0,CP=Actions[l0[-1]](l0,l2,n0,CP)
else:
courbes,n0,CP=Actions[l0[-1]](l0,n0,CP)
l=l+1; #print l
t=[]
courbes.number_of_items=len(courbes.ITEM.keys())
for k in courbes.ITEM.keys():
courbes.ITEM[k].pntsUV[0] =len(courbes.ITEM[k].beziers_knot)
if courbes.number_of_items>0:
if len(PATTERN.keys() )>0:
#print len(PATTERN.keys() )
warning = "Pattern list (for info not used): %t| "
p0=1
for P in PATTERN.keys():
warning+="%s %%x%s|"%(P,p0)
p0+=1
Padd = Blender.Draw.PupMenu(warning)
t=create_GEOtext(courbes)
save_GEOfile(dir,name[0],t)
# 0.1.8 ---------------------------------
# [O.select(0) for O in Blender.Scene.getCurrent().getChildren()]
# 0.1.8 ---------------------------------
Open_GEOfile(dir,name[0])
# 0.1.8 ---------------------------------
Blender.Object.Get()[-1].setName(name[0])
# 0.1.8 ---------------------------------
else:
pass
#=====================================================================
#====================== AI format mouvements =========================
#=====================================================================
#=========================================================
# une sorte de contournement qui permet d'utiliser la fonction
# et de documenter les variables Window.FileSelector
#=========================================================
def fonctionSELECT(nom):
global NOTHING_TODO,AI_VERSION
scan_FILE(nom)
if NOTHING_TODO==1:
warning = "AI %s compatible file "%AI_VERSION+" but nothing to do ? %t| Perhaps a compacted file ... "
NOTHING = Blender.Draw.PupMenu(warning)
if __name__=="__main__":
Blender.Window.FileSelector (fonctionSELECT, 'SELECT AI FILE')
#sys.path=oldpath
|