0
$\begingroup$

I have 2 collections in a scene (collection, items) both with objects (meshes, camera, lights). How can I select all objecst of type mesh from items and delete them?

bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

With that I can delete all from the scene but I want to limit it to delete all meshes from items. Probably I can if the collection name and type mesh but maybe there is a simpler way?

$\endgroup$
1
  • $\begingroup$ @Ray Mairlot, i'm actually trying to convert your Slider Puzzle script from 2014 to blender 2.8 $\endgroup$
    – maclaud
    Sep 25, 2019 at 21:45

1 Answer 1

3
$\begingroup$

Don't think there is a simpler way (as you mentioned in the question).

Globally:

  • Get the collection
  • Iterate over mesh objects in the collection
  • Keep its inner mesh data
  • Remove the object

Then (optional):

  • Iterate over the collected meshes
  • Look if it has became orphean (no user)
  • If yes, delete it

Commented code:

import bpy

collection_name = "items"

# Get the collection from its name
collection = bpy.data.collections[collection_name]

# Will collect meshes from delete objects
meshes = set()

# Get objects in the collection if they are meshes
for obj in [o for o in collection.objects if o.type == 'MESH']:
    # Store the internal mesh
    meshes.add( obj.data )
    # Delete the object
    bpy.data.objects.remove( obj )

# Look at meshes that are orphean after objects removal
for mesh in [m for m in meshes if m.users == 0]:
    # Delete the meshes
    bpy.data.meshes.remove( mesh )
$\endgroup$
2
  • 1
    $\begingroup$ thanks a lot, that work great for me $\endgroup$
    – maclaud
    Sep 25, 2019 at 15:50
  • $\begingroup$ Works great :) thank you $\endgroup$
    – ofekp
    Jun 8, 2022 at 16:02

You must log in to answer this question.

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