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
def set_material(self, object):
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():
    print('######## Output of Script #########')
    path = "Mesh.obj"


    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)

if __name__ == "__main__":

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.


2 Answers 2


That example tutorial runs and returns an error if you haven't saved the example image in the tutorial to your home folder.

Texture paint is easy enough without python code being necessary, but this seems to be over-complicating what would be normal per face material and image texture assignment. The example tutorial provides no result that is of benefit other than to understand how the code works - but I may be biased toward using a paint or bake option instead.

    bpy.ops.object.mode_set(mode ='OBJECT', toggle = False)

    obj = bpy.context.selected_objects[0]
    view_layer = bpy.context.view_layer
    view_layer.objects.active = obj

    textImage = bpy.data.images.new(name=textName, width=512, height=512)
    bpy.context.scene.tool_settings.image_paint.mode = 'IMAGE'
    bpy.context.scene.tool_settings.image_paint.canvas = textImage
  • 1
    $\begingroup$ Giving some code alone without an explanation is not very helpful. Please edit your answer to include some details about what it is you've done in this code. $\endgroup$
    – PGmath
    Commented Mar 18, 2021 at 12:17

You must log in to answer this question.

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