Assign image to mesh uv in python

Hello,

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:

  <b>Code:</b>			     def CreateAtlas() :    
   
  bpy.ops.mesh.uv_texture_add()       
  bpy.ops.object.editmode_toggle() 
  bpy.ops.uv.smart_project( angle_limit = 66, island_margin = 0 ) 
  img = bpy.ops.image.new( 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 
  bpy.ops.object.bake_image()	 

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

Thanks

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 bpy.data.images.new()
consolidate_update_textures() changes the image used by a texture: texture.image = new_image

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

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.

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 bpy.ops.image.new, 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 :frowning:

Solved it!

bpy.data.screens[‘UV Editing’].areas[1].spaces[0].image = bpy.data.images[‘My Texture’]

Wow what a nightmare!

:smiley:

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!

thanks
happy 2.5

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 = bpy.data.scenes[0]

key = 'Atlas'    

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

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

# Remove old materials        
while len( bpy.data.objects[0].data.materials ) &gt; 0:    
    bpy.ops.object.material_slot_remove()                    

# Remove old texture layer                
bpy.data.objects[0].data.uv_textures[0].active = True        
bpy.ops.mesh.uv_texture_remove()
bpy.context.area.type = oldCon

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…

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.