common blender python error messages

--python-console

enter the blender command line with:
blender --background --python-console
From the command prompt this will bring you into the python command line where you can test python script one line at a time. Always to start we must import the bpy module, this gives us access to blender functions so we can generate elements like: grease pencil objects, 3d meshes, animation, metaballs and more.
import bpy
bpy.data.objects[10]

bpy.data.objects[N]

Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'bpy_prop_collection[key]: key "10" not found'
Above is a very common error when writing bpy scripts. This error is similar to an index out of range error from a standard python list. When you encounter this error it means that you have requested an item by number index, and the number is greater than the number of items in the collection.

bpy.data.objects["N"]

KeyError: 'bpy_prop_collection[key]: key "N" not found'
Above is another common error that looks similar to a python dictionary lookup error. This error means that the requested object name is not in the current blender scene.

bpy_prop_collection

bpy_prop_collection is the standard collection type that blender provides in the python interface. It is a collection type that has features of both a python list and python dictionary. In other words, you can get objects by name or index. This has some major pros and cons, getting an object by name is not always consistent, because blender enforces unique names for each object, and when the user renames and object, if if the requested name already exists, blender will append .001 to the name. This complicates game logic that is depending on objects and getting them by name.

for object in bpy.data.objects

for ob in bpy.data.objects:
   print(ob.name)
   print(ob.location)
Above will loop thru all the objects in the current blender scene and print their name and location (position).

Comments

Popular Posts