1
$\begingroup$

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 = "my/dir"

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, ".fbx"):
        filepath_dst = os.path.splitext(filepath_src)[0] + ".glb"

        print("Converting %r -> %r" % (filepath_src, filepath_dst))

        reset_blend()

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

        bpy.ops.export_scene.gltf(export_format="GLB",filepath=filepath_dst)


if __name__ == "__main__":
    convert_recursive(CONVERT_DIR)

But it fails with the error bellow

Traceback (most recent call last):
  File "/Text", line 32, in <module>
  File "/Text", line 28, in convert_recursive
  File "/home/user/App/blender-2.81a-linux-glibc217-x86_64/2.81/scripts/modules/bpy/ops.py", line 201, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File "/home/user/App/blender-2.81a-linux-glibc217-x86_64/2.81/scripts/addons/io_scene_gltf2/__init__.py", line 483, in execute
    return gltf2_blender_export.save(context, export_settings)
  File "/home/user/App/blender-2.81a-linux-glibc217-x86_64/2.81/scripts/addons/io_scene_gltf2/blender/exp/gltf2_blender_export.py", 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: <unknown location>:-1


$\endgroup$
1
  • $\begingroup$ As a workaround, I think it will work if you avoid bpy.ops.wm.read_factory_settings. $\endgroup$
    – scurest
    Commented Nov 2, 2020 at 5:40

1 Answer 1

1
$\begingroup$

Creating a fake active object attribute on the context and passing it to the operator should work. Try this:

CONVERT_DIR = "my/dir"

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, ".fbx"):
        filepath_dst = os.path.splitext(filepath_src)[0] + ".glb"

        print("Converting %r -> %r" % (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="GLB",filepath=filepath_dst)


if __name__ == "__main__":
    convert_recursive(CONVERT_DIR)
$\endgroup$
1
  • $\begingroup$ It works! Thanks! $\endgroup$
    – Renas
    Commented Nov 2, 2020 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .