Skip to content

Instantly share code, notes, and snippets.

@patwooky
Created August 9, 2017 17:34
Show Gist options
  • Save patwooky/49a562c170ed980eca26cc2e7cb3937c to your computer and use it in GitHub Desktop.
Save patwooky/49a562c170ed980eca26cc2e7cb3937c to your computer and use it in GitHub Desktop.
Creates a hierarchy of with configurable depth, and random number of objects in Maya
'''
makeRandomHierarchy
by Patrick Woo
v001_01
20170809
Creates a hierarchy of with configurable depth, and random number of objects in Maya
'''
from pymel.core import *
def makeRandomHierarchy(name='randomHierarchy_grp', levels=5, objsPerLevel=(1,5)):
'''
Maya script
creates a hierarchy of objects in Maya
levels - <int> specify the depth of the groups level
objsPerLevel - <tuple> (min, max) specifies how many objects should be in each level
'''
from random import randint
import string
from functools import partial
def makeRandomStr(length=(1, 10)):
'''
crates a random string
length - <tuple> <min, max> minimum number and maximum number of letters to generate
'''
return ''.join([letters[randint(length[0], length[1]-1)] for x in range(randint(length[0], length[1]))] )
letters = string.ascii_letters
lettersLen = len(letters)
topGrpNode = group(name=name, empty=True)
randomPrims = (polyCube, polySphere,polyCylinder,polyPlane)
grpsCount = 1
objsCount = 0
levelGrpList = []
for whichLevel in range(levels):
randName = makeRandomStr(length=(3, 8))
thisGrp = group(empty=True, name='level_{}_{}'.format(whichLevel, randName))
grpsCount += 1
if whichLevel == 0:
thisGrp.setParent(topGrpNode)
else:
thisGrp.setParent(levelGrpList[whichLevel-1])
levelGrpList.append(thisGrp.longName())
for objId in range(0, randint(objsPerLevel[0],objsPerLevel[1])):
randObjName = 'lvl{}_obj{}_{}'.format(whichLevel, objId, makeRandomStr(length=(3, 8)))
# print 'randObjName is', randObjName
thisObj = partial(randomPrims[randint(0,len(randomPrims)-1)], constructionHistory=False).__call__()
thisObj = PyNode(thisObj[0])
thisObj.rename(randObjName)
thisObj.setParent(thisGrp)
objsCount += 1
print 'completed. created {} groups & {} objects'.format(grpsCount, objsCount)
print 'top group node is', PyNode(topGrpNode).longName()
return PyNode(topGrpNode).longName()
makeRandomHierarchy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment