Graphics Reference
In-Depth Information
the question. One is to look it up in the API. Another way is with the dir() command mentioned previously.
To do this, temporarily add the following line to your script:
print (dir(cont))
You now have a three-line script called move.py associated with a Python controller, which is in turn con-
nected to the Keyboard sensors and actuators shown previously in Figure 16-1 . Place your Blender window in
such a way that the terminal is visible, and enter game-play mode by pressing P. In game-play mode, press one
of the four arrow keys: up, down, right, or left. When you do so, you will trigger the script to run, and the con-
troller's dir() value will be printed to the System Console as follows (without the line breaks):
['__class__', '__delattr__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'activate', 'actuators', 'deactivate', 'executePriority', 'invalid',
'mode', 'name', 'owner', 'script', 'sensors', 'state',
'useHighPriority']
This is a list of all the methods defined for Controller objects. As you can see, just reading this list will give
youafewideasofwhatkindsofthingsyoucanrequestfromcontrollers.Becauseyou'reinterestedinaccessing
the Armature object that is the owner of the controller in this example, it is intuitively clear that the owner is
what you want. The line of code to retrieve the owner is as follows:
own = cont.
owner
Sensors and Actuators
You now have access to the controller and to the object that owns it. The next thing to do is to retrieve the ne-
cessary sensors. Again, the output from the dir() command showed that cont has a sensors method. You can
see what sensors you have active with this command:
print(cont.sensors)
Next, you want to keep the sensors distinct and assign them to meaningfully named variables, as shown in
the following code:
#Sensors
forward = cont.sensors['forward']
right = cont.sensors['right']
left = cont.sensors['left']
back = cont.sensors['back']
The retrieval of the actuators works in exactly the same way:
#Actuators
motion = cont.actuators["motion"]
action = cont.actuators["action"]
Game Logic in Python
Now all the necessary logic brick components are accessible within the script, and the code for the movement
itself can begin. Start by setting some variable values. Set the variables speed and rspeed with the values
Search WWH ::




Custom Search