Merge branch 'cb/fixs'
[plumiferos.git] / release / scripts / object_apply_def.py
blobbab378651847eb2692c74be433e4108a8bd0d0fa
1 #!BPY
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 """
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
15 __url__ = ("http://www.blender.org", "http://blenderartists.org", "http://members.iinet.net.au/~cpbarton/ideasman/", "http://jmsoler.free.fr")
16 __version__ = "1.6 07/07/2006"
18 __bpydoc__ = """\
19 This script creates "raw" copies of deformed meshes.
21 Usage:
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.
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.
32 Use this script if you want a "real" version of the deformed mesh, so you can
33 directly manipulate or export its data.
35 This script will work with object types: Mesh, Metaballs, Text3d, Curves and Nurbs Surface.
36 """
39 # $Id: object_apply_def.py 10546 2007-04-18 14:40:01Z campbellbarton $
41 # --------------------------------------------------------------------------
42 # ***** BEGIN GPL LICENSE BLOCK *****
44 # Copyright (C) 2003: Martin Poirier, theeth@yahoo.com
46 # Thanks to Jonathan Hudson for help with the vertex groups part
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.
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.
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.
62 # ***** END GPL LICENCE BLOCK *****
65 import Blender
66 import bpy
67 import BPyMesh
69 def copy_vgroups(source_ob, target_ob):
71 source_me = source_ob.getData(mesh=1)
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 vlist = source_me.getVertsFromGroup(vgroupname, True)
81 try:
82 for vpair in vlist:
83 target_me.assignVertsToGroup(vgroupname, [vpair[0]], vpair[1], ADD)
84 except:
85 pass
88 def apply_deform():
89 scn= bpy.data.scenes.active
90 #Blender.Window.EditMode(0)
92 NAME_LENGTH = 19
93 SUFFIX = "_def"
94 SUFFIX_LENGTH = len(SUFFIX)
95 # Get all object and mesh names
98 ob_list = list(scn.objects.context)
99 ob_act = scn.objects.active
101 # Assume no soft body
102 has_sb= False
104 # reverse loop so we can remove objects (metaballs in this case)
105 for ob_idx in xrange(len(ob_list)-1, -1, -1):
106 ob= ob_list[ob_idx]
108 ob.sel = 0 # deselect while where checking the metaballs
110 # Test for a softbody
111 if not has_sb and ob.isSB():
112 has_sb= True
114 # Remove all numbered metaballs because their disp list is only on the main metaball (un numbered)
115 if ob.type == 'MBall':
116 name= ob.name
117 # is this metaball numbered?
118 dot_idx= name.rfind('.') + 1
119 if name[dot_idx:].isdigit():
120 # Not the motherball, ignore it.
121 del ob_list[ob_idx]
124 if not ob_list:
125 Blender.Draw.PupMenu('No objects selected, nothing to do.')
126 return
129 if has_sb:
130 curframe=Blender.Get('curframe')
131 for f in xrange(curframe):
132 Blender.Set('curframe',f+1)
133 Blender.Window.RedrawAll()
135 used_names = [ob.name for ob in Blender.Object.Get()]
136 used_names.extend(Blender.NMesh.GetNames())
139 deformedList = []
140 for ob in ob_list:
142 # Get the mesh data
143 new_me= BPyMesh.getMeshFromObject(ob, vgroups=False)
145 if not new_me:
146 continue # Object has no display list
149 name = ob.name
150 new_name = "%s_def" % name[:NAME_LENGTH-SUFFIX_LENGTH]
151 num = 0
153 while new_name in used_names:
154 new_name = "%s_def.%.3i" % (name[:NAME_LENGTH-(SUFFIX_LENGTH+SUFFIX_LENGTH)], num)
155 num += 1
156 used_names.append(new_name)
158 new_me.name= new_name
160 new_ob= scn.objects.new(new_me)
161 new_ob.setMatrix(ob.matrixWorld)
163 # Make the active duplicate also active
164 if ob == ob_act:
165 scn.objects.active = new_ob
167 # Original object was a mesh? see if we can copy any vert groups.
168 if ob.type =='Mesh':
169 copy_vgroups(ob, new_ob)
171 Blender.Window.RedrawAll()
173 if __name__=='__main__':
174 apply_deform()