# “'NoneType' object has no attribute 'action'” error when looping over objects with Python

I keep getting an error when running this. Without the first for loop it does exactly what I needed, but i need it to work on all selected objects, or objects in scene. I am still very new to python. any help is appreciated.

sel_objs = [obj for obj in bpy.context.selected_objects]
for obj in sel_objs:
obj.select = True
ob = bpy.context.active_object
action = ob.animation_data.action
keyframes = []
for fcu in action.fcurves:
for kp in fcu.keyframe_points:
if kp.co[0] not in keyframes:
keyframes.append(kp.co[0])
with open('blender_data.txt' , 'w') as f:
for i in range(len(keyframes)):
k = keyframes[i]
bpy.context.scene.frame_set(k)
f.write(ob.name + ', ')
f.write(str(float(ob.location.x)) + ', ' + str(float(ob.location.y)) + ', ' + str(float(ob.location.z)) + ', ')
f.write(str(float(ob.scale.x)) + ', ' + str(float(ob.scale.y)) + ', ' + str(float(ob.scale.z)) + ', ')
f.write('%s \n' % (k))
obj.select = False


Here is the error I am getting

Traceback (most recent call last):
File "<blender_console>", line 4, in <module>
AttributeError: 'NoneType' object has no attribute 'action'


I seem to have the same problem as this looping through selected objects one at a time but his solution did not work, or I dont fully understand. appreciate any input or direction.

• Please state you Python experience. For example 0 month, 4 months , 8 months, 12 months. Does the object in question have any keyframes set? – atomicbezierslinger Mar 18 '19 at 16:48
• Do all objects have keyframes set? – atomicbezierslinger Mar 18 '19 at 16:54
• They do not all have key frames. and experience is like 4 days – Nburgess Mar 18 '19 at 17:16
• adding keyframes to each objcet removed the error thank you for your help – Nburgess Mar 18 '19 at 17:41
• 4 days. All the fun yet to arrive. – atomicbezierslinger Mar 18 '19 at 17:44

Add keyframes to all objects. Keyframes are associated with actions. Simplified explanation.

You have stated you 4 days of experience with Python, if I understand you correctly. Thus my answer is based on working with that context. Keyframes may be more familiar to you than Python. You may prefer to know more about Python slowly. The choice is yours.

Remember you can pose related [idea questions] here at BSE with a single web page but try to limit it to a reasonable small task.

On the other hand some people actually get lucky and get people to write the entire script for them.

The following search here at Blender Stack Exchange with keywords

animation_data is not none

can you show you different approaches to handling the problem situation via Python.

I do have concern about the Python in the question with regard to file writing. But you may consider asking that as a separate question. One file name is used for writing in the Python at this point. The file seems to be rewritten every time with only the last write being actually being persistent. That seems defective. I imagine you want to have data for all objects in the same file.

I suggest when the file writing is top priority, you pose another question. The changes needed seem achievable in 88 seconds. You should consider how many times you open the file. I believe it should be one file open per invocation. You know how to use for loops so if you think about it you will find the better placement of existing Python.

The file mode is 'w' which can mean ... delete and create fresh. I would suggest you change it to 'a' for append. Once the code is more correct you may decide to use 'w' with a different filename everytime. The filename can include the time and date. blender_data_D20180318_T120037.txt as as example you can improve. D for Date. T for time including seconds. Thus you would have a history to compare file versions.

You can delete parts of the file or the whole file for testing. I have not executed the Python so this is just a computer simulation mental simulation. Currently the Python seems to write info for the last object only which seems defective.