Apply operation on all objects in collection - python


I have a collection with parts. And I want to replace the mesh data of each object. For that, I use the name of the object and remove the last four characters. (.001, .002, etc).

I made a code, and that works, but only works on the active mesh:

import bpy
for obj in[‘Assets’].all_objects: =[:-4]

What I need is that all the objects need to have it’s mesh data replaced, and not one object from the collection. In other words, how to iterate through a collection and apply operation on all objects within a collection?

what you can try to do instead of using context, is using data:

for obj in myCollection:
  name =[name].name = name[:-4]

The other methode is to change the active object when iterating:

for obj in myCollection: = obj =[:-4]

It seems there is also an operator bpy.ops.object.rename_object but i have no clue on the utilization

Thanks, a step further now.
I tried it on a collection with 6 objects. The only strange thing is that only four of them got their mesh data replaced. Note that I take the, remove 4 characters and use that for
I will test again, and see why not all mesh data got replaced.

import bpy
myCollection =[‘Assets’].all_objects
for obj in myCollection: = obj =[:-4]

update: awh, I am also renaming the mesh data, I should replace it like: =[‘Plane.050’]


So got it working:

> import bpy
> myCollection =['Assets'].all_objects
> for obj in myCollection:
> = obj
>     a =[:-4]
> =[a]
1 Like

It’s my fault, i made a small mistake.
It should be:

for obj in myCollection: = obj

  #Renames object itself =[:-4]
  #Renames object data (mesh, curve and stuff) =[:-4]
1 Like