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.
For more options, visit https://groups.google.com/d/optout.