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