Bake to texture in python

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

karljj1
Posts: 5
Joined: Wed May 25, 2011 9:32 pm

Bake to texture in python

Postby karljj1 » Mon Jun 20, 2011 7:12 pm

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: Select all

   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. It is the ittle icon of an image with a drop down list of images that lets me select the new image. Any suggestions? When i hover my mouse over it the tool tip is 'Browse Images to be linkled', it does not show the python command in the tooltip :(


Thanks

underdoeg
Posts: 5
Joined: Thu Apr 29, 2010 3:51 am

Postby underdoeg » Wed Dec 21, 2011 8:15 pm

I'm struggling with the exact same problem.

Did you ever get it working?


So far I have

Code: Select all

bpy.data.screens['UV Editing'].areas[1].spaces[0].image = image

But I am not entirely sure if this is a good way to go?

Bhil
Posts: 1
Joined: Thu May 30, 2013 3:50 pm

Postby Bhil » Thu May 30, 2013 4:02 pm

underdoeg wrote:I'm struggling with the exact same problem.

Did you ever get it working?


So far I have

Code: Select all

bpy.data.screens['UV Editing'].areas[1].spaces[0].image = image

But I am not entirely sure if this is a good way to go?


Blender 2.6 example:

Code: Select all

# get first selected object (only as an example)
my_obj = bpy.context.selected_objects[0]

# create new image
uv_image = bpy.ops.image.new (
    name   = "empty",
    width  = 4096,
    height = 4096,
    color  = (1.0, 1.0, 1.0, 1.0),
    alpha  = False,
    )

# apply new image to uv
my_obj.data.uv_textures[0].data[0].image = uv_image


Be sure to have a uv map applied to your object. Otherwise the uv_textures[0] will not be available. You can insert any image for uv_image.
See here:
http://www.blender.org/documentation/bl ... image.open
and here:
http://www.blender.org/documentation/bl ... .image.new[/code]

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Thu May 30, 2013 5:37 pm

this is a two years old thread...

well, image assignment should actually be done per loop like

Code: Select all

img = bpy.data.images[0]

for uvface in bpy.context.object.data.uv_textures.active.data:
    uvface.image = img
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Python”

Who is online

Users browsing this forum: No registered users and 2 guests