Re: [Maya-Python] Re: Towers of Hanoi

62 views
Skip to first unread message
Message has been deleted
Message has been deleted

Justin Israel

unread,
Apr 28, 2014, 5:44:34 PM4/28/14
to python_in...@googlegroups.com
Yea good notes. I agree to just avoid using globals as they aren't needed here. Your functions should return their list values, and the TowersOfHanoi() should take the arguments it needs to run. 

In terms of initialization, where you need to clear and establish the scene, that may call for either an initialize() function, or moving this all into a class where you can offer a start() type method and then save the state of the discs as a member attribute, and then run the simulation. 

Also, as a general python idiom, one normally reserves upper-case naming for classes or global constants, and using lower_case (camelCase if you don't care about pep8) for functions and local variables.




On Tue, Apr 29, 2014 at 9:02 AM, Andres Weber <andres...@gmail.com> wrote:
Just gonna say a couple things that I noticed right off the bat (there's a TON of things wrong with this...but it's more of how it's organized that isn't necessarily great rather than syntax.

First thing is:
sourcePeg = [discList] should be sourcePeg = discList (then the two lists are the same list and can be modified accordingly.

Second thing is:
discList.append('disc_*') doesn't really make any sense (you don't want to append a wild card selection string...it doesn't actually represent the name of any maya objects.)

It should be more like this (but I still don't like it!).  You should initialize a variable that represents your object's name that can be used throughout.

curDisc = 'disc_%d'%i
cmds.duplicate( 'disc_0', n=curDisc )

cmds.move( 0, 0.3, 0,'disc_0', relative=True )
cmds.scale( 0.8, 1, 0.8,'disc_0', relative=True )
discList.append(curDisc)

Then the whole global local setup you have is totally convoluted.  In your first function (pegs) you shouldn't be defining pegs as [].  You would probably (depending on your design scheme) define that AFTER all the functions (or in a main() function) as a global and then initialize it there.  Then all you do is reference pegList without defining it or mentioning that it's global in the other functions and everything should function fine, not that any of this is necessary...you could just pass these variables between functions with less pain and convolution.  Also in your last call of TowersOfHanoi(len(sourcePeg), "A", "B", "C") you don't even reference the actual pegs from the pegs list.  You should be passing destinationPeg, sourcePeg and auxiliaryPeg as arguments to the TowersOfHanoi function instead so you're actually referencing the lists you expect rather than strings... you can't .pop or .append strings.

These are just some jumbled thoughts that I quickly wrote down...hopefully they guide you a little bit.  Other people might disagree with my thoughts design pattern-wise but at least they'll get you to functioning code since I would rather not come up with the design pattern right now...just dealing with syntax stuff.


On Monday, April 28, 2014 2:22:51 PM UTC-4, mixailmixail1 wrote:
Hi everyone! I am new to python and I'm having a few difficulties.
I'm attempting to implement a program in Python (using Maya script editor), that can generate an animation sequence
capable of visualising moving n disks from Peg 1(sourcePeg) to Peg 2(destinationPeg).

Here is what I've done so far:

import maya.cmds as cmds

cmds.select(all=True)
cmds.delete()

def Pegs( Height, Radius ):
    pegList=[]
    sourcePeg=cmds.polyCylinder( name='A', r=Radius, sx=15, sy=5, sz=2, h=Height )
    cmds.xform( piv=[0,-Height/2,0] )
    cmds.move( 0, Height/2, 4)

    destinationPeg=cmds.polyCylinder( name='B', r=Radius, sx=15, sy=5, sz=2, h=Height )
    cmds.xform( piv=[0,-Height/2,0] )
    cmds.move( 0, Height/2, 0)
   
    auxiliaryPeg=cmds.polyCylinder( name='C', r=Radius, sx=15, sy=5, sz=2, h=Height )
    cmds.xform( piv=[0,-Height/2,0] )
    cmds.move( 0, Height/2, -4 )
   
    pegList.append(sourcePeg)
    pegList.append(destinationPeg)
    pegList.append(auxiliaryPeg)
   
Pegs( 4, 0.05 )

def Discs( n, Height, Radius ):

    global discList
    global pegList
    discList=[]
    cmds.polyPipe( name='disc_0', r=Radius*6, t=1, h=Height*0.15 )
    cmds.xform( piv=[0,-Height*0.075/2,0] )
    cmds.move( 0, Height*0.0375, 4, 'disc_0' )

    for i in range(1, n):
        cmds.duplicate( 'disc_0', 'disc_'+str(i) )
        cmds.move( 0, 0.3, 0,'disc_0', relative=True )
        cmds.scale( 0.8, 1, 0.8,'disc_0', relative=True )
        discList.append('disc_*')
    print discList
       
Discs( 5, 4, 0.2 )   

def TowersOfHanoi( m, sourcePeg, destinationPeg, auxiliaryPeg):
    global pegList
    global discList  
   
    if m == 0:
   
        print "There are no Discs! "    
   
    elif m == 1:

        print " \t\t| Move Disc ", m, " from ", sourcePeg, " Peg ", "to ---> ", destinationPeg, " Peg |"
        cmds.move( 0, 0, 0, 'disc_0')
     
    else:       

        TowersOfHanoi(m - 1, sourcePeg, auxiliaryPeg, destinationPeg)
        print " \t\t| Move Disc ", m, " from ", sourcePeg, " Peg ", "to ---> ", destinationPeg, " Peg |"
       
        if sourcePeg:
            destinationPeg.append(sourcePeg.pop())
           
        cmds.move( 0, 0, -4, 'disc_*', r=True)
       
        TowersOfHanoi(m - 1, auxiliaryPeg, destinationPeg, sourcePeg)

sourcePeg = [discList]
destinationPeg = []
auxiliaryPeg = []
TowersOfHanoi(len(sourcePeg), "A", "B", "C")

print auxiliaryPeg, destinationPeg, sourcePeg         
     
.
.
.
I can't manage to get it to work... =S
Any help would be greatly appreciated!

Kind regards!



--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e88fd3cd-1334-4cde-acc3-34bf93bd6ae7%40googlegroups.com.

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

Message has been deleted
Message has been deleted
Message has been deleted

Marcus Ottosson

unread,
Apr 30, 2014, 10:23:08 AM4/30/14
to python_in...@googlegroups.com
There is no deleting on a mailing list. These are forever embossed in internet history. :)


On 30 April 2014 15:17, mixailmixail1 <mixail...@live.com> wrote:
Hello, thanks for the help =) Could you please delete the Post since It includes my script. Thank you! =]

--
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_m...@googlegroups.com.

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



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages