Results 1 to 11 of 11
  1. #1

    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:

    Code: 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



  2. #2
    Member Crouch's Avatar
    Join Date
    Oct 2004
    Location
    the Netherlands
    Posts
    1,412
    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



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



  6. #6
    Solved it!

    bpy.data.screens['UV Editing'].areas[1].spaces[0].image = bpy.data.images['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!

    thanks
    happy 2.5
    Wnd 8.1 64 bits / Thanks and a Happy 2.72 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 = 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 ) > 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



  9. #9
    Originally Posted by karljj1 View Post
    bpy.data.screens['UV Editing'].areas[1].spaces[0].image = bpy.data.images[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
    Location
    Ohio
    Posts
    11,247
    Here is how I am doing image sequences via code for my new Space Battle AddOn.

    Code:
    if MANAGE_IMAGE_SEQUENCE == True:
        # Create image texture from image
        temp_tex = bpy.data.textures.get(texture_name)
        if temp_tex == None:
            temp_tex = bpy.data.textures.new(texture_name, 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)
        try:
            temp_tex.image = bpy.data.images.load(realpath)                      # 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)
        except:
            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
        temp_ob.data.uv_textures.new(name=uv_name)
        if temp_tex.image != None:
            temp_ob.data.uv_textures[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.
    OSX Mac Mini i7 @ 2.6ghz 16Gb Ram (no GPU)
    Windows 7 8Gb, GeForce GTX 660 2Gb Ram AMD Hexcore @ 2.7Ghz.
    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
  •