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$ Commented 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$
1
  • $\begingroup$ Works great :) thank you $\endgroup$ Commented Jun 8, 2022 at 16:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.