View Full Version : Can someone convert this to Python?
Scott Ayers 09-03-2010, 05:00 PM I'm still having some trouble understanding how to do this kind of variable overloading in Python.
So I thought it might help me to learn this better if I posted one of my favorite Coffee examples using this method. And then seeing the same code converted to Python.
Coffee version:
//This script makes a cube bounce between 200y and -200y
var direction = 0;
main(doc,op)
{
var obj = doc->FindObject("Cube");
var gp = obj->GetPosition();
var offset = 3; // <-Change offset here
if(gp.y > 200)
{
direction=1;
}
else if(gp.y < 0)
{
direction=0;
}
if(direction==0)
{
var i;
for(i=0; i < offset; i++)
{
gp.y += offset;
}
}
if(direction==1)
{
var i;
for(i=0; i < offset; i++)
{
gp.y -= offset;
}
}
obj->SetPosition(gp);
}
Can anyone convert this code to Python so I can see how it's done in that language?
-ScottA
|
|
Scott Ayers
09-04-2010, 02:02 AM
Never mind Guys. I think I've got it figured out.
I guess the trick is any time you want to overload a variable like we've been doing in Coffee. You have to also add a copy of your prepass variable to the main section. And set it to global.:shrug:
I'll have to do more testing to see if that holds true or not.
Anyway.
Here's the Python code for you guys out there who are learning like me :
import c4d
direction = 0
def main():
global direction
obj = doc.SearchObject("Cube")
gp = obj.GetPos()
offset = 3
if gp.y > 280:
direction = 1
elif gp.y < 0:
direction = 0
if direction == 0:
for i in range(0,offset):
gp.y += offset
if direction == 1:
for i in range(0,offset):
gp.y -= offset
obj.SetPos(gp)
-ScottA
mayajunky
09-04-2010, 03:26 AM
hey cool, boy we're going to almost have to start posting .py docs, this code view chews it up. All I can say is each one of these sort of threads is helping me out about now. :) Here's some real simple stuff was trying, and a nice little logic trick for range checking.
import c4d
def main():
myList = ["word", "brown", "number", 9]
print myList
newNum = myList[3] + 9
print newNum
myList.append(10)
print myList
# checking if a value is in range using a logical chain
value = 5
lowRange = 4
highRange = 6
if lowRange <= value <= highRange:
print ("true")
mayajunky
09-04-2010, 03:33 AM
And here's a little test on "dynamic typing".
import c4d
def main():
# dynamic typing - as in datatypes
myDataType = "aString"
print myDataType
myDataType = 16
myDataType -= 8
print myDataType
# myDataType += " anotherWord" #uncomment this line to throw error
print myDataType
# a str is bound to a variable "myDataType"
# variable is re-bound to an int
# str "aString" is sent to garbage collection
# operation on int value with print cross ref
# when line is uncommented error is thrown from unsupported operand
# from trying to add a string to an integer
myDataType = "firstWord, "
myDataType += "secondWord"
print myDataType
Scott Ayers
09-04-2010, 02:42 PM
Yeah. The wrap tag for this forum software definitely can be a problem.
But I suppose on the plus side. It does force people to learn how to do the Python indentation stuff.
If someone can't even fix the indentation stuff. Then most likely having the code will be useless to them anyway.
I really hope people don't get into the habit of posting files. Because we don't get much file storage space here. And when people get close to running out of storage space they will be deleting old scripts that would have been useful to have. I hate when that happens.
Plus this forum get's indexed by search engines so you can google things without being here. But the results are text only and the attachments are not included.
And it's really much nicer to scan through a thread and see the code without having to download it just to see it.
I hope we don't go that route.
-ScottA
mayajunky
09-04-2010, 02:52 PM
Well yea lot's of valid points there. Wasn't really if you could or couldn't re-indent the code, was just about it being more work lol. But is nice to see right in the thread for sure. I was almost gonna post in thread and as attachment. I mean so far my biggest .py doc is 688bytes anyways. :P
CGTalk Moderation
09-04-2010, 02:52 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.