0
$\begingroup$

I made this custom exporter for my game and now I want to add an option to convert every file in a directory to my custom format. The problem is that the bpy.ops.import_scene.gltf function throws an exception when called.

What's weird is that if I run this code, which is the same as the batch_conversion function in the plugin, in the scripting tab it works as expected:

import bpy
import os

batch_path = "E:/Assets/test"
for root, dirs, files in os.walk(batch_path):
    for f in files:
        filename, extension = os.path.splitext(f)

        if extension == ".glb" or extension == ".gltf":

            bpy.ops.object.mode_set(mode='OBJECT')
            bpy.ops.object.select_all(action="SELECT")
            bpy.ops.object.delete()

            import_file = os.path.join(batch_path, f)
            print(f"Importing: {import_file}")
            bpy.ops.import_scene.gltf(filepath=import_file)

I'm using blender 4.1. Here's the exception that is thrown:

Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\__init__.py", line 2012, in execute
    return self.import_gltf2(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\__init__.py", line 2045, in import_gltf2
    return self.unit_import(self.filepath, import_settings)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\__init__.py", line 2060, in unit_import
    BlenderGlTF.create(gltf_importer)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 35, in create
    BlenderGlTF._create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 42, in _create
    BlenderScene.create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_scene.py", line 35, in create
    BlenderNode.create_vnode(gltf, 'root')
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 46, in create_vnode
    BlenderNode.create_vnode(gltf, child)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 32, in create_vnode
    obj = BlenderNode.create_object(gltf, vnode_id)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 81, in create_object
    BlenderNode.armature_display(gltf, obj)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 142, in armature_display
    bpy.data.collections[BLENDER_GLTF_SPECIAL_COLLECTION].objects.link(bpy.context.object)
TypeError: CollectionObjects.link(): error with argument 1, "object" -  Function.object does not support a 'None' assignment Object type
Error: Python: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\__init__.py", line 2060, in unit_import
    BlenderGlTF.create(gltf_importer)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 35, in create
    BlenderGlTF._create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 42, in _create
    BlenderScene.create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_scene.py", line 35, in create
    BlenderNode.create_vnode(gltf, 'root')
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 46, in create_vnode
    BlenderNode.create_vnode(gltf, child)
  File "C:\Program Files\Blender Foundation\BlendTraceback (most recent call last):
  File "C:\Users\marce\AppData\Roaming\Blender Foundation\Blender\4.1\scripts\addons\blender_exporter.py", line 364, in execute
    batch_conversion(context)
  File "C:\Users\marce\AppData\Roaming\Blender Foundation\Blender\4.1\scripts\addons\blender_exporter.py", line 316, in batch_conversion
    bpy.ops.import_scene.gltf(filepath=import_file)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\modules\bpy\ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Error: Python: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\__init__.py", line 2060, in unit_import
    BlenderGlTF.create(gltf_importer)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 35, in create
    BlenderGlTF._create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 42, in _create
    BlenderScene.create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_scene.py", line 35, in create
    BlenderNode.create_vnode(gltf, 'root')
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 46, in create_vnode
    BlenderNode.create_vnode(gltf, child)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 32, in create_vnode
    obj = BlenderNode.create_object(gltf, vnode_id)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 81, in create_object
    BlenderNode.armature_display(gltf, obj)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 142, in armature_display
    bpy.data.collections[BLENDER_GLTF_SPECIAL_COLLECTION].objects.link(bpy.context.object)
TypeError: CollectionObjects.link(): error with argument 1, "object" -  Function.object does not support a 'None' assignment Object type
Location: C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\modules\bpy\ops.py:109

Error: Python: Traceback (most recent call last):
  File "C:\Users\marce\AppData\Roaming\Blender Foundation\Blender\4.1\scripts\addons\blender_exporter.py", line 364, in execute
    batch_conversion(context)
  File "C:\Users\marce\AppData\Roaming\Blender Foundation\Blender\4.1\scripts\addons\blender_exporter.py", line 316, in batch_conversion
    bpy.ops.import_scene.gltf(filepath=import_file)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\modules\bpy\ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Error: Python: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\__init__.py", line 2060, in unit_import
    BlenderGlTF.create(gltf_importer)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 35, in create
    BlenderGlTF._create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 42, in _create
    BlenderScene.create(gltf)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_scene.py", line 35, in create
    BlenderNode.create_vnode(gltf, 'root')
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 46, in create_vnode
    BlenderNode.create_vnode(gltf, child)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 32, in create_vnode
    obj = BlenderNode.create_object(gltf, vnode_id)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 81, in create_object
    BlenderNode.armature_display(gltf, obj)
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_node.py", line 142, in armature_display
    bpy.data.collections[BLENDER_GLTF_SPECIAL_COLLECTION].objects.link(bpy.context.object)
TypeError: CollectionObjects.link(): error with argument 1, "object" -  Function.object does not support a 'None' assignment Object type
Location: C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\modules\bpy\ops.py:109

The models I'm trying to import are from here.

What may be causing this?

$\endgroup$

0

You must log in to answer this question.

Browse other questions tagged .