What is different between render by script and by console in blender

I have a script.

import bpy

obj = bpy.context.object

for action in bpy.data.actions:
	obj.animation_data.action = bpy.data.actions.get(action.name)

	bpy.context.scene.render.filepath = './render/' + action.name+'_'
	bpy.ops.render.render(use_viewport = True, write_still=True, animation=True)

and I try run it by command line:

blender -b 01_a_camera.blend -P render_action.py

I got an error mes:

Read prefs: /home/vietnam/.config/blender/2.79/config/userpref.blend
found bundled python: /snap/blender/20/2.79/python

Read blend: /home/vietnam/Downloads/01_a_camera.blend
Traceback (most recent call last):
  File "/home/vietnam/Downloads/render_action.py", line 4, in <module>
    obj.animation_data.action = bpy.data.actions.get(action.name)
AttributeError: 'NoneType' object has no attribute 'action'

Blender quit

But when I run that script by console or by Text editor on blender file, It works normally. What is the problem here?

I think problem is that when you run blender in console, it has no active object context and thus bpy.context.object returns None, which in turn causes your error that NoneType has no attribute ‘action’. You should specify your object some other way, finding it by name would be one way.

1 Like

oh, I resolved it.
Thank you very much