Mesh name = object name

hello , I am a total beginner in scripting for Blender and I tried to do a script that for all selected items makes the object name equal to the mesh name (I have a lot of objects in the scene and I need to automate this ).
I tried to create a really simple script

Blockquote
import bpy
obj = bpy.context.view_layer.objects.active

for obj in bpy.data.objects
objects.name = meshes.name

Blockquote

but i’t snot working, and I can’t understand why .

any help would be really appreciated !

Try this snippet:

import bpy
for obj in bpy.context.selected_objects:
  if obj.type == 'MESH':
    obj.name = obj.data.name

thanks! in fact I found the issue, I was using a good method but for pre-2.80 era. I redid the script , using the recent API and it works perfectly now

import bpy


for ob in bpy.context.selected_objects:
    
    bpy.context.view_layer.objects.active = ob
    bpy.context.object.data.name = bpy.context.object.name
    

so I found the solution ,I was using obsolete API commands, I leave it here in case you guys need these 2 lines for your projects! happy blending :slight_smile:

You dont have to change active object for name changes.

import bpy

for ob in bpy.context.selected_objects:
   ob.data.name = ob.name