home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 November / WNnov2005.iso / Windows / Equipement / Blender / blender-2.37a-windows.exe / $_5_ / .blender / scripts / UVpaint05.py < prev    next >
Text File  |  2004-11-07  |  13KB  |  496 lines

  1. #!BPY
  2.  
  3. """ Registration info for Blender menus: <- these words are ignored
  4. Name: 'UVpainter'
  5. Blender: 232
  6. Group: 'UV'
  7. Tip: 'Use vertex paint color value to fill uvmapping'
  8. """
  9.  
  10. __author__ = "Jean-Michel Soler (jms)"
  11. __url__ = ("blender", "elysiun",
  12. "Script's homepage, http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_uvpainting.htm",
  13. "Communicate problems and errors, http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender")
  14. __version__ = "0.5 05/2004"
  15.  
  16. __bpydoc__ = """\
  17. This script "paints" uv-mappings with the model's vertex colors.
  18.  
  19. Usage:
  20.  
  21. With this script you can export uv-maps filled with vertex colors to TARGA
  22. (.tga) images.  To use it the mesh must have proper uv coordinates assigned
  23. in UV Face Select Mode.  And to fill the projected faces with color, the mesh
  24. can also have material(s) and vertex colors painted on it.
  25.  
  26. The script has a GUI with a preview of the results and options like drawing
  27. lines or not, defining size, etc.  You can paint vertex colors in the mesh and
  28. see the uv-map updated in the script's window.
  29.  
  30. Notes:<br>
  31.     Material's rgb color is also used to fill the uv-map;<br>
  32.     If there are no vertex colors or texture faces in the mesh and you press
  33. the "Make" VColors button in the edit mesh buttons win, the current light setup
  34. is saved as vertex colors for the model;<br>
  35.     Check the script's homepage for example images.
  36. """
  37.  
  38. # $Id: UVpaint05.py,v 1.3 2004/11/07 16:31:13 ianwill Exp $
  39. #
  40. #----------------------------------------------
  41. # uvpainter script (c) 04/2004  jean-michel soler
  42. # http://jmsoler.free.fr/util/blenderfile/py/UVpaint05.zip
  43. # this script is released under GPL licence
  44. # for the Blender 2.33 scripts distribution
  45. #----------------------------------------------
  46. # Official page :
  47. # http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_uvpainting.htm
  48. # Communicate problems and errors on:
  49. # http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender 
  50. #----------------------------------------------
  51. # Page officielle :
  52. #   http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_uvpainting.htm
  53. # Communiquer les problemes et erreurs sur:
  54. #   http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender
  55. #--------------------------------------------- 
  56. # ce script est proposΘ sous licence GPL pour etre associe
  57. # a la distribution de Blender 2.33 et suivant
  58. # --------------------------------------------------------------------------
  59. # this script is released under GPL licence
  60. # for the Blender 2.33 scripts package
  61. # --------------------------------------------------------------------------
  62. # ***** BEGIN GPL LICENSE BLOCK *****
  63. #
  64. # Script copyright (C) 2003, 2004: Jean-Michel Soler 
  65. #
  66. # This program is free software; you can redistribute it and/or
  67. # modify it under the terms of the GNU General Public License
  68. # as published by the Free Software Foundation; either version 2
  69. # of the License, or (at your option) any later version.
  70. #
  71. # This program is distributed in the hope that it will be useful,
  72. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  73. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  74. # GNU General Public License for more details.
  75. #
  76. # You should have received a copy of the GNU General Public License
  77. # along with this program; if not, write to the Free Software Foundation,
  78. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  79. #
  80. # ***** END GPL LICENCE BLOCK *****
  81. # --------------------------------------------------------------------------
  82.  
  83. import Blender
  84. from Blender.Draw import *
  85. from Blender.BGL import *
  86. from Blender.NMesh import *
  87.  
  88. try:
  89.   import nt
  90.   os=nt
  91. except:
  92.    import posix
  93.    os=posix
  94.  
  95. def exist(path):
  96.        try:
  97.         pth=os.stat(Blender.sys.dirname(path))  
  98.        except:
  99.         return 0
  100.        return 1 
  101.  
  102. loc0= Blender.sys.dirname(Blender.Get ("filename")) 
  103. loc2=loc0+Blender.sys.dirsep+'test00.tga' 
  104.  
  105. glCr=glRasterPos2d
  106. glCl3=glColor3f
  107. glCl4=glColor4f
  108. glRct=glRectf
  109.  
  110. xlimit=0
  111. selmatlist=[]
  112.  
  113. def triangle(a,b,c):
  114.   glBegin(GL_TRIANGLES);
  115.   glColor3f(a[2],a[3],a[4])
  116.   glVertex2f(a[0],a[1]);
  117.   glColor3f(b[2],b[3],b[4])
  118.   glVertex2f(b[0],b[1]);
  119.   glColor3f(c[2],c[3],c[4])
  120.   glVertex2f(c[0],c[1]);
  121.   glEnd();
  122.  
  123. def Ltriangle(a,b,c):
  124.   glBegin(GL_LINES);
  125.   glColor3f(1.0,1.0,1.0)
  126.   glVertex2f(a[0],a[1]);
  127.   glVertex2f(b[0],b[1]);
  128.   glVertex2f(c[0],c[1]);
  129.   glEnd();
  130.  
  131. def carre(a,b,c,d):
  132.     triangle(a,b,c)
  133.     triangle(a,c,d)
  134.  
  135. def Lcarre(a,b,c,d):
  136.   glBegin(GL_LINES);
  137.   glColor3f(1.0,1.0,1.0)
  138.   glVertex2f(a[0],a[1]);
  139.   glVertex2f(b[0],b[1]);
  140.   glVertex2f(c[0],c[1]);
  141.   glVertex2f(d[0],d[1]);
  142.   glEnd();
  143.  
  144.  
  145.    
  146. def transface(f,x,y):
  147.     global xlimit
  148.  
  149.     
  150.  
  151.     a=[0,0,0.0, 0.0,0.0,0.0]
  152.     b=[0,0,0.0, 0.0,0.0,0.0]
  153.     c=[0,0,0.0, 0.0,0.0,0.0]
  154.     d=[0,0,0.0, 0.0,0.0,0.0]
  155.  
  156.     if len(f.v)>=3:
  157.         a[0]=int(f.uv[0][0]*x)
  158.         a[1]=int(f.uv[0][1]*y)
  159.  
  160.         if a[0]>xlimit:
  161.                xlimit=a[0]
  162.  
  163.         a[2]=f.col[0].r/255.0
  164.         a[3]=f.col[0].g/255.0
  165.         a[4]=f.col[0].b/255.0
  166.  
  167.         c[0]=int(f.uv[2][0]*x)
  168.         c[1]=int(f.uv[2][1]*y)
  169.  
  170.         if c[0]>xlimit:
  171.                xlimit=c[0]
  172.  
  173.         c[2]=f.col[2].r/255.0
  174.         c[3]=f.col[2].g/255.0
  175.         c[4]=f.col[2].b/255.0
  176.  
  177.      
  178.         b[0]=int(f.uv[1][0]*x)
  179.         b[1]=int(f.uv[1][1]*y)
  180.  
  181.         if b[0]>xlimit:
  182.                xlimit=b[0]
  183.  
  184.         b[2]=f.col[1].r/255.0
  185.         b[3]=f.col[1].g/255.0
  186.         b[4]=f.col[1].b/255.0
  187.  
  188.  
  189.     if  len(f.v)==4:     
  190.         d[0]=int(f.uv[3][0]*x)
  191.         d[1]=int(f.uv[3][1]*y)
  192.  
  193.         if d[0]>xlimit:
  194.                xlimit=d[0]
  195.  
  196.         d[2]=f.col[3].r/255.0
  197.         d[3]=f.col[3].g/255.0
  198.         d[4]=f.col[3].b/255.0
  199.     else:
  200.         d=0
  201.  
  202.  
  203.     #print a,b,c
  204.     return a,b,c,d
  205.  
  206.  
  207. def extract_faces(me,MENU):
  208.     global  TMATList, selmatlist 
  209.     if MENU==2:
  210.        listf=[]
  211.        for f in me.faces:
  212.           if f.mat in selmatlist:
  213.               listf.append(f)
  214.        return listf
  215.  
  216. def affiche_mesh(ME,x,y):
  217.     global LINE,xlimit,MMENU,XLIMIT,xwin,xlimit
  218.  
  219.     if ME.getType()=='Mesh':
  220.        me=GetRaw(ME.getData().name)
  221.        
  222.        if MMENU.val==1:
  223.           se=me.faces
  224.  
  225.        elif MMENU.val==3:
  226.           se=me.getSelectedFaces()
  227.  
  228.        elif MMENU.val==2:
  229.           se=extract_faces(me,2) 
  230.  
  231.        xlimit=0
  232.        for f in se:
  233.          a,b,c,d=transface(f,x,y)
  234.          if len(f.v)==4:
  235.             triangle(a,b,c)
  236.             triangle(a,c,d)
  237.          elif len(f.v)==3:
  238.             triangle(a,b,c)
  239.  
  240.        if LINE.val==1:
  241.          for f in se:
  242.            a,b,c,d=transface(f,x,y)
  243.            if len(f.v)==4:
  244.               Lcarre(a,b,c,d)
  245.            elif len(f.v)==3:
  246.               Ltriangle(a,b,c)
  247.  
  248.          if XLIMIT.val==0:
  249.             Lcarre([1,1],[1,y-2],[xlimit+2,y-2],[xlimit+2,1]) 
  250.          else:
  251.             Lcarre([1,1],[1,y-2],[xwin-2,y-2],[xwin-2,1]) 
  252.  
  253.  
  254.  
  255. def write_tgafile(loc2,bitmap,width,height,profondeur): 
  256.  
  257.                   f=open(loc2,'wb') 
  258.                   Origine_en_haut_a_gauche=32 
  259.                   Origine_en_bas_a_gauche=0 
  260.                   Data_Type_2=2 
  261.                   RVB=profondeur*8 
  262.                   RVBA=32 
  263.                   entete0=[] 
  264.                   for t in range(18): 
  265.                     entete0.append(chr(0)) 
  266.  
  267.                   entete0[2]=chr(Data_Type_2) 
  268.                   entete0[13]=chr(width/256) 
  269.                   entete0[12]=chr(width % 256) 
  270.                   entete0[15]=chr(height/256) 
  271.                   entete0[14]=chr(height % 256) 
  272.                   entete0[16]=chr(RVB) 
  273.                   entete0[17]=chr(Origine_en_bas_a_gauche) 
  274.  
  275.                   #Origine_en_haut_a_gauche 
  276.  
  277.                   for t in entete0: 
  278.                     f.write(t) 
  279.  
  280.                   for t in bitmap:
  281.  
  282.                     for c in [2,1,0,3]:
  283.                         #print t[c]%256      
  284.                         f.write(chr(t[c]*2)) 
  285.                   f.close() 
  286.  
  287.  
  288. def save(x0,y0,dx,dy):
  289.     im = Buffer(GL_BYTE,[dx*(dy+1),4])
  290.     glReadPixels(x0,y0,dx,dy,GL_RGBA, GL_BYTE,im); 
  291.     print len(im), dx*dy, dx, dy, len(im)/dy    
  292.     write_tgafile(loc2,im,dx,dy+1,4)
  293.  
  294. def DOCMat_list(TMATList,ME):
  295.     me=Blender.NMesh.GetRaw(ME.getData().name) 
  296.     if len(me.materials)!=0: 
  297.                 n=0 
  298.                 for mat in me.materials:
  299.                     TMATList[1][n][0]=mat.R
  300.                     TMATList[1][n][1]=mat.G
  301.                     TMATList[1][n][2]=mat.B
  302.                     n+=1
  303.                 TMATList[0]=n
  304.     else:
  305.               TMATList[0]=0
  306.     return TMATList
  307.  
  308. def SELMat_list():
  309.       global TMATList,selmatlist
  310.       Me=Blender.Object.GetSelected()
  311.       if Me!=[]:
  312.          if Me[0].getType()=='Mesh':   
  313.             TMATList=DOCMat_list(TMATList,Me[0])
  314.             selmatlist=[]
  315.             for TMat in TMATList[2]:
  316.                if TMat.val==1.0:
  317.                   selmatlist.append(TMATList[2].index(TMat))  
  318.             ERROR=0
  319.          else:
  320.             ERROR=1
  321.             TextERROR='Selected Object is not a mesh.'  
  322.       else:
  323.           ERROR=1
  324.           TextERROR='No Selected Object.'  
  325.       
  326. def DOCBONEMENU(TBONEMENU):
  327.     pass
  328.  
  329. # ----------
  330. # uvpaint1
  331. # ----------
  332. NSIZE=Create(1.0)
  333. # ----------
  334. # uvpaint2
  335. # ----------
  336. LINE=Create(0)
  337. # ----------
  338. # uvpaint3
  339. # ----------
  340. TEXT=Create(loc2)
  341. # ----------
  342. # uvpaint4
  343. # ----------
  344. TMENU="MODE MENU %t|All %x1|Material %x2|Selected %x3"
  345.  
  346. # coming soon : "|Bone %x4", perhaps in uvpainter v0.5
  347.  
  348. MMENU=Create(3)
  349. TDOCMat = Create(0)
  350. # ----------
  351. TMATList= [0,[],[]] 
  352. for t in range(16):
  353.     TMATList[1].append([0.0,0.0,0.0])
  354.     TMATList[2].append(Create(0))
  355. # ----------
  356. TDOCMat = Create(1)
  357. # ----------
  358. TBONEMENU= Create(1) 
  359. # ----------
  360.  
  361. XLIMIT=Create(0)
  362.  
  363. y=0
  364. x=0
  365. x0=0
  366. y0=0
  367. xwin=0
  368.  
  369. n0=32
  370.  
  371. def draw():
  372.     global NSIZE,LINE,x0,y0,y,x,TEXT,MMENU,TDOCMat
  373.     global XLIMIT,selmatlist,xwin
  374.  
  375.     size=Buffer(GL_FLOAT, 4)
  376.     glGetFloatv(GL_SCISSOR_BOX, size)
  377.     size= size.list
  378.  
  379.     for s in [0,1,2,3]: size[s]=int(size[s])
  380.     
  381.     n0=32
  382.     x0=size[0]
  383.     y0=size[1]
  384.     
  385.     x=size[2]
  386.     y=size[3]
  387.    
  388.     xwin=x
  389.     ywin=y
  390.    
  391.  
  392.     glClear(GL_COLOR_BUFFER_BIT)
  393.  
  394.     glShadeModel(GL_SMOOTH)
  395.     SelecMESH=Blender.Object.GetSelected()
  396.     if SelecMESH!=[]:
  397.        if SelecMESH[0].getType()=='Mesh':
  398.             affiche_mesh(SelecMESH[0],int(y*NSIZE.val),int(y*NSIZE.val-n0-2))
  399.  
  400.     glColor3f(0.0,0.0,0.0)
  401.     glRectf(4,size[3],555,size[3]-32 )
  402.  
  403.     glColor3f(1.0,1.0,1.0)
  404.  
  405.     glRasterPos2f(8, size[3]-13)
  406.     Text("uvpainter v0.5")
  407.      
  408.     glRasterPos2f(8, size[3]-28)
  409.     Text("Jm Soler, 05/2004")
  410.  
  411.     Button("ReDraw"      ,16 ,290-118+61 ,size[3]-30  ,60 ,13)
  412.     Button("Exit"        ,1  ,250-122+63 ,size[3]-30  ,38 ,13)
  413.     Button("Save"        ,6  ,250-16+61  ,size[3]-30  ,40 ,13)
  414.  
  415.     NSIZE= Slider("Sc:",4   ,290-118+61  ,size[3]-15 , 102, 13, NSIZE.val, 0.1,1.5,0,"SIZE.")
  416.     LINE=Toggle("line",   5   ,250-122+63   ,size[3]-15 , 38, 13, LINE.val, "Draw lines")
  417.  
  418.     glRasterPos2f(250-130  ,size[3]-13,)
  419.     Text("Mode")
  420.  
  421.     MMENU= Menu(TMENU    ,2  ,250-130, size[3]-30, 63, 13, MMENU.val, "MODE menu.")
  422.  
  423.     if MMENU.val==1 or MMENU.val==3:
  424.        glRasterPos2f( 250-16+61+42+80,size[3]-13)
  425.        if XLIMIT.val:
  426.               xl=xwin
  427.        else:
  428.               xl=xlimit
  429.  
  430.        Text("x :"+"%d"%(xl+2))  
  431.  
  432.        glRasterPos2f(250-16+61+42+65*2,size[3]-13)
  433.        Text("y :"+"%d"%(y-n0+1))  
  434.  
  435.        TEXT=String("to:",   7   , 278+61  ,size[3]-28 , 213, 13, TEXT.val, 256, "Draw lines")
  436.        if XLIMIT.val==1:
  437.           limit='winlimit'
  438.        else:
  439.           limit='maxXlimit'
  440.        XLIMIT=Toggle(limit, 9   , 250-16+61+42   ,size[3]-15 , 60, 13, XLIMIT.val, "to save picture from x max uv limit, or x window max limit")
  441.  
  442.     if MMENU.val==2:
  443.        TDOCMat=Toggle("doc"     ,24,250-130+35  ,size[3]-13 , 28, 13, TDOCMat.val)   
  444.        if TDOCMat.val==1:
  445.              SELMat_list()  
  446.              for t in range(TMATList[0]):
  447.                  glCl3(TMATList[1][t][0],
  448.                        TMATList[1][t][1],
  449.                        TMATList[1][t][2]) 
  450.                  glRct((293-16+61)+t*20,
  451.                        size[3]-13,
  452.                        (293-16+61)+t*20+20,
  453.                        size[3]-30,)
  454.                  TMATList[2][t]=Toggle("%s"%t , 32+t ,(293-16+61)+t*20  ,size[3]-13  ,20 , 13,TMATList[2][t].val)
  455.  
  456.     
  457.  
  458. def event(evt, val):    
  459.     if (evt== QKEY and not val): Exit()
  460.  
  461. def bevent(evt):
  462.     global LINE,NSIZE,n0,x0,y0,y,TEXT, loc2
  463.     global TMATList, selmatlist, TDOCMat,XLIMIT
  464.     global xlimit
  465.  
  466.     if   (evt== 1):
  467.         Exit()
  468.  
  469.     elif   (evt== 16):
  470.          pass
  471.  
  472.     elif   (evt== 4):
  473.        ng=NSIZE.val
  474.  
  475.     elif   (evt== 6):
  476.        if XLIMIT.val==1:
  477.           xi=xwin
  478.        else:
  479.           xi=xlimit
  480.  
  481.        save(x0,y0,xi+2,int(y*NSIZE.val-n0))
  482.  
  483.     elif (evt== 7):
  484.        if exist(TEXT.val):
  485.             loc2=TEXT.val
  486.        else:
  487.             TEXT.val=loc2
  488.  
  489.     elif (evt== 24) or (evt in [32,33,34,35,36,37,38,39,40,41,42,43,44]):
  490.        SELMat_list() 
  491.  
  492.  
  493.     Blender.Redraw()
  494.  
  495. Register(draw, event, bevent)
  496.