0
$\begingroup$

I'm trying to speed up the tedious process of baking in blender (without addons).

So far, I've managed to:

  • Add a new UVMap to all selected objects;
  • Set this new UV as active;
  • Create a new image for the bake, and save it;

What I have not been able to do:

  • Create a new Image node inside every material of every selected objects; (all questions on StackExchange are about all materials in the scene instead of only of selected objects, or selecting a specific material by name, which would be impractical here).
  • Insert the newly created Image into the newly created image node of every selected object's materials;
  • Set these nodes as active, for the bake to work.

If anyone can help, I'd appreciate!

What I have so far:

import bpy

#Create new bake UVMap and set it as active
for obj in bpy.context.selected_objects:
    # Make sure this is a mesh object and that it doesn't already use this UV Map
    if obj.type == 'MESH' and "bake" not in obj.data.uv_layers:
        obj.data.uv_layers.new(name="bake")
    obj.data.uv_layers['bake'].active = True

#create new image and save it 
image = bpy.data.images.new("Bake", alpha=True, width=1024, height=1024)
image.alpha_mode = 'STRAIGHT'
image.filepath_raw = "//Bake.png"
image.file_format = 'PNG'
image.save()

#This is the part I'm not managing to make it work        
C = bpy.context
eachmat = set(slot.material for o in C.selected_objects for slot in o.material_slots)
for eachmat in bpy.context.selected_objects:
    if eachMat.node_tree is not None:
        nodes = eachmat.node_tree.nodes if eachmat.node_tree else []
        node_image = nodes.new(type='ShaderNodeTexImage')
        node_image.image = bpy.data.images['Bake.png']
        node_image.location = 0,0

enter code here

$\endgroup$

1 Answer 1

1
$\begingroup$

The amazing Carlos Eduardo (https://twitter.com/carlosedubarre) found the solutions for the second part of the script, here is the code:

import bpy

#Create new bake UVMap and set it as active
for obj in bpy.context.selected_objects:
    # Make sure this is a mesh object and that it doesn't already use this UV Map
    if obj.type == 'MESH' and "bake" not in obj.data.uv_layers:
        obj.data.uv_layers.new(name="bake")
    obj.data.uv_layers['bake'].active = True

#create new image and save it 
image = bpy.data.images.new("Bake", alpha=True, width=1024, height=1024)
image.alpha_mode = 'STRAIGHT'
image.filepath_raw = "//Bake.png"
image.file_format = 'PNG'
image.save()

#loop inside list of materials from the selected objects
for object in bpy.context.selected_objects:
    for mat in object.data.materials:
        #activate use of nodes
        mat.use_nodes = True
        nodes = mat.node_tree.nodes
        #create new image texture node
        node = nodes.new('ShaderNodeTexImage')
        #node location in the shader editor
        node.location = (-500,100) 
        #use image in the image node = bpy.data.images
        node.image = bpy.data.images['Bake']
        #set node as active/selected, for baking
        node.select = True
$\endgroup$
1
  • $\begingroup$ LOL, thanks a lot @Arkhangels. it was a cool code to play $\endgroup$ Commented Mar 1, 2022 at 18:37

You must log in to answer this question.

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