# How to randomly pick an object from a collection?

I am working on a modal that paints objects & then drops them using rigid body. I'd like to include support for collections, where the user can store a group of objects as a collection & then randomly choose one of those objects to paint.

how can I access objects in a collection & duplicate the chosen object?

Thanks

Use this code:

import bpy
from random import random

#select your collection by name (in this case the collection named "Collection")
col = bpy.data.collections["Collection"]

objNum = len(col.objects)

#Generate random number
randomIndex = round(random()*(objNum-1))
#use the random number to call a random object in the collection
obj = col.objects[randomIndex]

#creat a copy of the object and link it to the collection
newObj = obj.copy()

• You may also want to .copy() the object's .data. Sep 17 '19 at 7:51
• You can also use random.randint(0,objNum - 1). Sep 17 '19 at 7:51