PDA

View Full Version : Python while loops vs. Coffee while loops


Scott Ayers
09-11-2010, 06:51 PM
Hey guys.
I'm sure I'm probably missing something simple here.
But in coffee I can incrementally move an object with a bunch of different methods. One of them being with while loops like this: main(doc,op)
{
var c = doc->FindObject("Cube");
var cpos = c->GetRelPos();
var offset = 1;

while(cpos.y < 200)
{
c->SetRelPos(vector(cpos.x, cpos.y + offset, cpos.z));
if (cpos.y < 200) break; //Important!! loops must have a way out to avoid infinite looping
}
if(cpos.y > 200)c->SetRelPos(vector(cpos.x, cpos.y, cpos.z));// set object to 200 if loop positioning overshoots 200
}
This works well and allows me to control the speed of the moving object.
However when I try to do the same thing with Python like this: import c4d
def main():
obj = doc.SearchObject("Cube")
pos = obj.GetRelPos()
offset = 1

while (pos.y < 200):
pos.y = pos.y + offset
if pos.y >= 200: break
obj.SetRelPos(c4d.Vector(pos.x, pos.y,pos.z))


The object moves very abruptly. No matter what I set the offset value to.
Even if I enable the python tag's "Frame Dependent" option.:sad:

I know there are ways to incrementally move things in python by using the += without using a while loop. I've used that method successfully.
But I was wondering why python treats my while loops differently than coffee does?


-ScottA

Scott Ayers
09-11-2010, 08:15 PM
Unrelated question for the mods here.
When we edit our posts here. Does the forum software save all the previous posts?

If so. I apologize for changing my code so many times.
Switching over from using GetPosition() to GetRelPos() while also in the middle of trying to get a grip on the way python does things has me correcting my posts a lot more than usual.

Sorry about that.

-ScottA

donelgreeko
09-11-2010, 09:38 PM
Hi Scott,

the converted script from Coffee to Python works fine here. In both while loops you don't use the same comparision



import c4d
def main():
c = op.GetObject()
cpos = c.GetRelPos()
offset = 1

while cpos.y < 200:
c.SetRelPos(c4d.Vector(cpos.x, cpos.y + offset, cpos.z))
if cpos.y<200: break
if cpos.y>200: c.SetRelPos(c4d.Vector(cpos.x, cpos.y, cpos.z))



Cheers, Sebastian

Scott Ayers
09-11-2010, 10:21 PM
Thanks Sebastian.

I originally posted a different script example. Then I realized it wasn't the correct one so I changed it(about a dozen times due to typing mistakes :rolleyes:). That's why the two examples don't match.

I got all twisted around with all of the new changes to the coffee and python SDK in R12.

-ScottA

CGTalk Moderation
09-11-2010, 10:21 PM
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.