Advertisement
tarirura

Untitled

Jul 17th, 2017
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. #!BPY
  2.  
  3. """
  4. Name: 'Apply Deformation'
  5. Blender: 242
  6. Group: 'Object'
  7. Tooltip: 'Make copys of all the selected objects with modifiers, softbodies and fluid baked into a mesh'
  8. """
  9.  
  10. __author__ = "Martin Poirier (theeth), Jean-Michel Soler (jms), Campbell Barton (ideasman)"
  11. # This script is the result of merging the functionalities of two other:
  12. # Martin Poirier's Apply_Def.py and
  13. # Jean-Michel Soler's Fix From Everything
  14.  
  15. __url__ = ("http://www.blender.org", "http://blenderartists.org", "http://jmsoler.free.fr")
  16. __version__ = "1.6 07/07/2006"
  17.  
  18. __bpydoc__ = """\
  19. This script creates "raw" copies of deformed meshes.
  20.  
  21. Usage:
  22.  
  23. Select any number of Objects and run this script.  A fixed copy of each selected object
  24. will be created, with the word "_def" appended to its name. If an object with
  25. the same name already exists, it appends a number at the end as Blender itself does.
  26.  
  27. Objects in Blender can be deformed by armatures, lattices, curve objects and subdivision,
  28. but this will only change its appearance on screen and rendered
  29. images -- the actual mesh data is still simpler, with vertices in an original
  30. "rest" position and less vertices than the subdivided version.
  31.  
  32. Use this script if you want a "real" version of the deformed mesh, so you can
  33. directly manipulate or export its data.
  34.  
  35. This script will work with object types: Mesh, Metaballs, Text3d, Curves and Nurbs Surface.
  36. """
  37.  
  38.  
  39. # $Id: object_apply_def.py 14769 2008-05-09 17:13:03Z campbellbarton $
  40. #
  41. # --------------------------------------------------------------------------
  42. # ***** BEGIN GPL LICENSE BLOCK *****
  43. #
  44. # Copyright (C) 2003: Martin Poirier, theeth@yahoo.com
  45. #
  46. # Thanks to Jonathan Hudson for help with the vertex groups part
  47. #
  48. # This program is free software; you can redistribute it and/or
  49. # modify it under the terms of the GNU General Public License
  50. # as published by the Free Software Foundation; either version 2
  51. # of the License, or (at your option) any later version.
  52. #
  53. # This program is distributed in the hope that it will be useful,
  54. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  55. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  56. # GNU General Public License for more details.
  57. #
  58. # You should have received a copy of the GNU General Public License
  59. # along with this program; if not, write to the Free Software Foundation,
  60. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  61. #
  62. # ***** END GPL LICENCE BLOCK *****
  63.  
  64.  
  65. import Blender
  66. import bpy
  67. import BPyMesh
  68.  
  69. def copy_vgroups(source_ob, target_ob):
  70.    
  71.     source_me = source_ob.getData(mesh=1)
  72.    
  73.     vgroups= source_me.getVertGroupNames()
  74.     if vgroups:
  75.         ADD= Blender.Mesh.AssignModes.ADD
  76.         target_me = target_ob.getData(mesh=1)
  77.         for vgroupname in vgroups:
  78.             target_me.addVertGroup(vgroupname)
  79.             if len(target_me.verts) == len(source_me.verts):
  80.                 try: # in rare cases this can raise an 'no deform groups assigned to mesh' error
  81.                     vlist = source_me.getVertsFromGroup(vgroupname, True)
  82.                 except:
  83.                     vlist = []
  84.                
  85.                 try:
  86.                     for vpair in vlist:
  87.                         target_me.assignVertsToGroup(vgroupname, [vpair[0]], vpair[1], ADD)
  88.                 except:
  89.                     pass
  90.  
  91.  
  92. def apply_deform():
  93.     scn= bpy.data.scenes.active
  94.     #Blender.Window.EditMode(0)
  95.    
  96.     NAME_LENGTH = 19
  97.     SUFFIX = "_def"
  98.     SUFFIX_LENGTH = len(SUFFIX)
  99.     # Get all object and mesh names
  100.    
  101.  
  102.     ob_list = list(scn.objects.context)
  103.     ob_act = scn.objects.active
  104.    
  105.     # Assume no soft body
  106.     has_sb= False
  107.    
  108.     # reverse loop so we can remove objects (metaballs in this case)
  109.     for ob_idx in xrange(len(ob_list)-1, -1, -1):
  110.         ob= ob_list[ob_idx]
  111.        
  112.         ob.sel = 0 # deselect while where checking the metaballs
  113.        
  114.         # Test for a softbody
  115.         if not has_sb and ob.isSB():
  116.             has_sb= True
  117.        
  118.         # Remove all numbered metaballs because their disp list is only on the main metaball (un numbered)
  119.         if ob.type == 'MBall':
  120.             name= ob.name
  121.             # is this metaball numbered?
  122.             dot_idx= name.rfind('.') + 1
  123.             if name[dot_idx:].isdigit():
  124.                 # Not the motherball, ignore it.
  125.                 del ob_list[ob_idx]
  126.            
  127.    
  128.     if not ob_list:
  129.         Blender.Draw.PupMenu('No objects selected, nothing to do.')
  130.         return
  131.    
  132.    
  133.     if has_sb:
  134.         curframe=Blender.Get('curframe')
  135.         for f in xrange(curframe):
  136.             Blender.Set('curframe',f+1)
  137.             Blender.Window.RedrawAll()
  138.  
  139.     used_names = [ob.name for ob in Blender.Object.Get()]
  140.     used_names.extend(Blender.NMesh.GetNames())
  141.    
  142.    
  143.     deformedList = []
  144.     for ob in ob_list:
  145.        
  146.         # Get the mesh data
  147.         new_me= BPyMesh.getMeshFromObject(ob, vgroups=False)
  148.        
  149.         if not new_me:
  150.             continue # Object has no display list
  151.        
  152.        
  153.         name = ob.name
  154.         new_name = "%s_def" % name[:NAME_LENGTH-SUFFIX_LENGTH]
  155.         num = 0
  156.        
  157.         while new_name in used_names:
  158.             new_name = "%s_def.%.3i" % (name[:NAME_LENGTH-(SUFFIX_LENGTH+SUFFIX_LENGTH)], num)
  159.             num += 1
  160.         used_names.append(new_name)
  161.        
  162.         new_me.name= new_name
  163.        
  164.         new_ob= scn.objects.new(new_me)
  165.         new_ob.setMatrix(ob.matrixWorld)
  166.        
  167.         # Make the active duplicate also active
  168.         if ob == ob_act:
  169.             scn.objects.active = new_ob
  170.        
  171.         # Original object was a mesh? see if we can copy any vert groups.
  172.         if ob.type =='Mesh':
  173.             copy_vgroups(ob, new_ob)
  174.    
  175.     Blender.Window.RedrawAll()
  176.  
  177. if __name__=='__main__':
  178.     apply_deform()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement