1
$\begingroup$

I'm a new python/Blender user I'm trying to move an object using an Arduino. I've watched many videos and tutorials, and somehow i could write a code using others codes. here is my code

import bge
import math
import serial



controller = bge.logic.getCurrentController()
own = controller.owner

serialport = serial.Serial('COM4', 9600)
sens = controller.sensors['run']


val1=0.0
val2=0.0
val3=0.0
p = pi/255

def run():
    if sens.positive: 
            serialport.write('s'.encode('ascii')) #blender sends the ascii code of "s" to the Arduino
            n = serialport.readline() #receive data from the arduino
            val1 = (float) (n)
            val2 = (val1-val3)* p
            val3 = val1
            print ('The value is: ')
            print (val2)
            own.applyRotation((0 , val2 , 0),0)

#    own.position.y = val2

my problem is with the sens = controller.sensors['run'] when i run the game engine the following message appears:

Python module can't be imported - object 'Cube', controller 'Python': KeyError: 'requested item "run" does not exist'

as usual i looked for a solution by checking questions of people have the same problem here and elsewhere. so i followed some of the best answers. such as, setting the actor flag in physics tab, some people suggested that reinstalling blender would help. nothing worked for me. thanks anyway

$\endgroup$
2

2 Answers 2

0
$\begingroup$

The line controller = bge.logic.getCurrentController() gets the controller logic brick that has the python script, then controller.sensors is a list of sensors that are connected to the python controller. The 'key' that you use with the sensor list must match one of the sensor names connected to the controller.

example logic bricks

So (1) is the connection required for the sensor to be in the controller.sensors list. Then (2) and (3) must be an exact match which is case sensitive.

So your error indicates that the name of run does not match any sensors you have connected to the python controller.

Having said that, you don't need to get the sensor for the script you have. I expect you are using an always sensor to run the script repeatedly, you should only need to read from your arduino and perform any changes you want.

The main flaw you will find is that the script is run cleanly from the start each time, that means the variables you are adjusting will be reset to zero every time. You want to use your script as a module rather than a script, this answer should explain the difference.

$\endgroup$
1
  • $\begingroup$ this worked for me thanks bro apparently i needed to name the sensor and the controller $\endgroup$
    – Bassel
    Oct 10, 2017 at 18:26
2
$\begingroup$

Analysis

Python module can't be imported - object 'Cube', controller 'Python': KeyError: 'requested item "run" does not exist'

This means you misconfigured the Python controller.

Either you use script mode and you refer to an not existing text block called "run" (the field should have red background).

Or you use module mode and you did not mention what file contains your code.

E.g. your code is called arduino.py. You can set up this line: arduino.applyRotation.

The BGE will look for arduino.py and check if it finds a function applyRotation.

arduino.py

import bge 

def applyRotation(controller):
    sensor = controller.sensors["run"]
    if sensor.positive:
        ...

Solution

Ensure the file and the function inside of it can be found.

Hint:

If you want to use the module mode ensure to retrieve the controller inside of the called function. Otherwise you get unwanted side effects.

$\endgroup$
1
  • $\begingroup$ I already added an Always sensor and match it to a Python controller called "Python". and i changed it's mode to module and i refer it to "run" as follows test11.run however it keeps showing the error. can you please explain what do u mean by retrieving the controller inside of the called function? Thanks anyway $\endgroup$
    – Bassel
    Oct 10, 2017 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .