Python Console

Introduction

The Python Console can be found in the Script Menu -> Console, besides other loggers, a Python entry is available.

../../_images/python_console_menu.jpg

In the Python Console, code can be written and executed. It’s also the place where all Python strings are written (print, exception, c4d.GePrint()) whatever the context is (Script Manager, plugins, Python Generator, etc…). If a script/plugin doesn’t work, or something unexpected happens in the code execution, checking the Python Console for errors it’s always recommended.

The Python console can also be used to retrieve Symbol ID for Parameters. See Drag And Drop.

Note

The c4d module is imported by default. Code executed from the console is executed in the main thread.

Features

Code Execution

The code run from the Python Console is executed in Cinema 4D main thread. It means that if an infinite loop takes place, Cinema 4D will hang as well. See Threading Information.

Basic Execution

As in a Python interpreter, it’s possible to write code after >>>. Then by pressing “Enter” the code will be executed.

../../_images/python_console_typping.gif

Multiline Execution

Since R20 Python Console supports multiple lines execution. Writing the next code is now possible.

for i in xrange(10):
    for x in xrange(10):
        print i, x

Variables

The following variables are pre-defined in the Python Console:

Drag And Drop

Scene Elements

Almost all scene elements (object, tag, material, etc…) can be dragged and dropped to the Python Console.

Note

When an object, tag, shader, or even a parameter is dragged and dropped into the script manager, a temporary variable named as the element is created.
This variable is only available in the current scope and for the current execution phase and is assigned to the dragged scene element.
../../_images/python_console_drag_object.gif

If a material called “Material” is dragged into the console and Enter is pressed, the following text will be printed:

« <c4d.Material object called ‘Material/Mat’ with ID 5703 at 0x000001D1876DB950> »

  1. c4d.Material is the actual class of the elements dragged. In this case c4d.Material.

  2. ‘Material/Mat’ is the dragged element’s name and the default name of this element.

  3. 5703 is the ID of the Type. For instance the material dragged was the default material with the ID 5703 which correspond to the symbol c4d.Mmaterial.

  4. 0x000001D1876DB950 is the current memory address of the Python Object. Can be different for the same object.

Parameters

All parameters can be dragged to the Python Console. It allows to set/get parameter. It can also be used to know the symbol of a parameter.

../../_images/python_console_drag_parameter.gif

If the Size.X parameter of a cube is dragged into the console, the following text will be printed:

« Cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_X] »

  1. ‘Cube’ is a variable, named as the object it’s refer to. The object is the host of the parameter. This means that if [c4d.PRIM_CUBE_LEN,c4d.VECTOR_X] is removed, writing Cube.GetName() it will returns the name of the object.

  2. ‘[c4d.PRIM_CUBE_LEN,c4d.VECTOR_X]’ can be splitted in multiple parts:
    1. [] redirect to GetParameter or SetParameter of an object according to the context.

    2. c4d.PRIM_CUBE_LEN, is the symbol corresponding to the Size parameter.

    3. c4d.VECTOR_X, is the symbols corresponding to the X parameter of the Size Parameter.

If Enter is pressed, the value of this parameter will be printed. It’s possible to edit the parameter value with this code:

Cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_X] = 50.0

Command History

It’s possible to navigate the command History with the arrow key ‘up’ and ‘down’.

Note

Multiline is not correctly supported right now.

Clearing the Python Console, will also delete the Command History.

Scrolling

Scrolling in the Python Console is relative to the active position of the cursor. If the cursor position is located on the prompt “>>>” then new lines in the Python Console will continuously scroll to the last line. If the cursor is located on any other lines of the Python Console, depending on the position of the scroll bar, the Python Console will scroll to make that line the first or the last of the logger.