AttributeError when moving object with script I want to select an imported object and move it to a specific location. The object is imported in an earlier part of the script. After the import i move it with the following part of the script.

import bpy

# Select object i want to move
bpy.data.objects["Cube"].select = True

# Move Origin to geometry and move object to location
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

bpy.context.object.location[0] = 0
bpy.context.object.location[1] = 0
bpy.context.object.location[2] = 0
bpy.context.object.rotation_euler[0] = 0
bpy.context.object.rotation_euler[1] = 0
bpy.context.object.rotation_euler[2] = 0


When the script is on position bpy.context.object.location[0] = 0 console gives following error:

Traceback (most recent call last):
  File "&lt;blender_console&gt;", line 1, in &lt;module&gt;
AttributeError: 'NoneType' object has no attribute 'location'


I understand the message, that my Cube has no location attributes. But he must have them. When i select him with right mouse and type bpy.context.object.location[0] = 0, no error is given and he moves to the position i want.

Do i have an error in the selection-part of my script? How can i solve this?
When you select multiple objects one of them is highlighted in a lighter colour. This "lighter" object is known as the active object and is the object that bpy.context.object refers to. This active object may be just one of many selected objects that is listed in bpy.context.selected_objects.

When manipulating objects using python the active object can be left undefined or no longer pointing to a valid object. In this situation you will get the 'NoneType' object error which is saying that the variable you used (bpy.context.object) is a NoneType or null and non existent object so has no location attribute (or any other attributes).

Using bpy.context.object and bpy.context.selected_objects are convenience properties that allows a script to dynamically work with the currently selected objects and allow it to differentiate the one active object. An example is the copy attributes addon which can take a property from the active object and apply it to the rest of the selected objects.

If you know the name of the object you want your script to work on then accessing it through bpy.data.objects['name'] will reliably refer to that object every time. If you want to use an operator that works on the active object, then you can set the active object with bpy.context.scene.objects.active = bpy.data.objects['name']