3
$\begingroup$

I have seen this thread: How can I use an image texture as a guide for weight painting?

and it allows me to use textures as a guide for weight painting... but is it possible to get weight paint maps in and out of Blender, so they could be edited with a more sophisticated image editor?

$\endgroup$
  • 1
    $\begingroup$ I'm pretty sure the answer is no. Weight maps are not images. $\endgroup$ – someonewithpc Mar 1 '15 at 17:46
  • 2
    $\begingroup$ I think that is my understanding, and that link is the best option I have found... but it would be nice if there were some way... $\endgroup$ – rcgauer Mar 1 '15 at 17:58
  • $\begingroup$ And my particular need is in relation to particle systems... especially when I have a need for multiple hair systems on a body, for example: long, short, add more here, omit here... I could do this in 10 minutes in Painter, but even with hairs turned off, the model is just crushing my machine... and you just don;t get a ton of precision. $\endgroup$ – rcgauer Mar 1 '15 at 18:34
8
$\begingroup$

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 :

how to use

$\endgroup$
  • $\begingroup$ I am probably not implementing this properly... but I get an error message to see the Py console, but no message there... $\endgroup$ – rcgauer Mar 2 '15 at 0:15
  • $\begingroup$ @rcgauer you should have your object selected in object mode ;also the to see the console go to 'Window->Toggle_system_console' ; I'm still working on it to add weight to vert_col so any feedback is welcome $\endgroup$ – Chebhou Mar 2 '15 at 0:23
  • $\begingroup$ HOLY COW! This is outstanding.... I do not have any comments yet, as I am working with it on a basic model. Let me go mess with my high-res mesh... but this is FANTASTIC! $\endgroup$ – rcgauer Mar 2 '15 at 19:42
  • $\begingroup$ @rcgauer your feedback will certainly help to improve it $\endgroup$ – Chebhou Mar 2 '15 at 19:45
  • 1
    $\begingroup$ @Chebhou refactored code to be compatible with restriced context of current Blender versions $\endgroup$ – aliasguru Mar 23 '17 at 13:53

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.