Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!BPY
- # """
- # Name: 'Re-align Armature'
- # Blender: 246
- # Group: 'Mesh'
- # Tooltip: '(Re)Attach an armature to a mesh and ensure that they have the same object centres.'
- # """
- import Blender
- from Blender import Window, Draw, Scene, Object, Mesh
- __author__ = 'Martin Ellison'
- __version__ = '1.0 2008/07/30'
- __url__ = []
- __email__ = ["Martin Ellison, m.e:acm*org", "scripts"]
- __bpydoc__ = """\
- From the popup, select one armature and one mesh.
- """
- # ***** BEGIN GPL LICENSE BLOCK *****
- #
- # Script copyright (C) Martin Ellison
- #
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software Foundation,
- # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- #
- # ***** END GPL LICENCE BLOCK *****
- # --------------------------------------------------------------------------
- import Blender
- from Blender import Window, Scene, Geometry, Mathutils, Object, Armature, Mesh, Draw
- from Blender.Mathutils import Vector
- def alignArmature(armatureObject, meshObject):
- armatureLoc = Vector(armatureObject.loc)
- armature = armatureObject.getData()
- meshLoc = Vector(meshObject.loc)
- diff = armatureLoc - meshLoc
- armature.makeEditable()
- for bone in armature.bones.values():
- bone.head -= diff
- bone.tail -= diff
- armature.update()
- armatureObject.setLocation(meshObject.loc)
- def main():
- print '-----'
- scn = Scene.GetCurrent()
- sel_object = scn.objects.active
- popup = []
- armatures = []
- meshes = []
- for obj in scn.getChildren():
- if obj.type == 'Armature':
- tog = Blender.Draw.Create(0)
- popup.append((obj.name + ' (arm)', tog))
- armatures.append((tog, obj.name))
- if obj.type == 'Mesh':
- tog = Blender.Draw.Create(0)
- popup.append((obj.name + ' (mesh)', tog))
- meshes.append((tog, obj.name))
- if not Blender.Draw.PupBlock("Attach mesh to armature? ", popup): return
- armatureName = ''
- for tog, name in armatures:
- if tog.val:
- if armatureName != '': Draw.PupMenu("Duplicate armature")
- armatureName = name
- meshName = ''
- for tog, name in meshes:
- if tog.val:
- if meshName != '': Draw.PupMenu("Duplicate mesh")
- meshName = name
- mesh_object = Object.Get(meshName)
- armature_object = Object.Get(armatureName)
- if mesh_object.getType()!='Mesh':
- result = Draw.PupMenu("You need a mesh")
- return
- the_mesh = mesh_object.getData(mesh=1)
- print 'the mesh is at ', mesh_object.loc
- if armature_object.getType()!='Armature':
- result = Draw.PupMenu("You need an armature")
- return
- the_armature = armature_object.getData()
- print 'the armature is at ', armature_object.loc
- editmode = Window.EditMode()
- if editmode: Window.EditMode(0)
- Window.WaitCursor(1)
- if mesh_object.parent != None: mesh_object.clrParent()
- alignArmature(armature_object, mesh_object)
- armature_object.makeParentDeform([mesh_object])
- if editmode: Window.EditMode(1)
- Window.WaitCursor(0)
- Window.RedrawAll()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement