3

I am trying to bake image from the script in cycles mode. In my node structure, since I am baking in cycles engine, I am adding an extra image texture node, with image input of a newly created image (named uv_image) and I am making this uv_image texture node my active node. Now I select the object and do a bake using bpy.ops.object.bake(). Here is the code I am executing :

    uv_image = "UV_image"
    bpy.data.images.new(name=uv_image,width = 1024,height = 1024)

    mat = bpy.data.materials[material.name]

    nodes = mat.node_tree.nodes
    node = nodes.new('ShaderNodeTexImage')
    node.location = 300,0
    node.label = 'uv_image'
    node.name = 'uv_image'
    node.image = bpy.data.images[uv_image]
    uv_img = bpy.data.images[uv_image]
    node.select = True
    nodes.active = node
    for area in bpy.context.screen.areas :
        if area.type == 'IMAGE_EDITOR' :
            area.spaces.active.image = uv_img

    for ob in bpy.data.objects:
        if 'pattern' in ob.name: #object of interest has the name pattern)
            ob.select = True
            bpy.context.scene.objects.active = ob
        else:
            ob.select = False   

    bpy.ops.object.bake(type='COMBINED')

The output baked image is blank when I am doing this from the script without any texture baked and it happens instantaneously. (Usually bake takes a few seconds) But when I execute the code without the bpy.ops.object.bake() statement and hit the bake button in GUI, the baked image is perfect. Another observation is that, once I bake in GUI, from then on, when I execute and bake from script, it works. So, everytime I need to bake it in GUI once to get this code working from script. It is really frustating!

EDIT 1:

By using bpy.ops.object.bake('INVOKE_DEFAULT', type='COMBINED'), I can see the texture getting baked well on the uv_image but when I tried to save the baked image through the following lines of code which follow the bake statement, I get a blank image in the destination folder. I think image is getting saved before it is getting updated with the baked texture. I tried to include image.update() before saving but that did not help.

 for image in bpy.data.images:
    if "UV_" in image.name:

        image = bpy.data.images[image.name]
        scene=bpy.context.scene
        scene.render.image_settings.file_format='JPEG'

        image_path = img_path + str(cg)+"_"+image.name+".jpg"
        image.save_render(image_path,scene)

EDIT 2:

If I add a time delay in the following way:

import time
time.sleep(60)

and I wait till the bake is complete and then save, then the image is saved after the entire bake. So, I am guessing that INVOKE_DEFAULT runs the baking in parallel without blocking the image being baked? Is this correct? Can someone help me find the documentation for INVOKE_DEFAULT along with other parameters ?

Sum-Al
  • 567
  • 7
  • 18
  • I have tried your script and it works... so there may be another contextual reason if you don't obtain the result ? – lemon Jul 14 '16 at 09:32
  • Yea, I think so too. Any idea how to get the same context from code? – Sum-Al Jul 14 '16 at 10:55
  • No... can this help you to compare ? – lemon Jul 14 '16 at 11:01
  • 2
    Try bpy.ops.object.bake('INVOKE_DEFAULT', type='COMBINED') – batFINGER Jul 14 '16 at 14:26
  • Seems to work for now. Thanks! I'll run it more and update the status again in a couple of days. – Sum-Al Jul 15 '16 at 07:14
  • Hey, I am encountering a problem. I follow the above code, then do a bake using bpy.ops.object.bake('INVOKE_DEFAULT', type='COMBINED') and then save the baked image in the script. When I run the script, the blank image is getting saved before it is getting baked. So, in GUI, the UV_image is getting baked well, but the image is saved before it is baked. – Sum-Al Jul 18 '16 at 10:10
  • Hi ! Have you enhanced/modified your script since your last message ? It could be very usefull for my actual problem, even in this actual state (http://blender.stackexchange.com/questions/60249/batch-selected-to-active-baking) :) – Vinc3r Aug 04 '16 at 08:38

0 Answers0