Why am I getting AttributeError: 'NoneType' object has no attribute 'Name'

Hello,
I’m using sensor conveyor to detect what kind of parts are going trough the conveyor and then I want to append certain types of parts to a list. The entire script is pretty long as there is lot of other things happening in the script but in short this is how I try to get the name:
sensor = app.findComponent(“Sensor Conveyor”)
sensor_signal = sensor.findBehaviour(“SensorSignal”)

and then later I try to read the name into a variable

part = sensor_signal.Value
and then I use part.Name when I want to know the name of the part but for some reason it is not working even though I have used this exactly same way before in different simulation and it worked perfectly then.
When I do “print part” and “print part.Name” it prints out <vcComponent object at 0x00000214CA278168> and the correct part name but then it also prints None and gives the error AttributeError: ‘NoneType’ object has no attribute ‘Name’. So what is going on? It prints out the correct name even though it complains it doesn’t exist? For a time it was also appending correct names to a list even though it was giving the same error and then suddenly it just stopped working. I have no idea what is causing this.

Terve @ceppo

You need to check that the sensor is not returning None. The sensor returns None if the part has already left the sensor region.

Try adding this to your code:

if sensor_signal.Value is not None:
    part = sensor_signal.Value
    print part, part.Name
else:
    print 'Error, no part detected'

Hi,
The sensor seems to return both name and none almost at the same time which is kinda weird since it should just read the sensors value once when it changes state. However if I just put pass into the else statement then it actually works just as I want it to work and no error message is given so problem solved. Thank you.

I have similar problem. I’m using raycast to detect parts from rack so I can grab parts to container in my crane. I append detected parts into list at OnSignal. Problem is that I get ‘NoneType’ object at list and this doesn’t happen every time. Some times simulation runs fine for 200 hours and sometimes it returns this error in the beginning of simulation.

For RayCast im using sampling at the rate of 0.1 seconds.

here’s the on signal which I am using:

Signal1=comp.findBehaviour("CompSignal")
def OnSignal( signal ):
  if signal == Signal1 and signal.Value is not None:
    RollStack.append(signal.Value)
  else:
    pass

EDIT: Found my error. Sensor was reading components of the rack. Responsible for fault :raising_hand_man: