def RunCombineAO(context):
    obj = bpy.context.active_object
    mesh = obj.data

    # Set mode
    oldMode = bpy.context.object.mode
    if (bpy.context.mode != 'OBJECT'):
        bpy.ops.object.mode_set(mode='OBJECT')

    # Check layers exist
    CheckVertexColorLayer(context.scene.scn_inputVCLayer1, mesh)
    CheckVertexColorLayer(context.scene.scn_inputVCLayer2, mesh)
    CheckVertexColorLayer(context.scene.scn_targetVCLayer, mesh)

    # Get layers
    inputLayer1 = mesh.vertex_colors[context.scene.scn_inputVCLayer1]
    inputLayer2 = mesh.vertex_colors[context.scene.scn_inputVCLayer2]
    targetLayer = mesh.vertex_colors[context.scene.scn_targetVCLayer]
    color = Color()

    # Perform operation
    i = 0
    for poly in mesh.polygons:
        for loop in poly.loop_indices:
            color.r = inputLayer1.data[i].color.r * inputLayer2.data[i].color.r
            color.g = inputLayer1.data[i].color.g * inputLayer2.data[i].color.g
            color.b = inputLayer1.data[i].color.b * inputLayer2.data[i].color.b
            targetLayer.data[loop].color = color
            i += 1

    # Update mesh and revert to old mode
    mesh.update()
    bpy.ops.object.mode_set(mode=oldMode)
    print("Finished CombineAOOperator")
Ejemplo n.º 2
0
def marble_color(x):
    clr = Color((0.0, 0.0, 0.0))
    x = math.sqrt(x + 1.0) * .707
    clr.g = green + .8 * x
    x = math.sqrt(x)
    clr.r = red + .6 * x
    clr.b = blue + .4 * x
    return clr[:]
Ejemplo n.º 3
0
def get_color_for_normal(normal, remap=True):
    color = Color(normal)

    if remap:
        color.r = remap_value(color.r, -1, 1, 0, 1)
        color.g = remap_value(color.g, -1, 1, 0, 1)
        color.b = remap_value(color.b, -1, 1, 0, 1)

    return color
def RunColorNoise(context):
    obj = bpy.context.active_object
    mesh = obj.data

    # Set mode
    oldMode = bpy.context.object.mode
    if (bpy.context.mode != 'OBJECT'):
        bpy.ops.object.mode_set(mode='OBJECT')

    col_layer = mesh.vertex_colors.active
    color = Color()

    # Perform operation
    i = 0
    for poly in mesh.polygons:
        for loop in poly.loop_indices:
            if (mesh.vertices[mesh.loops[loop].vertex_index].select):
                color = col_layer.data[loop].color
                h, s, v = colorsys.rgb_to_hsv(color.r, color.g, color.b)
                h += random.uniform(-context.scene.scn_noiseDeviance,
                                    context.scene.scn_noiseDeviance)
                s += random.uniform(-context.scene.scn_noiseDeviance,
                                    context.scene.scn_noiseDeviance)
                v += random.uniform(-context.scene.scn_noiseDeviance,
                                    context.scene.scn_noiseDeviance)
                r, g, b = colorsys.hsv_to_rgb(h, s, v)
                color.r = r
                color.g = g
                color.b = b
                col_layer.data[loop].color = color
            i += 1

    # Update mesh and revert to old mode
    mesh.update()
    bpy.ops.object.mode_set(mode=oldMode)
    print("Finished Color Noise")
Ejemplo n.º 5
0
    def paintVerts(self,
                   context,
                   start_point,
                   end_point,
                   start_color,
                   end_color,
                   circular_gradient=False,
                   use_hue_blend=False):
        region = context.region
        rv3d = context.region_data

        obj = context.active_object
        mesh = obj.data

        # Create a new bmesh to work with
        bm = bmesh.new()
        bm.from_mesh(mesh)
        bm.verts.ensure_lookup_table()

        # List of structures containing 3d vertex and project 2d position of vertex
        vertex_data = None  # Will contain vert, and vert coordinates in 2d view space
        if mesh.use_paint_mask_vertex:  # Face masking not currently supported
            vertex_data = [(v,
                            view3d_utils.location_3d_to_region_2d(
                                region, rv3d, obj.matrix_world @ v.co))
                           for v in bm.verts if v.select]
        else:
            vertex_data = [(v,
                            view3d_utils.location_3d_to_region_2d(
                                region, rv3d, obj.matrix_world @ v.co))
                           for v in bm.verts]

        # Vertex transformation math
        down_vector = Vector((0, -1, 0))
        direction_vector = Vector(
            (end_point.x - start_point.x, end_point.y - start_point.y,
             0)).normalized()
        rotation = direction_vector.rotation_difference(down_vector)

        translation_matrix = Matrix.Translation(
            Vector((-start_point.x, -start_point.y, 0)))
        inverse_translation_matrix = translation_matrix.inverted()
        rotation_matrix = rotation.to_matrix().to_4x4()
        combinedMat = inverse_translation_matrix @ rotation_matrix @ translation_matrix

        transStart = combinedMat @ start_point.to_4d(
        )  # Transform drawn line : rotate it to align to horizontal line
        transEnd = combinedMat @ end_point.to_4d()
        minY = transStart.y
        maxY = transEnd.y
        heightTrans = maxY - minY  # Get the height of transformed vector

        transVector = transEnd - transStart
        transLen = transVector.length

        # Calculate hue, saturation and value shift for blending
        if use_hue_blend:
            start_color = Color(start_color[:3])
            end_color = Color(end_color[:3])
            c1_hue = start_color.h
            c2_hue = end_color.h
            hue_separation = c2_hue - c1_hue
            if hue_separation > 0.5:
                hue_separation = hue_separation - 1
            elif hue_separation < -0.5:
                hue_separation = hue_separation + 1
            c1_sat = start_color.s
            sat_separation = end_color.s - c1_sat
            c1_val = start_color.v
            val_separation = end_color.v - c1_val

        color_layer = bm.loops.layers.color.active

        for data in vertex_data:
            vertex = data[0]
            vertCo4d = Vector((data[1].x, data[1].y, 0))
            transVec = combinedMat @ vertCo4d

            t = 0

            if circular_gradient:
                curVector = transVec.to_4d() - transStart
                curLen = curVector.length
                t = abs(max(min(curLen / transLen, 1), 0))
            else:
                t = abs(max(min((transVec.y - minY) / heightTrans, 1), 0))

            color = Color((1, 0, 0))
            if use_hue_blend:
                # Hue wraps, and fmod doesn't work with negative values
                color.h = fmod(1.0 + c1_hue + hue_separation * t, 1.0)
                color.s = c1_sat + sat_separation * t
                color.v = c1_val + val_separation * t
            else:
                color.r = start_color[0] + (end_color[0] - start_color[0]) * t
                color.g = start_color[1] + (end_color[1] - start_color[1]) * t
                color.b = start_color[2] + (end_color[2] - start_color[2]) * t

            if mesh.use_paint_mask:  # Masking by face
                face_loops = [
                    loop for loop in vertex.link_loops if loop.face.select
                ]  # Get only loops that belong to selected faces
            else:  # Masking by verts or no masking at all
                face_loops = [loop for loop in vertex.link_loops
                              ]  # Get remaining vert loops

            for loop in face_loops:
                new_color = loop[color_layer]
                new_color[:3] = color
                loop[color_layer] = new_color

        bm.to_mesh(mesh)
        bm.free()
        bpy.ops.object.mode_set(mode='VERTEX_PAINT')