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?



1 Answer 1


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()
  • 3
    $\begingroup$ You may also want to .copy() the object's .data. $\endgroup$
    – Robert Gützkow
    Sep 17, 2019 at 7:51
  • $\begingroup$ You can also use random.randint(0,objNum - 1). $\endgroup$ Sep 17, 2019 at 7:51
  • $\begingroup$ Since I am using a hit raycast, can I duplicate all objects & assign locations to equal the hit value? $\endgroup$ Sep 17, 2019 at 7:58
  • $\begingroup$ Thanks! I will try it out right now. $\endgroup$ Sep 17, 2019 at 7:58
  • $\begingroup$ @Animationsvfx you have to give us a lot more details to understand what you need $\endgroup$
    – Tareyes
    Sep 17, 2019 at 8:01

You must log in to answer this question.

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