TransWikia.com

Script to export gltf fails with 'Context' object has no attribute 'active_object'

Blender Asked by Renas on January 3, 2021

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


One Answer

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)

Correct answer by Richard Rose on January 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP