home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 November / WNnov2005.iso / Windows / Equipement / Blender / blender-2.37a-windows.exe / $_5_ / .blender / scripts / clean_mesh.py < prev    next >
Text File  |  2004-12-05  |  6KB  |  214 lines

  1. #!BPY
  2.  
  3. """
  4. Name: 'Clean Mesh'
  5. Blender: 234
  6. Group: 'Mesh'
  7. Tooltip: 'Clean unused data from all selected meshes'
  8. """
  9.  
  10. __author__ = "Campbell Barton"
  11. __url__ = ("blender", "elysiun")
  12. __version__ = "1.1 04/25/04"
  13.  
  14. __bpydoc__ = """\
  15. This script cleans specific data from all selected meshes.
  16.  
  17. Usage:
  18.  
  19. Select the meshes to be cleaned and run this script.  A pop-up will ask
  20. you what you want to remove:
  21.  
  22. - Free standing vertices;<br>
  23. - Edges that are not part of any face;<br>
  24. - Edges below a threshold length;<br>
  25. - Faces below a threshold area;<br>
  26. - All of the above.
  27.  
  28. After choosing one of the above alternatives, if your choice requires a
  29. threshold value you'll be prompted with a number pop-up to set it.
  30. """
  31.  
  32.  
  33. # $Id: clean_mesh.py,v 1.3 2004/12/01 04:49:04 ianwill Exp $
  34. #
  35. # -------------------------------------------------------------------------- 
  36. # Mesh Cleaner 1.0 By Campbell Barton (AKA Ideasman)
  37. # -------------------------------------------------------------------------- 
  38. # ***** BEGIN GPL LICENSE BLOCK ***** 
  39. # This program is free software; you can redistribute it and/or 
  40. # modify it under the terms of the GNU General Public License 
  41. # as published by the Free Software Foundation; either version 2 
  42. # of the License, or (at your option) any later version. 
  43. # This program is distributed in the hope that it will be useful, 
  44. # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  45. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  46. # GNU General Public License for more details. 
  47. # You should have received a copy of the GNU General Public License 
  48. # along with this program; if not, write to the Free Software Foundation, 
  49. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
  50. # ***** END GPL LICENCE BLOCK ***** 
  51. # -------------------------------------------------------------------------- 
  52.  
  53.  
  54. # Made by Ideasman/Campbell 2004/04/25 - ideasman@linuxmail.org
  55.  
  56. import Blender
  57. from Blender import *
  58. from math import sqrt
  59.  
  60. time1 = Blender.sys.time()
  61.  
  62. VRemNum = ERemNum = FRemNum = 0 # Remember for statistics
  63.  
  64.  
  65. #================#
  66. # Math functions #
  67. #================#
  68. def compare(f1, f2, limit):
  69.   if f1 + limit > f2 and f1 - limit < f2:
  70.     return 1
  71.   return 0
  72.  
  73. def measure(v1, v2):
  74.   return Mathutils.Vector([v1[0]-v2[0], v1[1] - v2[1], v1[2] - v2[2]]).length
  75.  
  76. def triArea2D(v1, v2, v3):
  77.   e1 = measure(v1, v2)  
  78.   e2 = measure(v2, v3)  
  79.   e3 = measure(v3, v1)  
  80.   p = e1+e2+e3
  81.   return 0.25 * sqrt(p*(p-2*e1)*(p-2*e2)*(p-2*e3))
  82.  
  83.  
  84. #=============================#
  85. # Blender functions/shortcuts #
  86. #=============================#
  87. def error(str):
  88.     Draw.PupMenu('ERROR%t|'+str)
  89.  
  90. def getLimit(text):
  91.   return Draw.PupFloatInput(text, 0.001, 0.0, 1.0, 0.1, 3)
  92.  
  93. def faceArea(f):
  94.   if len(f.v) == 4:
  95.     return triArea2D(f.v[0].co, f.v[1].co, f.v[2].co) + triArea2D(f.v[0].co, f.v[2].co, f.v[3].co)
  96.   elif len(f.v) == 3:
  97.     return triArea2D(f.v[0].co, f.v[1].co, f.v[2].co)
  98.  
  99.  
  100.  
  101. #================#
  102. # Mesh functions #
  103. #================#
  104. def delFreeVert(mesh):
  105.   global VRemNum
  106.   usedList = eval('[' + ('False,' * len(mesh.verts) )+ ']')
  107.   # Now tag verts that areused
  108.   for f in mesh.faces:
  109.     for v in f.v:
  110.       usedList[mesh.verts.index(v)] = True
  111.   vIdx = 0
  112.   for bool in usedList:
  113.     if bool == False:
  114.       mesh.verts.pop(vIdx)
  115.       vIdx -= 1
  116.       VRemNum += 1
  117.     vIdx += 1
  118.   mesh.update()
  119.  
  120.  
  121. def delEdge(mesh):
  122.   global ERemNum
  123.   fIdx = 0
  124.   while fIdx < len(mesh.faces):
  125.     if len(mesh.faces[fIdx].v) == 2:
  126.       mesh.faces.pop(fIdx)
  127.       ERemNum += 1
  128.       fIdx -= 1
  129.     fIdx +=1
  130.   mesh.update()
  131.  
  132. def delEdgeLen(mesh, limit):
  133.   global ERemNum
  134.   fIdx = 0
  135.   while fIdx < len(mesh.faces):
  136.     if len(mesh.faces[fIdx].v) == 2:
  137.       if measure(mesh.faces[fIdx].v[0].co, mesh.faces[fIdx].v[1].co) <= limit:
  138.         mesh.faces(fIdx)
  139.         ERemNum += 1
  140.         fIdx -= 1
  141.     fIdx +=1    
  142.   mesh.update()
  143.  
  144. def delFaceArea(mesh, limit):
  145.   global FRemNum
  146.   fIdx = 0
  147.   while fIdx < len(mesh.faces):
  148.     if len(mesh.faces[fIdx].v) > 2:
  149.       if faceArea(mesh.faces[fIdx]) <= limit:
  150.         mesh.faces.pop(fIdx)
  151.         FRemNum += 1
  152.         fIdx -= 1
  153.     fIdx +=1
  154.   mesh.update()
  155.  
  156.  
  157. #====================#
  158. # Make a mesh list   #
  159. #====================#
  160.  
  161. is_editmode = Window.EditMode()
  162. if is_editmode: Window.EditMode(0)
  163.  
  164. meshList = []
  165. if len(Object.GetSelected()) > 0:
  166.   for ob in Object.GetSelected():
  167.     if ob.getType() == 'Mesh':
  168.       meshList.append(ob.getData())
  169.  
  170.  
  171. #====================================#
  172. # Popup menu to select the functions #
  173. #====================================#
  174. if len(meshList) == 0:
  175.   error('no meshes in selection')
  176. else:
  177.   method = Draw.PupMenu(\
  178.   'Clean Mesh, Remove...%t|\
  179.   Verts: free standing|\
  180.   Edges: not in a face|\
  181.   Edges: below a length|\
  182.   Faces: below an area|%l|\
  183.   All of the above|')
  184.   
  185.   if method >= 3:
  186.     limit = getLimit('threshold: ')
  187.  
  188.   if method != -1:
  189.     for mesh in meshList:
  190.       if method == 1:
  191.         delFreeVert(mesh)
  192.       elif method == 2:
  193.         delEdge(mesh)
  194.       elif method == 3:
  195.         delEdgeLen(mesh, limit)
  196.       elif method == 4:
  197.         delFaceArea(mesh, limit)
  198.       elif method == 6: # All of them
  199.         delFaceArea(mesh, limit)
  200.         delEdge(mesh)
  201.         delFreeVert(mesh)
  202.       
  203.       mesh.update(0)
  204.       Redraw()
  205. print 'mesh cleanup time',Blender.sys.time() - time1
  206. if is_editmode: Window.EditMode(1)
  207.  
  208. if method != -1:
  209.   Draw.PupMenu('Removed from ' + str(len(meshList)) +' Mesh(es)%t|' + 'Verts:' + str(VRemNum) + ' Edges:' + str(ERemNum) + ' Faces:' + str(FRemNum))
  210.