Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I use the bpy module in Blender to load a human body mesh from a .obj file and colorize each face of the mesh based on the angle between the face's normal vector and the direction of the sun?

Specifically, I want to color each face of the mesh using the following formula: (vn•e + 1) / 2, where vn is the normal vector of the face, e is the direction of the sun, and • represents the dot product between vn and e.


What I have tried:

import bpy 
import random

filepath = "body_vertor.obj"



# Import the mesh from the .obj file
bpy.ops.import_scene.obj(filepath=filepath)



#bpy.ops.mesh.primitive_plane_add()
body = bpy.context.selected_objects[0]
#body = bpy.context.active_object

#-****************************

r = random.random()
y = random.random()
g = random.random()
alfa = 1.0

color = (r, g, y, alfa)

mat = bpy.data.materials.new("body")
mat.diffuse_color = color 

body.data.materials.append(mat)

bpy.ops.paint.vertex_paint_toggle(color)
Posted
Comments
0x01AA 26-Apr-23 15:31pm    
Now you tried it with the code you mentioned.
Where is your question?
What do you expect and what your code so far delivers? And with which result you are not happy?
Sokhibjamol Boeva 26-Apr-23 15:34pm    
it displays my mesh ( human body) but not color
[no name] 27-Apr-23 11:57am    
Why are you using a "random" color? You don't even know what color to look for. (Unless it's all nonsense code).
Sokhibjamol Boeva 28-Apr-23 4:24am    
if you do not know just do not reply the qeustion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900