2006-05-05 16:17:59 +00:00
|
|
|
#!BPY
|
|
|
|
"""
|
|
|
|
Name: 'Clean Weight...'
|
2008-04-23 14:04:05 +00:00
|
|
|
Blender: 245
|
2006-05-05 16:17:59 +00:00
|
|
|
Group: 'WeightPaint'
|
|
|
|
Tooltip: 'Removed verts from groups below a weight limit.'
|
|
|
|
"""
|
|
|
|
|
2008-04-23 14:04:05 +00:00
|
|
|
__author__ = "Campbell Barton aka ideasman42"
|
|
|
|
__url__ = ["www.blender.org", "blenderartists.org", "www.python.org"]
|
2006-05-05 16:17:59 +00:00
|
|
|
__version__ = "0.1"
|
|
|
|
__bpydoc__ = """\
|
|
|
|
|
|
|
|
Clean Weight
|
|
|
|
|
|
|
|
This Script is to be used only in weight paint mode,
|
|
|
|
It removes very low weighted verts from the current group with a weight option.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
#
|
|
|
|
# Script copyright (C) Campbell J Barton
|
|
|
|
#
|
|
|
|
# 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.
|
2008-08-04 23:20:12 +00:00
|
|
|
#
|
2006-05-05 16:17:59 +00:00
|
|
|
# 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 *****
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
2007-01-13 03:07:04 +00:00
|
|
|
from Blender import Scene, Draw, Object
|
2006-05-05 16:17:59 +00:00
|
|
|
import BPyMesh
|
2006-12-27 09:31:28 +00:00
|
|
|
def weightClean(me, PREF_THRESH, PREF_KEEP_SINGLE, PREF_OTHER_GROUPS):
|
2006-05-05 16:17:59 +00:00
|
|
|
|
|
|
|
groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me)
|
|
|
|
act_group= me.activeGroup
|
|
|
|
|
2007-01-13 03:07:04 +00:00
|
|
|
rem_count = 0
|
|
|
|
|
2006-12-27 09:31:28 +00:00
|
|
|
if PREF_OTHER_GROUPS:
|
|
|
|
for wd in vWeightDict:
|
|
|
|
l = len(wd)
|
|
|
|
if not PREF_KEEP_SINGLE or l > 1:
|
2008-08-04 23:20:12 +00:00
|
|
|
# cant use iteritems because the dict is having items removed
|
2006-12-27 09:31:28 +00:00
|
|
|
for group in wd.keys():
|
|
|
|
w= wd[group]
|
|
|
|
if w <= PREF_THRESH:
|
|
|
|
# small weight, remove.
|
|
|
|
del wd[group]
|
2007-01-13 03:07:04 +00:00
|
|
|
rem_count +=1
|
2008-08-04 23:20:12 +00:00
|
|
|
l-=1
|
2006-12-27 09:31:28 +00:00
|
|
|
|
|
|
|
if PREF_KEEP_SINGLE and l == 1:
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
for wd in vWeightDict:
|
|
|
|
if not PREF_KEEP_SINGLE or len(wd) > 1:
|
|
|
|
try:
|
|
|
|
w= wd[act_group]
|
|
|
|
if w <= PREF_THRESH:
|
|
|
|
# small weight, remove.
|
|
|
|
del wd[act_group]
|
2007-01-13 03:07:04 +00:00
|
|
|
rem_count +=1
|
2006-12-27 09:31:28 +00:00
|
|
|
except:
|
|
|
|
pass
|
2006-05-05 16:17:59 +00:00
|
|
|
|
|
|
|
# Copy weights back to the mesh.
|
|
|
|
BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict)
|
2007-01-13 03:07:04 +00:00
|
|
|
return rem_count
|
2006-05-05 16:17:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
scn= Scene.GetCurrent()
|
2006-12-27 09:31:28 +00:00
|
|
|
ob= scn.objects.active
|
2006-05-05 16:17:59 +00:00
|
|
|
|
2006-12-27 09:31:28 +00:00
|
|
|
if not ob or ob.type != 'Mesh':
|
2006-05-05 16:17:59 +00:00
|
|
|
Draw.PupMenu('Error, no active mesh object, aborting.')
|
|
|
|
return
|
|
|
|
|
|
|
|
me= ob.getData(mesh=1)
|
|
|
|
|
2006-12-27 09:31:28 +00:00
|
|
|
PREF_PEAKWEIGHT= Draw.Create(0.001)
|
2006-05-05 16:17:59 +00:00
|
|
|
PREF_KEEP_SINGLE= Draw.Create(1)
|
2006-12-27 09:31:28 +00:00
|
|
|
PREF_OTHER_GROUPS= Draw.Create(0)
|
2006-05-05 16:17:59 +00:00
|
|
|
|
|
|
|
pup_block= [\
|
2006-12-27 09:31:28 +00:00
|
|
|
('Peak Weight:', PREF_PEAKWEIGHT, 0.005, 1.0, 'Remove verts from groups below this weight.'),\
|
|
|
|
('All Other Groups', PREF_OTHER_GROUPS, 'Clean all groups, not just the current one.'),\
|
|
|
|
('Keep Single User', PREF_KEEP_SINGLE, 'Keep verts in at least 1 group.'),\
|
2006-05-05 16:17:59 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if not Draw.PupBlock('Clean Selected Meshes...', pup_block):
|
|
|
|
return
|
|
|
|
|
2007-01-13 03:07:04 +00:00
|
|
|
rem_count = weightClean(me, PREF_PEAKWEIGHT.val, PREF_KEEP_SINGLE.val, PREF_OTHER_GROUPS.val)
|
|
|
|
|
|
|
|
# Run on entire blend file. usefull sometimes but dont let users do it.
|
|
|
|
'''
|
|
|
|
rem_count = 0
|
|
|
|
for ob in Object.Get():
|
|
|
|
if ob.type != 'Mesh':
|
|
|
|
continue
|
|
|
|
me= ob.getData(mesh=1)
|
|
|
|
|
|
|
|
rem_count += weightClean(me, PREF_PEAKWEIGHT.val, PREF_KEEP_SINGLE.val, PREF_OTHER_GROUPS.val)
|
|
|
|
'''
|
|
|
|
Draw.PupMenu('Removed %i verts from groups' % rem_count)
|
2006-05-05 16:17:59 +00:00
|
|
|
|
|
|
|
if __name__=='__main__':
|
2008-08-04 23:20:12 +00:00
|
|
|
main()
|