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

  1. #!BPY
  2.  
  3. """
  4. Name: 'Envelope Assignment'
  5. Blender: 234
  6. Group: 'Animation'
  7. Tooltip: 'Assigns weights to vertices via envelopes'
  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. __doc__ = """\
  14. This script creates vertex groups from a set of envelopes.
  15.  
  16. "Envelopes" are Mesh objects with names following this naming convention:
  17.  
  18. <bone name>:<float value>
  19.  
  20. Notes:<br>
  21.   - All existing vertex groups of the target Mesh will be deleted.
  22.  
  23. Please check the script's homepage and the thread at blender.org (last link button above) for more info.
  24. """
  25.  
  26. # --------------------------------------------------------------------------
  27. # "Armature Symmetry" by Jonas Petersen
  28. # Version 0.9 - 10th November 2004 - first public release
  29. # --------------------------------------------------------------------------
  30. #
  31. # A script that creates vertex groups from a set of envelopes.
  32. #
  33. # Envelopes are Mesh objects with names that follow the following
  34. # naming convention (syntax):
  35. #
  36. #   <bone name>:<float value>
  37. #
  38. # All existing vertex groups of the target Mesh will be deleted.
  39. #
  40. # Find the latest version at: http://www.mindfloaters.de/blender/
  41. #
  42. # --------------------------------------------------------------------------
  43. # $Id: envelope_assignment.py,v 1.1 2005/05/17 07:17:52 ianwill Exp $
  44. # --------------------------------------------------------------------------
  45. # ***** BEGIN GPL LICENSE BLOCK *****
  46. #
  47. # Copyright (C) 2004: Jonas Petersen, jonas at mindfloaters dot de
  48. #
  49. # This program is free software; you can redistribute it and/or
  50. # modify it under the terms of the GNU General Public License
  51. # as published by the Free Software Foundation; either version 2
  52. # of the License, or (at your option) any later version.
  53. #
  54. # This program is distributed in the hope that it will be useful,
  55. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  56. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  57. # GNU General Public License for more details.
  58. #
  59. # You should have received a copy of the GNU General Public License
  60. # along with this program; if not, write to the Free Software Foundation,
  61. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  62. #
  63. # ***** END GPL LICENCE BLOCK *****
  64.  
  65. # --------------------------------------------------------------------------
  66. # CONFIGURATION
  67. # --------------------------------------------------------------------------
  68.  
  69. # Note: Theses values will later be editable via a gui interface
  70. # within Blender.
  71.  
  72. # SEPARATOR is the character used to delimit the bone name and the weight
  73. # in the envelope name.
  74. SEPARATOR = ":"
  75.  
  76. # --------------------------------------------------------------------------
  77. # END OF CONFIGURATION
  78. # --------------------------------------------------------------------------
  79.  
  80. import Blender, math, sys, string
  81. from Blender import Mathutils
  82. from Blender.Mathutils import *
  83.  
  84. def run(target_obj):
  85.     target_mesh = target_obj.getData()
  86.  
  87.     num_verts = len(target_mesh.verts)
  88.     warn_count = 0
  89.     main_obj_loc = Vector(list(target_obj.getLocation()))
  90.  
  91.     Blender.Window.EditMode(0)
  92.  
  93.     def vertGroupExist(group):
  94.         global vert_group_names;
  95.         for name in target_mesh.getVertGroupNames():
  96.             if group == name:
  97.                 return True
  98.         return False
  99.  
  100.     def isInside(point, envl_data):
  101.         for i in range(len(envl_data['normals'])):
  102.             vec = point - envl_data['points'][i]
  103.             if DotVecs(envl_data['normals'][i], vec) > 0.0:
  104.                 return False
  105.         return True
  106.  
  107.     envls = {}
  108.  
  109.     Blender.Window.DrawProgressBar(0.0, "Parsing Zones")
  110.  
  111.     # go through all envelopes and fill the 'envls' dict with points, normals
  112.     # and weights of the box faces
  113.     for obj in Blender.Object.Get():
  114.         if obj.getType() == "Mesh":
  115.             name = obj.getName()
  116.             pos = name.find(SEPARATOR)
  117.             if (pos > -1):
  118.                 mesh = obj.getData()
  119.                 loc = Vector(list(obj.getLocation()))
  120.  
  121.                 bone_name = name[0:pos]
  122.                 try:
  123.                     weight = float(name[pos+1:len(name)])
  124.                 except ValueError:
  125.                     print "WARNING: invalid syntax in envelope name \"%s\" - syntax: \"<bone name>:<float value>\""%(obj.getName())
  126.                     warn_count += 1
  127.                     weight = 0.0
  128.  
  129.                 envl_data = {'points': [], 'normals': [], 'weight': weight}
  130.                 for face in mesh.faces:
  131.                     envl_data['normals'].append(Vector(list(face.normal)))
  132.                     envl_data['points'].append(Vector(list(face.v[0].co)) + loc)
  133.  
  134.                 if not envls.has_key(bone_name):
  135.                     # add as first data set
  136.                     envls[bone_name] = [envl_data]
  137.                 else:
  138.                     # add insert in sorted list of data sets
  139.                     inserted = False
  140.                     for i in range(len(envls[bone_name])):
  141.                         if envl_data['weight'] > envls[bone_name][i]['weight']:
  142.                             envls[bone_name].insert(i, envl_data)
  143.                             inserted = True
  144.                     if not inserted:
  145.                         envls[bone_name].append(envl_data)
  146.  
  147.     Blender.Window.DrawProgressBar(0.33, "Parsing Vertices")
  148.     
  149.     assign_count = 0
  150.     vert_groups = {}
  151.  
  152.     # go throug all vertices of the target mesh
  153.     for vert in target_mesh.verts:
  154.         point = Vector(list(vert.co)) + main_obj_loc
  155.  
  156.         vert.sel = 1
  157.         counted = False
  158.  
  159.         for bone_name in envls.keys():
  160.             for envl_data in envls[bone_name]:
  161.  
  162.                 if (isInside(point, envl_data)):
  163.  
  164.                     if (not vert_groups.has_key(bone_name)):
  165.                         vert_groups[bone_name] = {}
  166.  
  167.                     if (not vert_groups[bone_name].has_key(envl_data['weight'])):
  168.                             vert_groups[bone_name][envl_data['weight']] = []
  169.  
  170.                     vert_groups[bone_name][envl_data['weight']].append(vert.index)
  171.  
  172.                     vert.sel = 0
  173.  
  174.                     if not counted:
  175.                         assign_count += 1
  176.                         counted = True
  177.  
  178.                     break
  179.  
  180.  
  181.     Blender.Window.DrawProgressBar(0.66, "Writing Groups")
  182.  
  183.     vert_group_names = target_mesh.getVertGroupNames()
  184.  
  185.     # delete all vertices in vertex groups
  186.     for group in vert_group_names:
  187.         try:
  188.             v = target_mesh.getVertsFromGroup(group)
  189.         except:
  190.             pass
  191.         else:
  192.             # target_mesh.removeVertsFromGroup(group) without second argument doesn't work
  193.             #print "removing", len(v), "vertices from group \"",group,"\""
  194.             target_mesh.removeVertsFromGroup(group, v)
  195.  
  196.     # delete all vertex groups
  197.     for group in vert_group_names:
  198.         target_mesh.removeVertGroup(group)
  199.  
  200.     # create new vertex groups and fill them
  201.     if 1:
  202.         for bone_name in vert_groups.keys():
  203.             # add vertex group
  204.             target_mesh.addVertGroup(bone_name)
  205.  
  206.             for weight in vert_groups[bone_name]:
  207.                 print "name: ", bone_name, ": ", weight, "len: ", len(vert_groups[bone_name][weight])
  208.                 index_list = vert_groups[bone_name][weight]
  209.                 target_mesh.assignVertsToGroup(bone_name, index_list, weight, 'replace')
  210.  
  211.     target_mesh.update(0)
  212.  
  213.     Blender.Window.DrawProgressBar(1.0, "")
  214.  
  215.     if assign_count < num_verts:
  216.         Blender.Window.EditMode(1)
  217.         print '\a'
  218.         if warn_count: warn_msg =  " There is also %d warning(s) in the console."%(warn_count)
  219.         else: warn_msg = ""
  220.         Blender.Draw.PupMenu("Envelope Assignment%%t|%d vertices were not assigned.%s"%(num_verts-assign_count, warn_msg))
  221.     elif warn_count:
  222.         print '\a'
  223.         Blender.Draw.PupMenu("Envelope Assignment%%t|There is %d warning(s) in the console."%(warn_count))
  224.  
  225. sel_objs = Blender.Object.GetSelected()
  226. if len(sel_objs) != 1 or sel_objs[0].getType() != "Mesh":
  227.     print '\a'
  228.     Blender.Draw.PupMenu("Envelope Assignment%t|Please select 1 Mesh object to assign vertex groups to!")
  229. else:
  230.     if string.find(sel_objs[0].getName(), SEPARATOR) > -1:
  231.         print '\a'
  232.         Blender.Draw.PupMenu("Envelope Assignment%t|Don't use the command on the envelopes themselves!")
  233.     else:
  234.         run(sel_objs[0])
  235.