This script will convert weight to vertex color and vertex color to weight :
- the script create new groups if they don't exist and overwrite old groups with the same names
- the script use two methods for conversions grayscale<-->weight and RGB<-->weight
- the conversion works in two ways weight--> color and color-->weight
HOW TO USE:
- run the script once "it will be registered"
- in 3D view select the object
- hit space and type weight&color
- choose the settings and conversion hit OK
- new groups or vertex color maps will be created with the appropriate values "the viewport will change to show the result"
import bpy
from bpy.types import Operator
from bpy.props import EnumProperty
from mathutils import Color
bl_info = {
'name': 'Convert Vertex Color to Weights',
'author': 'Chebhou on StackExchange, refactor by aliasguru',
'version': (0, 1, 0),
'blender': (2, 7, 5),
'location': '3D View > Spacebar Menu > weight & color',
'description': 'converts Vertex colors to weights and vice versa',
'tracker_url': '',
'wiki_url': 'http://blender.stackexchange.com/questions/26467/weight-paint-maps-in-out-of-blender-to-allow-editing-in-photoshop-or-other',
'support': 'COMMUNITY',
'category': '3D View'}
color = 'HSV' #'HSV' method used for conversion
def convert(value, method):
if method == 'BW2W':
return (value.r + value.g+ value.b)/3
elif method == 'W2BW':
col = Color((value, value, value))
return col
elif method == 'HSV2W':
return 1-(value.h / 0.66)
elif method == 'W2HSV':
col = Color()
col.hsv = (0.66*(1-value), 1, 1)
return col
def vert_col2weight(context, color):
color_maps = context.object.data.vertex_colors
for color_map in color_maps :
group_name = color_map.name
#check for existing group with the same name
if None == context.object.vertex_groups.get(group_name):
context.object.vertex_groups.new( name = group_name)
group_ind = context.object.vertex_groups[group_name].index
for poly in context.object.data.polygons:
for loop_ind in poly.loop_indices:
vert_ind =context.object.data.loops[loop_ind].vertex_index
col = color_map.data[loop_ind].color
if color == 'BW':
weight = convert(col, 'BW2W')
else :
weight = convert(col, 'HSV2W')
context.object.vertex_groups[group_ind].add([vert_ind], weight,'REPLACE')
def weight2vert_col(context, color):
vert_groups = context.object.vertex_groups
col = Color()
for vert_g in vert_groups:
group_name = vert_g.name
#check for existing group with the same name
if None == context.object.data.vertex_colors.get(group_name):
context.object.data.vertex_colors.new(name=group_name)
color_map = context.object.data.vertex_colors[group_name]
for poly in context.object.data.polygons:
for loop_ind in poly.loop_indices:
vert_ind =context.object.data.loops[loop_ind].vertex_index
#check if the vertex belong to the group
weight = 0
for g in context.object.data.vertices[vert_ind].groups:
if g.group == vert_groups[group_name].index:
weight = vert_groups[group_name].weight(vert_ind)
#convert weight to vert_col
if color == 'BW':
col = convert(weight, 'W2BW')
else :
col = convert(weight, 'W2HSV')
#assign to the color map
color_map.data[loop_ind].color = col
class weight_color(Operator):
"""weight from&to vert_color"""
bl_idname = "object.weight_color"
bl_label = "weight & color"
bl_options = {'REGISTER', 'UNDO'} #should remove undo ?
#parameters and variables
convert = EnumProperty(
name="Convert",
description="Choose conversion",
items=(('W2C', "Weight to vertex color", "convert weight to vertex color"),
('C2W', "Vertex color to weight", "convert vertex color to weight")),
default='W2C',
)
color = EnumProperty(
name="Color type",
description="Choose a color system",
items=(('BW', "Gray scale", "map weight to grayscale"),
('HSV', "RGB color", "map weight to rgb colors")),
default='HSV',
)
#main function
def execute(self, context):
bpy.ops.object.mode_set(mode = 'OBJECT')
if self.convert == 'W2C':
weight2vert_col(context, self.color)
bpy.ops.object.mode_set(mode = 'VERTEX_PAINT')
else:
vert_col2weight(context, self.color)
bpy.ops.object.mode_set(mode = 'WEIGHT_PAINT')
context.active_object.data.update()
self.report({'INFO'},"Conversion is Done")
return {'FINISHED'}
#get inputs
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def addObject(self, context):
self.layout.operator(
weight_color.bl_idname,
text = weight_color.__doc__,
icon = 'VPAINT_HLT')
def register():
bpy.utils.register_class(weight_color)
bpy.types.VIEW3D_MT_object.append(addObject)
def unregister():
bpy.types.VIEW3D_MT_object.remove(addObject)
bpy.utils.unregister_class(weight_color)
if __name__ == "__main__":
register()
any feedback is welcome
example :
