Python Console¶
What is the Python Console¶
The Python console (PythonConsoleView) is a view on which you can interactively enter and execute Python code on Choreonoid. You can execute Python code on the spot to check and manipulate the states of items and models. It is also useful for checking the behavior of the classes and functions you are going to use before writing a script.
This view can be shown from the main menu with “View” - “Show View” - “Python Console”.
Basic Usage¶
The console shows the prompt “>>> “, and you can enter and execute code in the same way as with the standard Python interpreter.
>>> print("Hello, Choreonoid!")
Hello, Choreonoid!
By importing Choreonoid modules, you can manipulate the objects on Choreonoid. For example, you can obtain a body item loaded in the item tree and show its information as follows.
>>> from cnoid.Base import *
>>> from cnoid.BodyPlugin import *
>>> bodyItem = RootItem.instance.findItem(BodyItem)
>>> bodyItem.name
'SR1'
>>> bodyItem.body.numJoints
29
Note that the namespace of the code executed on the console is shared with the Python script items (those whose individual namespace property is false). It is also possible to check the result of a script by referring to the variables in the script from the console after the script is executed.
Entering Code that Spans Multiple Lines¶
When entering code that spans multiple lines, such as for statements and function definitions, the prompt of the continuation lines changes to “… “. As with the standard Python interpreter, the block is executed when you enter an empty line at the end of the block.
>>> for i in range(3):
... print(i)
...
0
1
2
Input Completion¶
When you press the Tab key while entering code, the module names and the member names of classes and functions that can be entered at that position are completed. When there are multiple candidates, the list of the candidates is shown. You can proceed with input while checking the methods of a class on the spot.
Command History¶
With the up and down arrow keys, you can recall the commands you entered before. Take advantage of this when executing the same or similar commands repeatedly.