PDA

View Full Version : Python funcations, require help


Midoriiro
10-14-2011, 04:35 AM
Hi, I am creating a solar system in maya using only Python code for a class

I had my general layout finished and decided to go back and compress a lot of it by using self defined functions

I had one of the functions working (createPlanets) up until maya crashed, when I reopened it up I typed it exactly as I had it yet it doing something weird.

I think the problem is with createPlanets() but now I don't know for sure ><

Here is my code:

#Nick Stromberg "Solar System"

from __future__ import division
import maya.cmds as cmds
import maya.mel as mel

#Create a new file
cmds.file(f=True,new=True)
#Sets up a keyset in help manage shape of animation
cmds.keyTangent(itt="linear",ott="linear",g=True)

#Queries how long the animation is to be
timeLength=input("How many years?")
cmds.playbackOptions(animationEndTime=timeLength,animationStartTime=1)

#Library of information about planets
nameInfo=["sun","mercury","venus","earth","mars","jupiter","saturn","uranus","neptune"]
planetName=[sun,mercury,venus,earth,mars,jupiter,saturn,uranus,neptune]
scaleInfo=[1380000,4876,12107,12756,6794,142983,120536,51117,49527]
positionInfo=[0,57000000,107000000,150000000,229000000,777000000,1429000000,2871000000,4496000000]
axisInfo=[0,0,-177.5,-23.44,-25.19,-3.13,-26.73,-97.77,-28.32]
revolutionInfo=[0,87,224,365,687,4331,10832,30799,60190]
rotationInfo=[0,58,243,1,1,1,0.38,0.42,0.71,0.67]
incInfo=[6.34,2.19,1.57,1.67,0.32,0.93,1.02,0.72]

#Calculating each planet's size
#for x in scaleInfo:
# x=x/100000

#Modify Library's ratios using functions

def divideScale(n):
a=0
while a<n:
scaleInfo[a]=scaleInfo[a]/200000
a=a+1

divideScale(9)

#Modify and calculate each planet's distance from the Sun using a function
#for x in positionInfo:
# x=x/500000000

def dividePosition(n):
a=1
while a<n:
positionInfo[a]=(positionInfo[a]/37500000)+scaleInfo[0]
a=a+1

dividePosition(9)

#Create each individual planet using info from libraries using a simple function

def createPlanets(x):
a=0
while a<x:
planetName[a]=cmds.polySphere(n=nameInfo[a])
cmds.scale(scaleInfo[a],scaleInfo[a],scaleInfo[a])
cmds.move(positionInfo[a],0,0,)
a=a+1

createPlanets(9)

#Apply all planets orbital inclination in relation to the invariable plane

mercuryInc=cmds.group(mercury[0],n='mercuryInc')
venusInc=cmds.group(venus[0],n='venusInc')
earthInc=cmds.group(earth[0],n='earthInc')
marsInc=cmds.group(mars[0],n='marsInc')
jupiterInc=cmds.group(jupiter[0],n='jupiterInc')
saturnInc=cmds.group(saturn[0],n='saturnInc')
uranusInc=cmds.group(uranus[0],n='uranusInc')
neptuneInc=cmds.group(neptune[0],n='neptuneInc')

cmds.xform(mercuryInc,worldSpace=True,piv=(0,0,0))
cmds.xform(venusInc,worldSpace=True,piv=(0,0,0))
cmds.xform(earthInc,worldSpace=True,piv=(0,0,0))
cmds.xform(marsInc,worldSpace=True,piv=(0,0,0))
cmds.xform(jupiterInc,worldSpace=True,piv=(0,0,0))
cmds.xform(saturnInc,worldSpace=True,piv=(0,0,0))
cmds.xform(uranusInc,worldSpace=True,piv=(0,0,0))
cmds.xform(neptuneInc,worldSpace=True,piv=(0,0,0))

cmds.rotate(0,0,incInfo[0],mercuryInc)
cmds.rotate(0,0,incInfo[1],venusInc)
cmds.rotate(0,0,incInfo[2],earthInc)
cmds.rotate(0,0,incInfo[3],marsInc)
cmds.rotate(0,0,incInfo[4],jupiterInc)
cmds.rotate(0,0,incInfo[5],saturnInc)
cmds.rotate(0,0,incInfo[6],uranusInc)
cmds.rotate(0,0,incInfo[7],neptuneInc)

#Pivot all planets to Sun (origin)

mercuryRev=cmds.group(mercuryInc,n='mercuryRev')
venusRev=cmds.group(venusInc,n='venusRev')
earthRev=cmds.group(earthInc,n='earthRev')
marsRev=cmds.group(marsInc,n='marsRev')
jupiterRev=cmds.group(jupiterInc,n='jupiterRev')
saturnRev=cmds.group(saturnInc,n='saturnRev')
uranusRev=cmds.group(uranusInc,n='uranusRev')
neptuneRev=cmds.group(neptuneInc,n='neptuneRev')

