This documentation is for the free plugin Py4D in CINEMA 4D R11.5 and not for the C4DSDK of Python in CINEMA 4D R12. For R12, please visit PluginCafe.com
This documentation is for the free plugin Py4D in CINEMA 4D R11.5 and not for the C4DSDK of Python in CINEMA 4D R12. For R12, please visit PluginCafe.com
| Release: | 0.9.9983 |
|---|---|
| Date: | April 08, 2010 |
Python is a programming language in which plugins and expressions for CINEMA 4D are written. This programming language resembles C/C++ and Java. Some things work the same and some do not.
Note
Python has no brackets, so use 4-space indentation. Tabs are optional but not recommended - please avoid tabs.
A feature that might be unnerving for any C programmer is the fact that Python is typeless. Meaning that any variables have no type:
number = 5
name = "Bobe"
your_object = None
Typeless is kind of a misnomer, a better word would be typefull. Variables in CINEMA can hold any type of data. A variable could hold a number at first and then you could assign it to contain a string. This versatility also has some pitfalls, but these are beyond the scope of this introduction.
Even though a variable can hold any kind of value (a number, a cube, or a matrix), you can still figure out what it is holding at this moment. The type() function allows us to find this information out. type() figures this out by looking at what is contained in the variable. Once it has identified what the type is, it tells you by it’s return value.
Remarks are text that are not read by the compiler. They are ignored. You can remark all text after the symbol # on a line. For example:
#this is a remark
a = 4.5 # set 'a' equal to four
c = 2 # set 'c' equal to two
b = c + a / 4 # this might be the right algorithm
These are generally programmer notes. They can be other things as well, but the important thing to realize is that they are ignored by the compiler.
Python does not support prorotypes for class or function definitions. To check out how to avoid this requirement you can check out: http://docs.python.org/reference/compound_stmts.html#function-definitions
Symbol Chart(for logical operators):
== equals to != not equal to <= less than or equal to >= greater than or equal to < less than > greater than
1 == 1 is True 1 != 1 is is False 1 <= 1 is is True 1 >= 1 is is True 1 > 1 is is False 1 < 1 is is False
Conditional expressions are used in conditional execution, which happens to be the next section.
Conditional execution allows certain code to be executed and other code to be skipped based on a certain condition:
code_line1 # always do this
code_line2 # always do this
if condition: # if condition is True...
do_this # ...do this
code_line3 # always do this
Another example:
code_line1 # always do this
code_line2 # always do this
if condition: # if condition is True...
do_this # ...do this
else: # if condition is False...
do_that # do that
code_line3 # always do this
And yet another example:
if condition: #
do_this1 #
elif condition2: # if previous condition is False and condition2 is True...
do_this2 # ... do_this2 and only this
elif condition3: # ...and if previous conditions are False and condition3 is True
do_this3 # ... do_this3 and only this
else: # if all previous conditions are False
do_this4 # do_this4 and only this
Btw, just a hint, Python does not contain a switch statement. There are some alternative structure which might help you, but the easiest way is just to use the following if statement structure:
if number==1:
do_this1
elif number==2:
do_this2
elif number==3:
do_this3
else: #like default in a switch statement
do_something_else
To repeat the execution of code blocks you can use loops.
For loop: http://docs.python.org/reference/compound_stmts.html#the-for-statement While loop: http://docs.python.org/reference/compound_stmts.html#the-while-statement
Simply use return to return control back to it’s calling function. Alternatively you can have a variable after it or even a number. Return examples:
return # return to calling function with none
return 0 # return to calling function passing back 0
return a # return to calling function passing back a
return a+b # return to calling function passing back a+b
There is statement, called pass which is a placeholder for coe that hasn’t been written yet. This statement piece doesn’t do anything:
Python will raise an IndentationError, it expected an indented block:
def rotate():
So if a piece of code isn’t done yet, just use the pass statement:
def rotate():
pass
#everything is fine now
Note
Pass is similar to {} in C.O.F.F.E.E. or C++.
Classes in Python are very similar to other programing. Here is an example class:
class ExampleClass():
x = None
y = "Hello World!"
z = 3
def RotateShape(self, shape, v):
#rotate code goes here
pass
def ScaleShape(self, shape, v):
#scale code goes here
pass
def MoveShape(self, shape, v):
#move (displacement) code goes here
pass
To call functions or use variables within a class is done using the member operator (.). Just use the variable with the member operator to get at the member variable or class. Example:
class MyClass():
v = 400 #init v with 400
y = None
def Add(self, x):
"""Add x with v"""
return self.v + self.y + x
def SetY(self, value):
self.y = value
if __name__=='__main__':
c = MyClass()
c.SetY(20)
result = c.Add(100)
print result #output '520'