Results 1 to 11 of 11
  1. #1

    Assign image to mesh uv in python


    I am trying to bake all the textures of a model into a single texture. I have managed to do this manually and am now trying to write a script to do this. However some of the blender operations do not show me the python calls so i am a little stuck. Here is what i do so far:

    Code: def CreateAtlas() :

    bpy.ops.uv.smart_project( angle_limit = 66, island_margin = 0 )
    img = name='Atlas', width = 1024, height = 1024 )
    #How do i assign the image to the uvs???
    bpy.context.scene.render.bake_type = 'TEXTURE' # Set to bake texures only

    After i uv project i need to change the image that is used to a new image however i do not know the python command to do this. To do this manually i select my new image from the drop down list "Browse Images to be linked"', Does anyone know the python command for this??


  2. #2
    Member Crouch's Avatar
    Join Date
    Oct 2004
    the Netherlands
    You could take a look at my Texture Paint Plus add-on, specifically the Consolidate images option. It consolidates all image textures of an object into a single image. I think it's very similar to what you're trying to do (minus the original unwrapping). In the code you should look at the functions whose name start with consolidate_

    consolidate_copy_images() creates a new image file using
    consolidate_update_textures() changes the image used by a texture: texture.image = new_image

  3. #3
    thanks crouch. I will take a look at your scripts. Its strange if i create a new image in the editor it automatically assigns it to the UV's however if i create a new image in script it does nothing.

  4. #4
    Are you in the right mode?

    [NON CODE]
    If I create a new image and i'm not in Edit Mode, it doesn't assign it.

  5. #5
    I am enabling edit mode in the script which is the mode i am in when i do it manually. If i hover over the button it says the command is, if i press the button a new image is created and appears in ther uv window, however if i call the python command the texture is created but does not change the uv window. I need to assign it to the uv window but have no idea how. I have been working on it all day

  6. #6
    Solved it!['UV Editing'].areas[1].spaces[0].image =['My Texture']

    Wow what a nightmare!

  7. #7
    this seemst itnersting but for other people

    can you make a complete little sample script showing this assignment

    that would help other people and me too!

    happy 2.5
    Nice 3D models = 25 % K.I.S.S + 25 % common sense + 25 % Inspiration + 25 % Knowledge
    Win 10 64 bits / Thanks and a Happy 2.76b with Cycles

  8. #8
    Here is a cut down version of my script, it will probably need editing. by default it expects a single object in the scene which is made up of many textures. It will combine all the textures into a new single texture called an Atlas. You can then save this new texture out and save the model file. with it.
    I have used it to automaitcally process 11,000 files which took about 11 hours~ to reduce the complexity so they run in real time.

    def CreateAtlas() :

    scene =[0]

    key = 'Atlas'

    # Change context
    oldCon = bpy.context.area.type
    bpy.context.area.type = 'CONSOLE'

    bpy.ops.uv.smart_project( angle_limit = 66 ) name='Atlas', width = 1024, height = 1024, )['UV Editing'].areas[1].spaces[0].image =[key] # Set the new image to the new UV texture
    bpy.context.scene.render.bake_type = 'TEXTURE' # Set to bake texures only

    # Remove old materials
    while len([0].data.materials ) > 0:

    # Remove old texture layer[0].data.uv_textures[0].active = True
    bpy.context.area.type = oldCon

  9. #9
    Originally Posted by karljj1 View Post['UV Editing'].areas[1].spaces[0].image =[key] # Set the new image to the new UV texture
    seems the new blender (2.65) does not have image as attribute, any idea how to make it work now?

    I'm trying to apply the uv texture to my surface but nothing worked so far...

  10. #10
    Member Atom's Avatar
    Join Date
    Jan 2006
    Here is how I am doing image sequences via code for my new Space Battle AddOn.

        # Create image texture from image
        temp_tex =
        if temp_tex == None:
            temp_tex =, type = 'IMAGE')
        #realpath = os.path.expanduser('~/snippets/textures/color.png')
        # Pick a random explosion sequence. HARD CODED FILENAMES!
        the_path = "E:\\Documents and Settings\\Admin\\My Documents\\"    #Renderbox
        the_path = "C:\\Users\\Overseer\\Documents\\Maps"                #Laptop.
        explosion_map_index = random.randint(0,5)
        realpath = "%sMaps\\Animated\\fireball-3_large\\fireball-3_large_0001.png" % the_path
        explosion_duration = 90     # Set length of image sequence, in frame, here.
        if explosion_map_index == 1:
            realpath = "%sMaps\\Animated\\explosion-5\\explosion-5_0001.tif"  % the_path
            explosion_duration = 64     # Set length of image sequence, in frame, here.
        ex_dur_middle = int(explosion_duration/3)
            temp_tex.image =                      # Load the file, may need try/except to handle file not found.
            temp_tex.image.use_premultiply = True
            temp_tex.image.source = 'SEQUENCE'   
            temp_tex.image_user.frame_duration = explosion_duration
            temp_tex.image_user.frame_start = end_frame
            temp_tex.image_user.frame_offset = 0
            temp_tex.image_user.use_cyclic = False
            temp_tex.image_user.use_auto_refresh = True
            temp_mat.diffuse_color = (0.0,1.0,0.0)
            print("trace_missile_particles: Failure to load texture file [%s]." % realpath)
            temp_mat.diffuse_color = (1.0,1.0,0.0)
        # Add texture slot for image texture.
        temp_ts = temp_mat.texture_slots.add()
        temp_ts.texture = temp_tex
        temp_ts.texture_coords = 'UV'
        temp_ts.use_map_color_diffuse = True 
        temp_ts.use_map_color_emission = True 
        temp_ts.emission_color_factor = 1.0
        temp_ts.use_map_density = True 
        temp_ts.mapping = 'FLAT'
        # UV map the plane for rendering, not viewport.
        uv_name = "uv_explosion%s" % post_fix
        if temp_tex.image != None:
  [0].data[0].image = temp_tex.image
    This way you don't have to worry about edit modes, the context or bpy.ops screwing you down the line come render time.
    No reason to use Blender anymore.
    Houdini Indie is only $200.00
    Atom's Links Page

  11. #11
    Another advantage of doing this way(assigning via texture) is that you don't have to go looking for the UV/Image editor screen, you don't even have to have one open. The other way will give you errors if you don't have a UV/Image editor open already.
    "Yeah, well, some people meditate, some people get massages. I blend." -Will Smith in Enemy of the State
    AddOn:Curly Curves | AddOn:Bevel/Taper Curves

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts