Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

randomly pick objects from a list

randomly pick objects from a list

danispag
Enthusiast Enthusiast
1,720 Views
1 Reply
Message 1 of 2

randomly pick objects from a list

danispag
Enthusiast
Enthusiast

So I have a list with 5 objects in it. Based on vertex/face selection I can instance and attach 1 of these 5 objects to the surface of another object (for example a tile on a floor). My problem so far is that I want to use all 5 objects and randomly place them on my selection of vertices.

 

can someone help please?

I do really appreciate

 

my code so far:

 

from maya import cmds
import random as rand

tileSel = ['tile_01', 'tile_02', 'tile_03', 'tile_04', 'tile_05']

def init():
selectedItems = cmds.ls(sl=True, fl=True)
selectionSize = len(selectedItems)

if not selectedItems:
return #get out of the function and do nothing

else: #if either vertex, edges or faces are selected
performSelection(selectedItems)

def performSelection(selectedItems):

grp = cmds.group(em=True, n='Cloner_GRP')

if not selectedItems:
cmds.select(clear=False)
else:
cmds.select(clear=True)

for all in selectedItems:
cmds.select(all)
clst = cmds.cluster()
cloneObj = cmds.instance(cmds.ls(tileSel)) #instaning Custom Objects

selectedItems = cmds.xform(clst, q=True, rp=True)
cmds.xform(cloneObj, t=selectedItems, ws=True)

# Constrain cloneObj to floor at the closest point.
gConst = cmds.geometryConstraint( 'floor', cloneObj )
cmds.delete(gConst)

# Constrain cloneObj to floor at the face normals.
nConst = cmds.normalConstraint('roof', cloneObj, worldUpType="scene", aimVector=(0, 1, 0), upVector=(0, 1, 1), weight=1)
cmds.delete(nConst)

cmds.delete(clst)
cmds.parent(cloneObj, grp)

init()

0 Likes
1,721 Views
1 Reply
Reply (1)
Message 2 of 2

g2m.agent
Collaborator
Collaborator

 

import random
a=["a","b","c"]
random.choice(a)
# result: a or b or c

 

 
0 Likes