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 <[email protected]>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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/e88fd3cd-1334-4cde-acc3-34bf93bd6ae7%40googlegroups.com<https://groups.google.com/d/msgid/python_inside_maya/e88fd3cd-1334-4cde-acc3-34bf93bd6ae7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1RSzMsbMd%2BpyY0tRVwP43nrOH3BHxNUvWL9%2ByVb%2B_4RA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to