What am I missing here.

46 views
Skip to first unread message

jettam

unread,
Sep 14, 2017, 6:56:17 PM9/14/17
to Python Programming for Autodesk Maya
I am trying to write a function that will first make a spiral staircase, and then distribute the staircase randomly.  I can get this script to generate only one spiral staircase, but it stops there. I can't get it to duplicate and distribute multiples of these. I have attached a logic path that I think the script should follow A,B,C,D (see attached image) Am I missing something ? 



Here is the code:
import maya.cmds as mc
import random

def distributeObjects(stairs=None):
   
''' This will make and randomly distribute a spiralStaircase '''
   
    checkTest
= 0
   
   
def makestairs():
       
''' first make one spiral staircase '''
       
# make a dictionary
        steps
= {'name':'Step','slide':5,'rotations':10,'spacing':1,'stepDepth':2,'stepHeight':.75}
        stepName
=steps.get('name')
       
# make masterGroup
        groupAllSteps
= mc.group(name="Group"+stepName+"s", empty=True)
       
for x in range (0,100):  
           
#make stairs
           
# make step
            stepObj
= mc.polyCube(name=stepName+"#", w=5, h=steps.get('stepHeight'), d=steps.get('stepDepth'), ch=0)
           
# make group
            stepGrp
= mc.group(empty=True, name=stepName+'_Grp#')
           
# parent stepObj to stepGrp
            mc
.parent(stepObj, stepGrp)
           
# rotate stepGrp
            mc
.xform(stepGrp, ro=(0,(steps.get('rotations')*x),0))
           
# transform stepObj
            mc
.xform(stepObj[0], t=(steps.get('slide'),steps.get('stepHeight')*steps.get('spacing')*x,0))
           
# parent stepObj to masterGroup
            mc
.parent(stepObj, groupAllSteps)
           
# delete stepGrp
            mc
.delete(stepGrp)
            checkTest
= 1
                       
   
def distributeStairs():
       
''' duplicate the pre-made spiral staircase X amount of times, and randomly distribute them '''
       
for i in range (stairs):
            trX
= random.uniform(-30,30)
            trY
= random.uniform(-30,30)
            trZ
= random.uniform(-30,30)
            newGroup
= mc.duplicate(GroupSteps,name=GroupSteps+i)
            mc
.setAttr(newGroup+".translate",trX,trY,trZ,type='double3')

   
if stairs >= 1:
        makestairs
()
       
   
if checkTest == 1:
        distributeStairs
()


distributeObjects
(3)



Simon Anderson

unread,
Sep 14, 2017, 8:02:02 PM9/14/17
to Python Programming for Autodesk Maya
Hey, the only thing that looks a bit suspect to me is the way you are setting a variable that is outside of the method. The scope of the variable may be incorrect.

Can I suggest that if everything runs correctly in your makestairs() then you return True, and then check against the returned variable. Bit nicer then passing values to and from different method scopes.

    if stairs >= 1:
        
checkTest = makestairs()
        
    
if checkTest: 
        distributeStairs
()


Also noticed that you are setting checkTest over a hundred times as you have it running in the loop, maybe move it out of the loop. So that it only gets run once all the stairs are built. I would also recommend passing values into a method, instead of just accessing them from a different scope, this can result in bugs, and it a bit ugly.

def makestairs(staris)
    # code here

def distributeStairs(stairs):
     # code here

if checkTest: 
    distributeStairs
(stairs)

I hope that helps

jettam

unread,
Sep 14, 2017, 10:54:33 PM9/14/17
to Python Programming for Autodesk Maya
Thanks !   :) 

Marcus Ottosson

unread,
Sep 15, 2017, 1:23:19 AM9/15/17
to python_in...@googlegroups.com
Just want to say, Justin, I think it's great how you're approaching Python. Lots of small, well articulated problems with a great attitude towards those helping you and the problem itself. This is how you learn a new skill the quickest whilst providing those around you with challenges to solve. You'll be a ninja in no time. Keep it up!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/edd70e60-7403-4874-9cd6-3d6038333416%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

jettam

unread,
Sep 15, 2017, 2:02:33 PM9/15/17
to Python Programming for Autodesk Maya
Thanks Marcus, I needed to hear that today. I am feeling kind of bad for posting Duh! questions to this forum. So thanks to those that respond so patiently, it helps me a lot. 
Reply all
Reply to author
Forward
0 new messages