0
$\begingroup$

I want to give the color to the human body mesh that was loaded from the .obj file as v,vn, vt,f I want to give the color with a specific formula as color=(n*e+1)/2)*225 where n is vn, normal vector, and e is sun position can be from above as (0,0,1) and the color should fill the triangle of the given normal vector.enter image description here

Here is my sample code but it is not working

import bpy
import math
# Set up the scene
scene = bpy.context.scene
scene.render.engine = 'CYCLES'
# Import the human body model
bpy.ops.import_scene.obj(filepath="C:\\Users\\Desktop\\body_script\\body_triangular.obj")
# Rename the imported object
human_obj = bpy.context.selected_objects[0]
human_obj.name = "Human"
# Add a Sun lamp to the scene
bpy.ops.object.light_add(type='SUN', location=(0,0,0))
sun = bpy.data.objects['Sun']
sun.name = "Sun"
sun.data.direction = (0, 0, 1)
# Get the sun position
e = math.cos(sun.rotation_euler[0])
# Define the color function
def get_color(n, e):
    r = (n[0]*e+1)/2 * 255
    g = (n[1]*e+1)/2 * 255
    b = (n[2]*e+1)/2 * 255
    return (r, g, b)
# Loop through the vertices and set their colors
human_mesh = human_obj.data
for poly in human_mesh.polygons:
    for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
        n = human_mesh.loops[loop_index].normal
        color = get_color(n, e)
        if len(human_mesh.vertex_colors) == 0:
            human_mesh.vertex_colors.new()
        human_mesh.vertex_colors.active.data[loop_index].color = color
$\endgroup$
8
  • $\begingroup$ Could you explain what you want to obtain and what is not working? $\endgroup$
    – lemon
    Commented Apr 22, 2023 at 16:19
  • $\begingroup$ it displays the body but it is not putting the colors $\endgroup$
    – Szabi
    Commented Apr 22, 2023 at 16:44
  • $\begingroup$ I want to color each triangular section in the picture as color = ((normal.dot(sun_pos) + 1) / 2) * 255 here, normal - vn normal vector of the triangle, I want to fill it as each triangle color = (color, color, color, 255) $\endgroup$
    – Szabi
    Commented Apr 22, 2023 at 16:56
  • $\begingroup$ ok, afaik, sun data has no "direction". What do you mean by this line? $\endgroup$
    – lemon
    Commented Apr 22, 2023 at 17:00
  • 1
    $\begingroup$ Hello, Szabi .. welcome.. To avoid this question being clobbered for being XY, I think it would be much better to illustrate / indicate the desired end effect of this code. $\endgroup$
    – Robin Betts
    Commented Apr 23, 2023 at 10:49

1 Answer 1

0
$\begingroup$

ok, all is not clear about what is the goal here (in my opinion).

But here are some modifications for the end of your script (with needed comments added).

Also you'll need to remove

sun.data.direction = (0, 0, 1)

because sun has no directionin its data.

def get_color(n, e):
    # blender's colors are from 0 to 1
    # so I removed "* 255"
    r = (n[0]*e+1)/2
    g = (n[1]*e+1)/2
    b = (n[2]*e+1)/2
    return (r, g, b, 1) # added "1", because vcol are rgba

# Loop through the vertices and set their colors
human_mesh = human_obj.data

# This can be done once (so outside of the loop)
if len(human_mesh.vertex_colors) == 0:
    human_mesh.vertex_colors.new()
for poly in human_mesh.polygons:
    n = poly.normal
    # as you need to use the face normal, the calculation below
    # can be done once per face
    color = get_color(n, e)
    for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
        #n = human_mesh.loops[loop_index].normal << this is not the face normal
        human_mesh.vertex_colors.active.data[loop_index].color = color

Using Suzanne, that gives the following result:

enter image description here

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .