ScriptSpot

Forum topic · General Scripting

Using objects created in an if-loop, lateron in that same loop

By Ralf · 2008-12-20

Description

Hello,
I'm still working on my script (some topics ago i had a question) And i got stuck agian. Im trying to create a moon...its orbit around a planet moves ofcourse with the planets orbit. So i want either
1) to path-constrain orbit1 to orbit2, or (the better option)
2) to link the orbit to the planet.
Problem here seems (aside from the syntax which i still have trouble learning) that this parent-orbit or parent-planet is created in the same if-loop.

the next loop works just as expected, all orbits are centered around [0,0,0]...... sofar so good



/* ExampleArray = #(name,center,type,other) */ 

Plt = #(#("Jupiter", "Origin", "Planet","Other Properties"),\
#("Io", "Jupiter", "Moon","Other Properties"))

for i=1 to Plt.count do
(
   orbit1 = ellipse() 
   orbit1.name = "Orbit of " + Plt[i][1]
--some more lines setting properties of Orbit1 here i left out for easy reading
	
   globe = sphere()
--some more lines setting properties of globe here i left out for easy reading

   globe.pos.controller = Path_Constraint()
   globe.pos.controller.path = orbit1
)

Now, i'd like to get the moon-orbit to move around the orbit of its parent Option1). i could just create a seperate ellipse for this, but that would mean is get multiple identical ellipses. I'd rather select and use the existing one. The next script doesnt work...
i have an idea why, but don't know how to do it right



for i=1 to Plt.count do
(
	orbit1 = ellipse() 
	orbit1.name = "Orbit of " + Plt[i][1]	
--some more lines setting properties of Orbit1 here i left out for easy reading

	globe = sphere()
--some more lines setting properties of globe here i left out for easy reading

   if Plt[i][3]=="Moon" do (
	PPath = Max Select "Orbit of " + Plt[i][2]
--this previous line is obvious the problem
	orbit1.pos.controller = Path_Constraint()
	orbit1.pos.controller.path = PPath
	)
	
	globe.pos.controller = Path_Constraint()
	globe.pos.controller.path = orbit1
)

I tried some variations on the next loop aswell. This gave me the impression i cannot select anything created earlier in the "for i=1 to Plt.count do"-loop until the whole loop is finnished. is that correct?


if Plt[i][1][2]=="Moon" do (
		LC = Link_Constraint()
		Parent = Plt[i][2]
		orbit1.controller = LC
		orbit1.transform.controller.addTarget $Parent 0
		)

Could anyone please help me out a bit?

Comments (6)

Ralf · 2008-12-23

ah, great, this is gonna help me to make some major improvements during the holydays.
(i did indeed download v1 2 times... i was wondering)

You're right. Moon-orbits don't rotate with the parentorbit, the major-ax is 'fixed' wrt the stars. I already got the shape and orientation of orbits and planets working. It wasnt very hard, and left it out of the code in my 1st post.

For the animation i got an input for a start and end date(y/m/d), plus an accuracy-slider (hours/frame) which now work 95%. Only the non-constant velocity has yet to be implemented, but i got the formula's from kepler, and a good idea how to do it.

I'll post the 'results' after newyear. i dont have the illusion that it will be finnished, but i hope to make some good progress over the next couple of day's

Before i became a modeler i studied astrophysics for a few year, so this was a nice case to learn MaxScript by. ofcourse not the easiest case (typical me)
But it keeps me thinking about it around 24h a day :) ... and it's great to know someone else can get entusiastic about it aswell.
tnx again Anton :)

Anton Berg · 2008-12-23

made another version with some added animation just for fun ;-)

I really can´t let this project go... it is fun. Will be interesting to see your final result!

Attachments

Anton Berg · 2008-12-23

Have you looked at v03 of my script? You should be able to add x amount of planets and n amount of moons connected to every planet.

But I create a separate path spline for all the moons. I am not sure but the moons of a planet dont follow the same spline do they or?

regaring your first script: you typed this..
PPath = Max Select "Orbit of " + Plt[i][2]
--this previous line is obvious the problem

if you would like to store an objectname in the variable PPath you need to make it into an object refence again.

Example:
PPath = $myplanet --this is a direct reference if you know the name
-- If you need to dynamically access the name do
myName = getNodeByName ( "Orbit of " + Plt[i][2])

--the getNodeByName takes a string as an input and returns
--ALL nodes with that name... so you might get several
--answeres if you dont name your objects correctly
--ex it might evaluate to:

PPath = $Orbit of Jupiter

I used the execute command in my v02 of this script but then I realised that it Doesnt handles spaces in object names like "Oribt of xxx" would not work OK with execute. It would think that "of" and "xxx" was commands.

Attachments

Ralf · 2008-12-22

tnx, i'm going to try some of these things when i get home, but i'm worried it won't fix the problem.

Thing is, in you're example you create the planet 'global' and then 'link' the moon to it (orbit2.pos = abplanet.pos)

That would be fine if there where only 2 objects or so, but my array contains over 100 objects...and i want end-users to add more if they like. (with the restriction the parent is always created before the child) So i have (and want) to use an if-loop.

This way the parent is created 'local' in loop-iteration i and the child in i+1.
So i think i have to get the parent out of the scene. If this is impossible i could re-organize my array, but i'd rather adjust the code (for flexabilaty-reasons).

Anyway, i'm gonna try some of your code (or what it inspired me to) and get back on it tommorow.. tnx sofar

Anton Berg · 2008-12-23

A made a small example script with a simple example of how to build the system up... check it out and let me know if it helps or doesent ;-)

By the way... one thing that is "problematic" in maxscript is to store object names as a string and later access them again. This is probably the most common place to use the execute command (which is very expensive to use) so you could convert a string: "$myobjectname" into $myobjectname without the quotes!

Happy maxscripting!

By the way how do you get the input data for this? Do you have a textfile or database?

Attachments

Anton Berg · 2008-12-21

If you to link the position of a path to another object I would recommend the:
$myPathName.parent = $thisIsTheParentObject

I dont think you need the Link_constraint unless you need to do some vary complex variable linking.

I am looking at the other "problem" to see if it as a good way of solving this but I just thought I should write something about the .parent instead of link_constraint first.

just open my small example script to se it in action.

Attachments