home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 November / WNnov2005.iso / Windows / Equipement / Blender / blender-2.37a-windows.exe / $_5_ / .blender / scripts / bpymodules / gimp2obj.py < prev    next >
Text File  |  2005-03-21  |  10KB  |  321 lines

  1. """
  2. #----------------------------------------------
  3. # (c) jm soler juillet 2004, released under Blender Artistic Licence 
  4. #    for the Blender 2.34 Python Scripts Bundle.
  5. #----------------------------------------------
  6. # Page officielle :
  7. #   http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_import_gimp.htm
  8. # Communiquer les problemes et erreurs sur:
  9. #   http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender
  10. #---------------------------------------------
  11. """
  12. SHARP_IMPORT=0
  13. SCALE=1
  14.  
  15. import sys
  16. #oldpath=sys.path
  17.  
  18. import Blender
  19. BLversion=Blender.Get('version')
  20.  
  21. try:
  22.     import nt
  23.     os=nt
  24.     os.sep='\\'
  25.  
  26. except:    
  27.     import posix
  28.     os=posix
  29.     os.sep='/'
  30.     
  31. def isdir(path):
  32.     try:
  33.         st = os.stat(path)
  34.         return 1 
  35.     except:
  36.         return 0
  37.     
  38. def split(pathname):
  39.          if pathname.find(os.sep)!=-1:
  40.              k0=pathname.split(os.sep)
  41.          else:
  42.             if os.sep=='/':
  43.                 k0=pathname.split('\\')
  44.             else:
  45.                 k0=pathname.split('/') 
  46.  
  47.          directory=pathname.replace(k0[len(k0)-1],'')
  48.          Name=k0[len(k0)-1]
  49.          return directory, Name
  50.         
  51. def join(l0,l1):        
  52.      return  l0+os.sep+l1
  53.     
  54. os.isdir=isdir
  55. os.split=split
  56. os.join=join
  57.  
  58. def filtreFICHIER(nom):
  59.      f=open(nom,'r')
  60.      t=f.readlines()
  61.      f.close()
  62.      if len(t)==1 and t[0].find('\r'):
  63.               t=t[0].split('\r')
  64.      if len(t)>1 and t[1].find('#POINTS:')==0: 
  65.           return t   
  66.      else:
  67.          warning = "OK?%t| Not a valid file or an empty file ... "  # if no %xN int is set, indices start from 1
  68.          result = Blender.Draw.PupMenu(warning)
  69.          return "false"
  70.  
  71. #===============================
  72. # Data
  73. #===============================
  74. #===============================
  75. # Blender Curve Data
  76. #===============================
  77. objBEZIER=0
  78. objSURFACE=5
  79. typBEZIER3D=1  #3D
  80. typBEZIER2D=9  #2D
  81.  
  82. class Bez:
  83.       def __init__(self):
  84.            self.co=[]
  85.            self.ha=[0,0]
  86.  
  87.       def echo(self):
  88.           #print 'co = ', self.co 
  89.           #print 'ha = ', self.ha
  90.           pass
  91.            
  92. class ITEM:
  93.       def __init__(self):
  94.                self.type        =  typBEZIER3D,       
  95.                self.pntsUV      =  [0,0]              
  96.                self.resolUV     =  [32,0]            
  97.                self.orderUV     =  [0,0]             
  98.                self.flagUV      =  [0,0]              
  99.                self.Origine     =  [0.0,0.0]
  100.                self.beziers_knot = []
  101.  
  102. class COURBE:
  103.       def __init__(self):
  104.               self.magic_number='3DG3'              
  105.               self.type            =  objBEZIER        
  106.               self.number_of_items =  0              
  107.               self.ext1_ext2       =  [0,0]             
  108.               self.matrix          =  """0.0 0.0 1.0 0.0
  109. 0.0 1.0 0.0 0.0
  110. 0.0 0.0 1.0 0.0
  111. 0.0 0.0 0.0 1.0 """ #- right-handed object matrix. Used to determine position, rotation and size
  112.               self.ITEM            = {}
  113.  
  114. courbes=COURBE()
  115. PATTERN={}
  116. BOUNDINGBOX={'rec':[],'coef':1.0}
  117. npat=0
  118. #=====================================================================
  119. #======== name of the curve in the courbes dictionnary ===============
  120. #=====================================================================
  121. n0=0
  122.  
  123. #=====================================================================
  124. #====================== current Point ================================
  125. #=====================================================================
  126. CP=[0.0,0.0] #currentPoint
  127.  
  128. def MINMAX(b):
  129.    global BOUNDINGBOX
  130.    r=BOUNDINGBOX['rec']
  131.    for m in range(0,len(b)-2,2):
  132.         #print m, m+1  , len(b)-1
  133.         #print b[m], r, r[0]
  134.         if float(b[m])<r[0]: 
  135.            r[0]=float(b[m])
  136.  
  137.         if float(b[m])>r[2]: r[2]=float(b[m])
  138.  
  139.         if float(b[m+1])<r[1]: r[1]=float(b[m+1])
  140.         if float(b[m+1])>r[3]: r[3]=float(b[m+1])
  141.  
  142. #=====================================================================
  143. #===== to compare last position to the original move to displacement =
  144. #=====  needed for cyclic efinition  =================================
  145. #=====================================================================
  146. def test_egalitedespositions(f1,f2):
  147.     if f1[0]==f2[0] and f1[1]==f2[1]:
  148.        return Blender.TRUE
  149.     else:
  150.        return Blender.FALSE
  151.  
  152.  
  153. def Open_GEOfile(dir,nom):
  154.     if BLversion>=233:
  155.        Blender.Load(dir+nom+'OOO.obj', 1)
  156.        BO=Blender.Object.Get()
  157.  
  158.        BO[-1].LocZ=1.0
  159.        
  160.        BO[-1].makeDisplayList() 
  161.        Blender.Window.RedrawAll()
  162.     else:
  163.        print "Not yet implemented"
  164.  
  165. def create_GEOtext(courbes):
  166.     global SCALE, B, BOUNDINGBOX
  167.     r=BOUNDINGBOX['rec']
  168.     if SCALE==1:
  169.        SCALE=1.0
  170.     elif SCALE==2:
  171.        SCALE=r[2]-r[0]
  172.     elif SCALE==3:
  173.        SCALE=r[3]-r[1]
  174.  
  175.     t=[]
  176.     t.append(courbes.magic_number+'\n')
  177.     t.append(str(courbes.type)+'\n')
  178.     t.append(str(courbes.number_of_items)+'\n')
  179.     t.append(str(courbes.ext1_ext2[0])+' '+str(courbes.ext1_ext2[1])+'\n')
  180.     t.append(courbes.matrix+'\n')
  181.     
  182.     for k in courbes.ITEM.keys():
  183.  
  184.         t.append("%s\n"%courbes.ITEM[k].type)
  185.  
  186.         t.append("%s %s \n"%(courbes.ITEM[k].pntsUV[0],courbes.ITEM[k].pntsUV[1]))
  187.         t.append("%s %s \n"%(courbes.ITEM[k].resolUV[0],courbes.ITEM[k].resolUV[1]))
  188.         t.append("%s %s \n"%(courbes.ITEM[k].orderUV[0],courbes.ITEM[k].orderUV[1]))
  189.         t.append("%s %s \n"%(courbes.ITEM[k].flagUV[0],courbes.ITEM[k].flagUV[1]))
  190.  
  191.         flag =0#courbes.ITEM[k].flagUV[0]
  192.  
  193.         for k2 in range(flag,len(courbes.ITEM[k].beziers_knot)):
  194.            k1 =courbes.ITEM[k].beziers_knot[k2]
  195.            t.append("%4f 0.0 %4f \n"%(float(k1.co[0])/SCALE,float(k1.co[1])/SCALE))
  196.            t.append("%4f 0.0 %4f \n"%(float(k1.co[4])/SCALE,float(k1.co[5])/SCALE))
  197.            t.append("%4f 0.0 %4f \n"%(float(k1.co[2])/SCALE,float(k1.co[3])/SCALE))
  198.            t.append(str(k1.ha[0])+' '+str(k1.ha[1])+'\n')
  199.     return t
  200.  
  201. def save_GEOfile(dir,nom,t):
  202.      f=open(dir+nom+'OOO.obj','w')
  203.      f.writelines(t)
  204.      f.close()
  205.      #warning = "REMINDER : %t | Do not forget to rename your blender file NOW ! %x1"
  206.      #result = Blender.Draw.PupMenu(warning)
  207.  
  208.  
  209. #=====================================================================
  210. #=====      GIMP format   :  DEBUT           =========================
  211. #=====================================================================
  212. CLOSED=0
  213.  
  214. def mouvement_vers(l,l1,l2,n0):
  215.     global BOUNDINGBOX, CP
  216.     if l[1] == '3' :
  217.          n0+=1
  218.          courbes.ITEM[n0]=ITEM() 
  219.          courbes.ITEM[n0].Origine=[l[-3],l[-1],] 
  220.          courbes.ITEM[n0-1].beziers_knot[0].co[0]=CP[0]
  221.          courbes.ITEM[n0-1].beziers_knot[0].co[1]=CP[1]
  222.          CP=[l2[-3],  l2[-1]]
  223.  
  224.     elif l[1]=='1' and (n0 not in courbes.ITEM.keys()): 
  225.        courbes.ITEM[n0]=ITEM() 
  226.        courbes.ITEM[n0].Origine=[l[-3],l[-1],] 
  227.        CP=[l2[-3],  l2[-1]]
  228.     
  229.     B=Bez()
  230.     B.co=[ CP[0],CP[1], 
  231.            l1[-3],  l1[-1], 
  232.            l[-3],   l[-1]]
  233.  
  234.     CP=[l2[-3],  l2[-1]]
  235.  
  236.     if BOUNDINGBOX['rec']==[]:
  237.        BOUNDINGBOX['rec']=[float(l2[-3]),  float(l2[-1]), float(l[-3]), float(l[-1])]
  238.     B.ha=[0,0]    
  239.  
  240.     """
  241.     if len( courbes.ITEM[n0].beziers_knot)>=1:     
  242.        courbes.ITEM[n0].beziers_knot[-1].co[2]=l1[-3]
  243.        courbes.ITEM[n0].beziers_knot[-1].co[3]=l1[-1]
  244.     """
  245.  
  246.     MINMAX(B.co)    
  247.     courbes.ITEM[n0].beziers_knot.append(B)  
  248.     return  courbes,n0      
  249.      
  250. Actions=   { "1" : mouvement_vers,
  251.              "3" : mouvement_vers    }
  252.      
  253. TAGcourbe=Actions.keys()
  254.              
  255. def scan_FILE(nom):
  256.   global CP, courbes, SCALE, MAX, MIN, CLOSED
  257.   dir,name=split(nom)
  258.   name=name.split('.')
  259.   #print name
  260.   n0=0
  261.   result=0 
  262.   t=filtreFICHIER(nom)
  263.   if t!="false":
  264.      if not SHARP_IMPORT:
  265.          warning = "Select Size : %t| As is %x1 | Scale on Height %x2| Scale on Width %x3" 
  266.          SCALE = Blender.Draw.PupMenu(warning)
  267.      npat=0
  268.      l=0
  269.      while l <len(t)-1 :
  270.        #print 'len(t)',len(t)        
  271.        t[l].replace('\n','') 
  272.        if t[l][0]!='%':
  273.             l0=t[l].split()               
  274.             #print l0[0], l0[1]
  275.             if l0[0]=='TYPE:' and l0[1] in TAGcourbe:
  276.                    #print l0[0], l0[1],
  277.                    l1=t[l+1].split()                
  278.                    l2=t[l+2].split()                
  279.                    courbes,n0=Actions[l0[1]](l0,l1,l2,n0)
  280.             elif l0[0]=='#Point':
  281.                 POINTS= int(l0[0])
  282.             elif l0[0]=='CLOSED:' and l0[1]=='1':
  283.                 CLOSED=1
  284.        l=l+1;                     
  285.      
  286.      courbes.number_of_items=len(courbes.ITEM.keys())
  287.  
  288.      courbes.ITEM[n0].beziers_knot[0].co[0]=CP[0]
  289.      courbes.ITEM[n0].beziers_knot[0].co[1]=CP[1]
  290.  
  291.      for k in courbes.ITEM.keys():
  292.         #print k  
  293.         if CLOSED == 1:
  294.            B=Bez()
  295.            B.co=courbes.ITEM[k].beziers_knot[0].co[:]
  296.            B.ha=courbes.ITEM[k].beziers_knot[0].ha[:]
  297.            B.echo()
  298.            courbes.ITEM[k].beziers_knot.append(B)  
  299.            courbes.ITEM[k].flagUV[0]=1
  300.            courbes.ITEM[k].pntsUV[0] =len(courbes.ITEM[k].beziers_knot)
  301.  
  302.      if courbes.number_of_items>0:
  303.        t=create_GEOtext(courbes)
  304.        save_GEOfile(dir,name[0],t)
  305.        Open_GEOfile(dir,name[0])
  306.      else:
  307.         pass
  308.  
  309. #=====================================================================
  310. #====================== GIMP Path format mouvements =========================
  311. #=====================================================================
  312. #=========================================================          
  313. # une sorte de contournement qui permet d'utiliser la fonction
  314. # et de documenter les variables Window.FileSelector
  315. #=========================================================
  316. def fonctionSELECT(nom):
  317.     scan_FILE(nom)
  318.  
  319. #Blender.Window.FileSelector (fonctionSELECT, 'SELECT a GIMP Path FILE')
  320. #sys.path=oldpath
  321.