Python Console¶
Introduction¶
The Python Console can be found in the Script Menu -> Console, besides other loggers, a Python entry is available.
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.
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:
doc:
BaseDocument
: The current active document. Similar toc4d.documents.GetActiveDocument()
.op:
BaseObject
: The current active Object. Can be None if no object is selected. Similar toBaseDocument.GetActiveObject()
Drag And Drop¶
Scene Elements¶
Almost all scene elements (object, tag, material, etc…) can be dragged and dropped to the Python Console.
Note
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> »
c4d.Material
is the actual class of the elements dragged. In this casec4d.Material
.‘Material/Mat’ is the dragged element’s name and the default name of this element.
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.
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.
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] »
‘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.
- ‘[c4d.PRIM_CUBE_LEN,c4d.VECTOR_X]’ can be splitted in multiple parts:
[] redirect to
GetParameter
orSetParameter
of an object according to the context.c4d.PRIM_CUBE_LEN, is the symbol corresponding to the Size parameter.
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.