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.
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