Script to export gltf fails with 'Context' object has no attribute 'active_object' I'm trying to convert a batch of files to glb using blender with the script bellow. That i modified from How to batch convert between file formats?
But i'm getting the error

'Context' object has no attribute 'active_object'

If I change the output to obj it works with no problem
I'm using blender v2.81a
CONVERT_DIR = &quot;my/dir&quot;

import os

def file_iter(path, ext):
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            ext = os.path.splitext(filename)[1]
            if ext.lower().endswith(ext):
                yield os.path.join(dirpath, filename)

import bpy


def reset_blend():
    bpy.ops.wm.read_factory_settings(use_empty=True)

def convert_recursive(base_path):
    for filepath_src in file_iter(base_path, &quot;.fbx&quot;):
        filepath_dst = os.path.splitext(filepath_src)[0] + &quot;.glb&quot;

        print(&quot;Converting %r -&gt; %r&quot; % (filepath_src, filepath_dst))

        reset_blend()

        bpy.ops.import_scene.fbx(filepath=filepath_src)

        bpy.ops.export_scene.gltf(export_format=&quot;GLB&quot;,filepath=filepath_dst)


if __name__ == &quot;__main__&quot;:
    convert_recursive(CONVERT_DIR)


But it fails with the error bellow
Traceback (most recent call last):
  File &quot;/Text&quot;, line 32, in &lt;module&gt;
  File &quot;/Text&quot;, line 28, in convert_recursive
  File &quot;/home/user/App/blender-2.81a-linux-glibc217-x86_64/2.81/scripts/modules/bpy/ops.py&quot;, line 201, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File &quot;/home/user/App/blender-2.81a-linux-glibc217-x86_64/2.81/scripts/addons/io_scene_gltf2/__init__.py&quot;, line 483, in execute
    return gltf2_blender_export.save(context, export_settings)
  File &quot;/home/user/App/blender-2.81a-linux-glibc217-x86_64/2.81/scripts/addons/io_scene_gltf2/blender/exp/gltf2_blender_export.py&quot;, line 31, in save
    if bpy.context.active_object is not None:
AttributeError: 'Context' object has no attribute 'active_object'

location: /home/user/App/blender-2.81a-linux-glibc217-x86_64/2.81/scripts/modules/bpy/ops.py:201



location: &lt;unknown location&gt;:-1
Creating a fake active object attribute on the context and passing it to the operator should work. Try this:
CONVERT_DIR = &quot;my/dir&quot;

import os

def file_iter(path, ext):
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            ext = os.path.splitext(filename)[1]
            if ext.lower().endswith(ext):
                yield os.path.join(dirpath, filename)

import bpy


def reset_blend():
    bpy.ops.wm.read_factory_settings(use_empty=True)

def convert_recursive(base_path):
    for filepath_src in file_iter(base_path, &quot;.fbx&quot;):
        filepath_dst = os.path.splitext(filepath_src)[0] + &quot;.glb&quot;

        print(&quot;Converting %r -&gt; %r&quot; % (filepath_src, filepath_dst))

        reset_blend()

        bpy.ops.import_scene.fbx(filepath=filepath_src)
        
        # create a copy of the context
        ctx = bpy.context.copy()
        
        # because the active_object attribute isn't created until the user interacts
        # with the scene we create one here but we don't need to set it to anything
        ctx['active_object'] = None

        # pass our context copy with active object attribute to the operator
        bpy.ops.export_scene.gltf(ctx, export_format=&quot;GLB&quot;,filepath=filepath_dst)


if __name__ == &quot;__main__&quot;:
    convert_recursive(CONVERT_DIR)