Baking Ambient Occlusion for many OBJ-files

Hello BlenderArtists,

i’d like to bake AO maps for many OBJ files, so i thought of a python script to do this for me. The process just writes black images and i can’t figure out why. Now i need some help to figure out what is wrong in my blend-file and script.


import bpy
import os
import sys

print('');print('');print('');

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

# UVs richtig schön bauen

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

basepath = os.getcwd()
respath = os.path.join(basepath, 'test')

# welche?
baselistTxt = open(os.path.join(basepath, 'baselist.txt') )
baselist = baselistTxt.read()
baselistTxt.close()
print(baselist)
baselist = baselist.split('
')

# 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] #.add()
if 'wuff' in bpy.data.textures:
    matTex = bpy.data.textures['wuff']
else:
    matTex = bpy.data.textures.new( name='wuff', type='IMAGE' )

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

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

# walk baselist
for baseplate in baselist:

    # alles löschen
    for obj in bpy.data.objects:
        if obj.type == 'MESH' or obj.type == 'LAMP':
            obj.select = True
            bpy.ops.object.delete()
        
    # breite und tiefe
    bpWidth = baseplate[19:22]
    bpDepth = baseplate[23:26]
    print( 'Breite %s  - Tiefe %s' % (bpWidth, bpDepth) )
    
    # importieren
    bpFilepath = os.path.join(respath, baseplate, 'standardUV.obj')
    bpy.ops.import_scene.obj(filepath=bpFilepath)
    bpObj = bpy.context.selected_objects[0]
    print(bpObj)
    bpObj.select = True
    bpObjName = bpObj.name
    print(bpObjName)
    
    # aktiv setzen!!
    bpy.context.scene.objects.active = bpObj
#    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
    
    # lampe an
    lamp = bpy.data.objects.new( name="Lichto", object_data=lampData )    
    bpy.context.scene.objects.link( lamp )
    lamp.location = ( 0.05, -0.05, 1.0 )
    
    #mat zuweisen
    bpObj.data.materials.append(mat)
    
    
    # neues bild backen  
    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.mode_set(mode='OBJECT')
    #bpy.ops.mesh.uv_texture_add()
    bpy.ops.object.bake_image()
    
    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
    
    # export unter neuem namen
    tSelfFilename= os.path.join(respath, baseplate, 'self.jpg')
    
    img.file_format = 'JPEG'
    img.filepath_raw = tSelfFilename
    img.save()
    
    print(img.name)
    
    bpObj.select = True
    #bpy.ops.object.delete()
    


bakerman.zip (100 KB)