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

  1. #!BPY
  2.  
  3. """
  4. Name: 'Armature Symmetry'
  5. Blender: 234
  6. Group: 'Animation'
  7. Tooltip: 'Make an armature symetrical'
  8. """
  9.  
  10. __author__ = "Jonas Petersen"
  11. __url__ = ("blender", "elysiun", "Script's homepage, http://www.mindfloaters.de/blender/", "thread at blender.org, http://www.blender.org/modules.php?op=modload&name=phpBB2&file=viewtopic&t=4858")
  12. __version__ = "0.9 2004-11-10"
  13.  
  14. __doc__ = """\
  15. This script creates perfectly symmetrical armatures.
  16.  
  17. With default configuration it will:<br>
  18.   - Look for bones that have the reference suffix (".L") and
  19. adjust/create the according opposite bone (suffix ".R");<br>
  20.   - Center align all bones that _don't_ have the suffix ".X".
  21.  
  22. Please check the script's homepage and the thread at blender.org (last link button above) for more info.
  23.  
  24. For this version users need to edit the script code to change default options.
  25. """
  26.  
  27. # --------------------------------------------------------------------------
  28. # "Armature Symmetry" by Jonas Petersen
  29. # Version 0.9 - 10th November 2004 - first public release
  30. # --------------------------------------------------------------------------
  31. #
  32. # A script for creating perfectly symmetrical armatures.
  33. #
  34. # It is available in Object Mode via the menu item:
  35. #
  36. #   Object -> Scripts -> Armature Symmetry
  37. #
  38. # With default configuration it will:
  39. #
  40. #   - Look for bones that have the reference suffix (".L") and
  41. #     adjust/create the according opposite bone (suffix ".R").
  42. #
  43. #   - Center align all bones that _don't_ have the suffix ".X"
  44. #
  45. # Find the latest version at: http://www.mindfloaters.de/blender/
  46. #
  47. # --------------------------------------------------------------------------
  48. # $Id: armature_symetry.py,v 1.1 2005/05/17 07:17:52 ianwill Exp $
  49. # --------------------------------------------------------------------------
  50. # ***** BEGIN GPL LICENSE BLOCK *****
  51. #
  52. # Copyright (C) 2004: Jonas Petersen, jonas at mindfloaters dot de
  53. #
  54. # This program is free software; you can redistribute it and/or
  55. # modify it under the terms of the GNU General Public License
  56. # as published by the Free Software Foundation; either version 2
  57. # of the License, or (at your option) any later version.
  58. #
  59. # This program is distributed in the hope that it will be useful,
  60. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  61. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  62. # GNU General Public License for more details.
  63. #
  64. # You should have received a copy of the GNU General Public License
  65. # along with this program; if not, write to the Free Software Foundation,
  66. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  67. #
  68. # ***** END GPL LICENCE BLOCK *****
  69.  
  70. # --------------------------------------------------------------------------
  71. # CONFIGURATION
  72. # --------------------------------------------------------------------------
  73.  
  74. # Note: Theses values will later be editable via a gui interface
  75. # within Blender.
  76.  
  77. # CENTER_SUFFIX is the suffix for bones that should (or shouldn't) get
  78. # center aligned. The default is '.X'.
  79. CENTER_SUFFIX = '.X'
  80.  
  81. # CENTER_SUFFIX_MODE:
  82. #
  83. #   'include'  only bones with the CENTER_SUFFIX appended
  84. #              get center aligned.
  85. #
  86. #   'exclude'  (default)
  87. #              all bones except those with the CENTER_SUFFIX
  88. #              appended get center aligned.
  89. #
  90. #
  91. #   'off'      bones will not get center aligned at all.
  92. #
  93. CENTER_SUFFIX_MODE = 'exclude'
  94.  
  95. # The suffix for the reference and opposite side of the
  96. # armature. Bone positions of the opposite side will be overwritten by
  97. # the mirrored values of the reference side.
  98. # The default is REF_SUFFIX = '.L' and OPP_SUFFIX = '.R'.
  99. REF_SUFFIX = '.L'
  100. OPP_SUFFIX = '.R'
  101.  
  102. # MIRROR_AXIS defines the axis in which bones are mirrored/aligned.
  103. # Values:
  104. #   0 for X (default)
  105. #   1 for Y
  106. #   2 for Z
  107. MIRROR_AXIS = 0
  108.  
  109. # --------------------------------------------------------------------------
  110. # END OF CONFIGURATION
  111. # --------------------------------------------------------------------------
  112.  
  113. import Blender
  114.  
  115. def splitName(bone):
  116.     name = bone.getName()
  117.     base = name[0:len(name)-ref_suffix_len]
  118.     rsuff = name[-ref_suffix_len:len(name)]
  119.     csuff = name[-center_suffix_len:len(name)]
  120.     return name, base, rsuff, csuff
  121.  
  122. ref_suffix_len = len(REF_SUFFIX);
  123. center_suffix_len = len(CENTER_SUFFIX);
  124. armature_selected = False
  125.  
  126. obj_list = Blender.Object.GetSelected()
  127. for obj in obj_list:
  128.     if obj.getType() == "Armature":
  129.         armature_selected = True
  130.         arm = obj.getData()
  131.         bones = arm.getBones()
  132.         bonehash = {}
  133.  
  134.         for bone in bones:
  135.             bonehash[bone.getName()] = bone
  136.  
  137.         for bone in bones:
  138.             name, base, rsuff, csuff = splitName(bone)
  139.  
  140.             # reference bone?
  141.             if (rsuff == REF_SUFFIX):
  142.                 oppname = base + OPP_SUFFIX
  143.  
  144.                 # create opposite bone if necessary
  145.                 if not bonehash.has_key(oppname):
  146.                     bonehash[oppname]=Blender.Armature.Bone.New(oppname)
  147.                     parent = bone.getParent()
  148.                     if parent:
  149.                         pname, pbase, prsuff, pcsuff = splitName(parent)
  150.                         if prsuff == REF_SUFFIX:
  151.                             poppname = pbase + OPP_SUFFIX
  152.                             if bonehash.has_key(poppname):
  153.                                 bonehash[oppname].setParent(bonehash[poppname])
  154.                         else:
  155.                             bonehash[oppname].setParent(parent)
  156.                     arm.addBone(bonehash[oppname])
  157.  
  158.                 # mirror bone coords
  159.  
  160.                 tail = bone.getTail()
  161.                 tail[MIRROR_AXIS] *= -1;
  162.                 bonehash[oppname].setTail(tail)
  163.  
  164.                 head = bone.getHead()
  165.                 head[MIRROR_AXIS] *= -1;
  166.                 bonehash[oppname].setHead(head)
  167.  
  168.                 roll = -bone.getRoll()
  169.                 bonehash[oppname].setRoll(roll)
  170.  
  171.                 # Write access to ik flag not (yet?) supported in Blender (2.34)
  172.                 #if bone.hasParent():
  173.                 #    bonehash[oppname].setIK(not bone.getIK())
  174.  
  175.             # center bone?
  176.             elif (rsuff != OPP_SUFFIX) and \
  177.                  (CENTER_SUFFIX_MODE != 'off') and \
  178.                  ((CENTER_SUFFIX_MODE == 'exclude' and csuff != CENTER_SUFFIX) or \
  179.                  (CENTER_SUFFIX_MODE == 'include' and csuff == CENTER_SUFFIX)):
  180.  
  181.                 # center bone coords
  182.  
  183.                 tail = bone.getTail()
  184.                 tail[MIRROR_AXIS] = 0.0;
  185.                 bone.setTail(tail)
  186.  
  187.                 head = bone.getHead()
  188.                 head[MIRROR_AXIS] = 0.0;
  189.                 bone.setHead(head)
  190.  
  191.                 # Setting set roll in python rotates all child bones.
  192.                 # Not so if set via the Transform Properties in Blender.
  193.                 # Bug?
  194.                 bone.setRoll(0.0)
  195.  
  196. if not armature_selected:
  197.     Blender.Draw.PupMenu("Armature Symmetry%t|Please select an Armature object!")
  198.