I referenced this tutorial to paint textures using bpy. I just replaced the Cube object with my mesh object loaded from an obj file. Rather than using all the textures I just used one of them (in code it is sTex). But on running my script, I don't see any texture inpainted on the mesh.
import bpy, os, math
from mathutils import *
class Material:
def __init__(self, name, diffuse, specular, alpha):
mat = bpy.data.materials.new(name)
mat.diffuse_color = diffuse
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 1.0
mat.specular_color = specular
mat.specular_shader = 'COOKTORR'
mat.specular_intensity = 0.5
mat.alpha = alpha
mat.ambient = 1
self.mat = mat
self.add_texture()
def set_material(self, object):
object.data.materials.append(self.mat)
def add_texture(self):
sTex = bpy.data.textures.new('BumpTex', type = 'STUCCI')
sTex.noise_basis = 'BLENDER_ORIGINAL'
sTex.noise_scale = 0.25
sTex.noise_type = 'SOFT_NOISE'
sTex.saturation = 1
sTex.stucci_type = 'PLASTIC'
sTex.turbulence = 5
mtex = self.mat.texture_slots.add()
mtex.texture = sTex
mtex.texture_coords = 'ORCO'
mtex.use_map_color_diffuse = False
mtex.use_map_normal = True
def load_mesh(path):
bpy.ops.import_scene.obj(filepath = path)
def start():
os.system('cls')
print('######## Output of Script #########')
path = "Mesh.obj"
load_mesh(path)
Man = bpy.data.objects["Man"]
Man.scale = Vector((0.613, 0.613, 0.613))
Man.select = True
bpy.context.scene.objects.active = Man
red = Material('red', (1, 0, 0), (1, 1, 1), 1)
red.set_material(Man)
if __name__ == "__main__":
start()
What I know is - To paint a texture
1. Load the mesh.
2. Create new material for the mesh.
3. Select a texture corresponding to this material. Go to edit mode for that mesh, select all vertices, then unwrap using SMART UV project.
4. Go to UV Image editor, unwrap using SMART UV project, select the texture created above.
But I'm not able to do this with bpy. As such the code runs without any error. Please help me where I'm missing. If possible, explanation with a piece of code will be much more helpful.