It is possible repeat a set of commands for multiple objects? I have 645 objects and I would like to repeat the same commands for each one.
I followed this tutorial:
Is there a way to repeat a set of actions on an object?
But the script gives me an error.
    import bpy

bpy.ops.material.new()
bpy.ops.node.add_node(type=&quot;ShaderNodeAttribute&quot;, use_transform=True)
bpy.ops.node.translate_attach_remove_on_cancel(TRANSFORM_OT_translate={&quot;value&quot;:(-1405.21, -1784.65, 0), &quot;orient_type&quot;:'GLOBAL', &quot;orient_matrix&quot;:((1, 0, 0), (0, 1, 0), (0, 0, 1)), &quot;orient_matrix_type&quot;:'GLOBAL', &quot;constraint_axis&quot;:(False, False, False), &quot;mirror&quot;:True, &quot;use_proportional_edit&quot;:False, &quot;proportional_edit_falloff&quot;:'SMOOTH', &quot;proportional_size&quot;:1, &quot;use_proportional_connected&quot;:False, &quot;use_proportional_projected&quot;:False, &quot;snap&quot;:False, &quot;snap_target&quot;:'CLOSEST', &quot;snap_point&quot;:(0, 0, 0), &quot;snap_align&quot;:False, &quot;snap_normal&quot;:(0, 0, 0), &quot;gpencil_strokes&quot;:False, &quot;cursor_transform&quot;:False, &quot;texture_space&quot;:False, &quot;remove_on_cancel&quot;:True, &quot;release_confirm&quot;:False, &quot;use_accurate&quot;:False, &quot;use_automerge_and_split&quot;:False}, NODE_OT_attach={}, NODE_OT_insert_offset={})
bpy.data.materials[&quot;Materiale&quot;].node_tree.nodes[&quot;Attributo&quot;].attribute_name = &quot;Col&quot;
bpy.ops.node.link(detach=False)

So I run these commands for one object, then select the others and run the script, but it gives me an error.
I don't know how to use Blender or Python well but I need it for my thesis work.
Thanks to anyone who wants to help me.
Using bpy.ops is really not recommended, as it needs context. The context will be wrong.
I suggest this instead:
If you want to create only 1 data block of a material:
import bpy

scene = bpy.context.scene

mat = bpy.data.materials.new(&quot;My Material&quot;)
mat.use_nodes = True
nodes = mat.node_tree.nodes
node_attribute = nodes.new(type=&quot;ShaderNodeAttribute&quot;)
node_attribute.location = (-1405.21, -1784.65)
node_attribute.attribute_name = &quot;Col&quot;

for o in scene.objects:
    if o.type =='MESH':
        o.data.materials.append(mat)

If you want to create a new material for each object instead:
import bpy

scene = bpy.context.scene

for idx ,o in enumerate(scene.objects):
    if o.type == 'MESH': 
        mat = bpy.data.materials.new(&quot;My Material_&quot;+str(idx)) #to make your personal index into name (if you want)
        mat.use_nodes = True
        nodes = mat.node_tree.nodes
        node_attribute = nodes.new(type=&quot;ShaderNodeAttribute&quot;)
        node_attribute.location = (-1405.21, -1784.65)
        node_attribute.attribute_name = &quot;Col&quot;
        
        o.data.materials.append(mat)

Note:
If you want work only on selected objects , you have to replace for o in scene.objects:with for o in bpy.context.selected_objects: