# Select object from within collection

Can anyone tell me how I can choose an arbitrary object from within a collection? My script leaves me with nothing active (or selected) at one point, but I need something to be active.

I have the collection name from this:

Coll = bpy.data.collections.get('Structure')


So I can do something like this:

if Coll:
for obj in Coll.objects:
obj.select_set(True)


Which makes each object active one at a time, leaving me with the last one active at the end of the loop. But that seems pretty heavy handed. There must be a neater way.

from random import choice

• Alternatively after choice and without deselecting all first for obj in Coll.objects: obj.select_set(obj is rnd_obj) and to set as active context.view_layer.objects.active = rnd_obj – batFINGER May 1 '19 at 12:41