- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
from maya import cmds as mc
import random
def buildSkyscraper(numFloors):
#Checks for and deletes existing building
if mc.objExists('Building_GRP'):
mc.delete('Building_GRP')
#creates a list of base objects
base = mc.ls(selection = True)
#greats a group for floors of building
floorGRP = mc.group(n = 'Building_GRP'.format(base), em = True)
mc.select(base)
selList = mc.ls(selection = True)
#creates a copy of the object,distributes each object in the tower
for f in range(numFloors):
obj = mc.duplicate(random.choice(selList), n = 'Building_floor{1}'.format(selList, (f + 1)))
bbox = mc.exactWorldBoundingBox()
yMove = (bbox[4] - bbox[1])
moveIt = (yMove * (f))
mc.setAttr('%s.translateY' % (obj[0]), moveIt)
my_list = mc.ls('*Building_floor*', tr = True)
#parents object under building group
for m in my_list:
mc.parent(m, floorGRP)
#hides original objects
for s in selList:
mc.hide(s)
buildSkyscraper(15)
Hi All,
I have written the above code as an exercise in using bounding box information to distribute objects in a stack or tower. It works as is, using any number of "base" objects and randomly distributing them to the value specified IF all of the objects are the same height.
I have tried a few ways to manipulate this code so that if I have objects of different heights that they stack themselves on top of the last "floor" created. I have run into a few issues sorting this out. I can code it to do this but only if I make a few assumptions to make the math work around these assumptions. I would prefer to write the code that does not assume what the users "base" objects may be and can work with any geometry selected.
Thanks for your help, any suggestions are greatly appreciated.
Gabs
Solved! Go to Solution.