cmds.xform(mercuryRev,worldSpace=True,piv=(0,0,0))
cmds.xform(venusRev,worldSpace=True,piv=(0,0,0))
cmds.xform(earthRev,worldSpace=True,piv=(0,0,0))
cmds.xform(marsRev,worldSpace=True,piv=(0,0,0))
cmds.xform(jupiterRev,worldSpace=True,piv=(0,0,0))
cmds.xform(saturnRev,worldSpace=True,piv=(0,0,0))
cmds.xform(uranusRev,worldSpace=True,piv=(0,0,0))
cmds.xform(neptuneRev,worldSpace=True,piv=(0,0,0))

#Set planet revolutions
#Followed by setting planet rotations

def mercuryRotate(n):
a=0
b=349.34
c=0
while (a-1)<n:
cmds.setKeyframe(mercuryRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[1]
a=a+1

def mercuryRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(mercury[0],v=b,t=c,at='rotateY')
b=b+360
c=c+rotationInfo[1]
a=a+1

def venusRotate(n):
a=0
b=274.00
c=0
while (a-1)<n:
cmds.setKeyframe(venusRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[2]
a=a+1

def venusRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(venus[0],v=b,t=c,at='rotateY')
b=b-360
c=c+rotationInfo[2]
a=a+1

def earthRotate(n):
a=0
b=283.14
c=0
while (a-1)<n:
cmds.setKeyframe(earthRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[3]
a=a+1

def earthRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(earth[0],v=b,t=c,at='rotateY')
b=b+360
c=c+rotationInfo[3]
a=a+1

def marsRotate(n):
a=0
b=154.86
c=0
while (a-1)<n:
cmds.setKeyframe(marsRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[4]
a=a+1

def marsRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(mars[0],v=b,t=c,at='rotateY')
b=b+360
c=c+rotationInfo[4]
a=a+1

def jupiterRotate(n):
a=0
b=152.64
c=0
while (a-1)<n:
cmds.setKeyframe(jupiterRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[5]
a=a+1

def jupiterRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(jupiter[0],v=b,t=c,at='rotateY')
b=b+360
c=c+rotationInfo[5]
a=a+1

def saturnRotate(n):
a=0
b=202.88
c=0
while (a-1)<n:
cmds.setKeyframe(saturnRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[6]
a=a+1

def saturnRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(saturn[0],v=b,t=c,at='rotateY')
b=b+360
c=c+rotationInfo[6]
a=a+1

def uranusRotate(n):
a=0
b=131.33
c=0
while (a-1)<n:
cmds.setKeyframe(uranusRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[7]
a=a+1

def uranusRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(uranus[0],v=b,t=c,at='rotateY')
b=b-360
c=c+rotationInfo[7]
a=a+1

def neptuneRotate(n):
a=0
b=122.28
c=0
while (a-1)<n:
cmds.setKeyframe(neptuneRev,v=b,t=c,at='rotateY')
b=b+360
c=c+revolutionInfo[8]
a=a+1

def neptuneRotate2(n):
a=0
b=0
c=0
while (a-1)<n:
cmds.setKeyframe(neptune[0],v=b,t=c,at='rotateY')
b=b+360
c=c+rotationInfo[8]
a=a+1

#Apply the calculated revolutions and rotations to the length of the animation

mercuryRotate(timeLength/revolutionInfo[1])
mercuryRotate2(timeLength/rotationInfo[1])
venusRotate(timeLength/revolutionInfo[2])
venusRotate2(timeLength/rotationInfo[2])
earthRotate(timeLength/revolutionInfo[3])
earthRotate2(timeLength/rotationInfo[3])
marsRotate(timeLength/revolutionInfo[4])
marsRotate2(timeLength/rotationInfo[4])
jupiterRotate(timeLength/revolutionInfo[5])
jupiterRotate2(timeLength/rotationInfo[5])
saturnRotate(timeLength/revolutionInfo[6])
saturnRotate2(timeLength/rotationInfo[6])
uranusRotate(timeLength/revolutionInfo[7])
uranusRotate2(timeLength/rotationInfo[7])
neptuneRotate(timeLength/revolutionInfo[8])
neptuneRotate2(timeLength/rotationInfo[8])


When I run the script it is telling me that ""mercury" is not defined"

And more importantly it is probably not finding it because when it creates the planets it is adding "Shape" to the end of each of their names

for instance:
instead of just "mercury", the planet is called "mercuryShape"

It was not doing this before and I cannot for the life of me find out why any help would be much appreciated
Thank you in advance
-Nick

P.S. I plan to move on and make functions for that whole bottom bit as well as change it from asking how many days to how many years

CGTalk Moderation
10-14-2011, 04:35 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.