1
$\begingroup$

i'm trying to bake ambient occlusion maps for combinations of parts in a python script. While in another blend-file the baking works, i get a "No objects or images found to bake to" error and don't know what to do about it. The important code block is marked below. The for-loop just goes through the filenames written in baselist.txt and loads the Wavefront OBJ-files.

import bpy
import os
import sys

bpy.ops.object.mode_set(mode='OBJECT')

# initial values
bpy.context.scene.render.bake_type = 'AO'
bpy.data.worlds[0].light_settings.samples = 4

basepath = os.path.join('C://', 'Users', 'bassmas', 'Desktop', 'Bakerman')
respath = os.path.join(basepath, 'resources')
baselistTxt = open(os.path.join(basepath, 'baselist.txt') )
baselist = baselistTxt.read()
baselistTxt.close()
baselist = baselist.split('\n')

# beleuchtung
lampData = bpy.data.lamps.new( name="Licht", type='SUN' )

#material
if 'einheitsmaterial' in bpy.data.materials:
    mat = bpy.data.materials['einheitsmaterial']
else:
    mat = bpy.data.materials.new('einheitsmaterial')
mat.diffuse_color = (1, 1, 1)

if 'selfAO' in bpy.data.images:
    img = bpy.data.images['selfAO']
else:
    img = bpy.ops.image.new( name='selfAO', width=256, height=256 )


# for each OBJ-File
for baseplate in baselist:

    # alles löschen
    for obj in bpy.data.objects:
        if obj.type == 'MESH':
            obj.select = True
            bpy.ops.object.delete()

    # import OBJ
    bpFilepath = os.path.join(respath, baseplate, 'standardUV.obj')
    bpy.ops.import_scene.obj(filepath=bpFilepath)
    bpObj = bpy.context.selected_objects[0]
    bpObj.select = True

    # set active
    bpy.context.scene.objects.active = bpObj
    bpy.ops.object.mode_set(mode='OBJECT')

    # switch on lamp
    lamp = bpy.data.objects.new( name="Lichto", object_data=lampData )    
    bpy.context.scene.objects.link( lamp )
    lamp.location = ( 0.05, -0.05, 1.0 )

    # assign material
    bpObj.data.materials.append(mat)    

    ##########################################
    # NOW WHAT IS THE WAY TO GO HERE?
    img = bpy.data.images['selfAO']
    bpObj.data.uv_textures[0].data[0].image = img
    #bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.data.screens['UV Editing'].areas[1].spaces[0].image = img 
    #bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.mesh.uv_texture_add()
    bpy.ops.object.bake_image()
    ##########################################

    # export unter neuem namen
    tSelfFilename= os.path.join(respath, baseplate, 'self.jpg')    
    img.file_format = 'JPEG'
    img.filepath_raw = tSelfFilename
    img.save()

How to set the active texture/image from within the python script?

$\endgroup$

1 Answer 1

0
$\begingroup$

I've solved it by adding a texture to the material, seems this has done the deal. I had to switch to "generated" image source. Now some updated code is here:

import bpy
import os
import sys

bpy.ops.object.mode_set(mode='OBJECT')

# initial values
bpy.context.scene.render.bake_type = 'AO'
bpy.data.worlds[0].light_settings.samples = 4

basepath = os.getcwd()
respath = os.path.join(basepath, 'resources')
baselistTxt = open(os.path.join(basepath, 'baselist.txt') )
baselist = baselistTxt.read()
baselistTxt.close()
baselist = baselist.split('\n')

# beleuchtung
lampData = bpy.data.lamps.new( name="Licht", type='SUN' )

#material
if 'einheitsmaterial' in bpy.data.materials:
    mat = bpy.data.materials['einheitsmaterial']
else:
    mat = bpy.data.materials.new('einheitsmaterial')
mat.diffuse_color = (1, 1, 1)

matTexSlot = mat.texture_slots[0]
if 'AOTexture' in bpy.data.textures:
    matTex = bpy.data.textures['AOTexture']
else:
    matTex = bpy.data.textures.new( name='AOTexture', type='IMAGE' )

if 'selfAO' in bpy.data.images:
    img = bpy.data.images['selfAO']
else:
    img = bpy.ops.image.new( name='selfAO', width=256, height=256 )
img.source = 'GENERATED'

matTex.image = imgSelf
matTexSlot.texture = matTex
matTexSlot.texture_coords = 'UV'
matTexSlot.mapping = 'FLAT'

# for each OBJ-File
for baseplate in baselist:

    # alles löschen
    for obj in bpy.data.objects:
        if obj.type == 'MESH':
            obj.select = True
            bpy.ops.object.delete()

    # import OBJ
    bpFilepath = os.path.join(respath, baseplate, 'standardUV.obj')
    bpy.ops.import_scene.obj(filepath=bpFilepath)
    bpObj = bpy.context.selected_objects[0]
    bpObj.select = True

    # set active
    bpy.context.scene.objects.active = bpObj
    bpy.ops.object.mode_set(mode='OBJECT')

    # switch on lamp
    lamp = bpy.data.objects.new( name="Lichto", object_data=lampData )    
    bpy.context.scene.objects.link( lamp )
    lamp.location = ( 0.05, -0.05, 1.0 )

    # assign material
    bpObj.data.materials.append(mat)    

    ##########################################
    # NOW WHAT IS THE WAY TO GO HERE?
    img = bpy.data.images['selfAO']
    bpObj.data.uv_textures[0].data[0].image = img
    bpy.ops.object.mode_set(mode = 'EDIT', toggle=False)
    bpy.data.screens['UV Editing'].areas[1].spaces[0].image = img
    bpy.ops.object.bake_image()
    #bpy.ops.object.mode_set(mode='OBJECT')
    ##########################################

    # export unter neuem namen
    tSelfFilename= os.path.join(respath, baseplate, 'self.jpg')    
    img.file_format = 'JPEG'
    img.filepath_raw = tSelfFilename
    img.save()
$\endgroup$

You must log in to answer this question.

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