Python Language Overview

Python is a programming language in which plugins and expressions for Cinema 4D are written.

Note

Python has no brackets, so use 4-space indentation. Tabs are optional but not recommended - please avoid tabs.

Variables: the Python Way

A feature that might be annoying for any C programmer is the fact that Python is typeless. Meaning that 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 4D 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 its return value.

Remarks in Python

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 functions

Python does not support prototypes for class or function definitions. To check out how to avoid this requirement you can read: https://docs.python.org/3.9/reference/compound_stmts.html#function-definitions

Python flow control

Conditional expressions

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

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

Between, 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

Returning from a function and return value

Simply use return to return control back to its 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

Pass statement

There is statement, called pass which is a placeholder for code 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 is not done yet, just use the pass statement

def rotate():
    pass          #everything is fine now

Note

pass is similar to {} in C++.

Classes in Python

Classes in Python are very similar to other programming languages. Here is an example

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'