Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1.  
  2. #!BPY
  3.  
  4. # """
  5. # Name: 'Re-align Armature'
  6. # Blender: 246
  7. # Group: 'Mesh'
  8. # Tooltip: '(Re)Attach an armature to a mesh and ensure that they have the same object centres.'
  9. # """
  10.  
  11. import Blender
  12. from Blender import Window, Draw, Scene, Object, Mesh
  13.  
  14. __author__ = 'Martin Ellison'
  15. __version__ = '1.0 2008/07/30'
  16. __url__ = []
  17. __email__ = ["Martin Ellison, m.e:acm*org", "scripts"]
  18. __bpydoc__ = """\
  19. From the popup, select one armature and one mesh.
  20.  
  21. """
  22.  
  23. # ***** BEGIN GPL LICENSE BLOCK *****
  24. #
  25. # Script copyright (C) Martin Ellison
  26. #
  27. # This program is free software; you can redistribute it and/or
  28. # modify it under the terms of the GNU General Public License
  29. # as published by the Free Software Foundation; either version 2
  30. # of the License, or (at your option) any later version.
  31. #
  32. # This program is distributed in the hope that it will be useful,
  33. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. # GNU General Public License for more details.
  36. #
  37. # You should have received a copy of the GNU General Public License
  38. # along with this program; if not, write to the Free Software Foundation,
  39. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  40. #
  41. # ***** END GPL LICENCE BLOCK *****
  42. # --------------------------------------------------------------------------
  43. import Blender
  44. from Blender import Window, Scene, Geometry, Mathutils, Object, Armature, Mesh, Draw
  45. from Blender.Mathutils import Vector
  46.  
  47. def alignArmature(armatureObject, meshObject):
  48. armatureLoc = Vector(armatureObject.loc)
  49. armature = armatureObject.getData()
  50. meshLoc = Vector(meshObject.loc)
  51. diff = armatureLoc - meshLoc
  52. armature.makeEditable()
  53. for bone in armature.bones.values():
  54. bone.head -= diff
  55. bone.tail -= diff
  56. armature.update()
  57. armatureObject.setLocation(meshObject.loc)
  58.  
  59. def main():
  60. print '-----'
  61. scn = Scene.GetCurrent()
  62. sel_object = scn.objects.active
  63. popup = []
  64. armatures = []
  65. meshes = []
  66. for obj in scn.getChildren():
  67. if obj.type == 'Armature':
  68. tog = Blender.Draw.Create(0)
  69. popup.append((obj.name + ' (arm)', tog))
  70. armatures.append((tog, obj.name))
  71. if obj.type == 'Mesh':
  72. tog = Blender.Draw.Create(0)
  73. popup.append((obj.name + ' (mesh)', tog))
  74. meshes.append((tog, obj.name))
  75. if not Blender.Draw.PupBlock("Attach mesh to armature? ", popup): return
  76. armatureName = ''
  77. for tog, name in armatures:
  78. if tog.val:
  79. if armatureName != '': Draw.PupMenu("Duplicate armature")
  80. armatureName = name
  81. meshName = ''
  82. for tog, name in meshes:
  83. if tog.val:
  84. if meshName != '': Draw.PupMenu("Duplicate mesh")
  85. meshName = name
  86.  
  87. mesh_object = Object.Get(meshName)
  88. armature_object = Object.Get(armatureName)
  89. if mesh_object.getType()!='Mesh':
  90. result = Draw.PupMenu("You need a mesh")
  91. return
  92. the_mesh = mesh_object.getData(mesh=1)
  93. print 'the mesh is at ', mesh_object.loc
  94. if armature_object.getType()!='Armature':
  95. result = Draw.PupMenu("You need an armature")
  96. return
  97. the_armature = armature_object.getData()
  98. print 'the armature is at ', armature_object.loc
  99.  
  100. editmode = Window.EditMode()
  101. if editmode: Window.EditMode(0)
  102. Window.WaitCursor(1)
  103.  
  104. if mesh_object.parent != None: mesh_object.clrParent()
  105. alignArmature(armature_object, mesh_object)
  106. armature_object.makeParentDeform([mesh_object])
  107.  
  108. if editmode: Window.EditMode(1)
  109. Window.WaitCursor(0)
  110. Window.RedrawAll()
  111.  
  112.  
  113. if __name__ == '__main__':
  114. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement