How to hack gltf exporter so that it writes its output to a string variable instead of the file?

I have a similar problem which is described in the following link.

I want to redirect gltf exporter’s output to a variable instead of file.

How can I achieve this?

You could write it out to a temp directory and read the file back as a variable. Gltf can produce more than one file though (json, binary, and textures) once you load the json payload you can load the dependent data. If you are using glb then you will have too seek though the gltf binary header for the right offsets to your json and binary payload.

Thank you for your response, but I did not like your solution. I do not want to write it to disk and read it back, your solution is a little bid time consuming for me. I’d rather read the json part of gltf directly to a variable.

There’s no straightforward way except for mapping a directory to ram. On linux it’s possible with tmpfs.

If we are only limited to Python, I’d substitute the function that writes gltf to a file. It is in

blender_folder/4.0/scripts/addons/io_scene_gltf2/io/exp/gltf2_io_export.py

You have to either modify this code inplace or write your own substitution and apply it before exporting any gltf:

def save_gltf(gltf, export_settings, encoder, glb_buffer):
    print('Hijacked!')

import io_scene_gltf2.io.exp.gltf2_io_export as gltf2io
gltf2io.save_gltf = save_gltf

That’s very-very fragile tho

1 Like

Thank you, it seems your solution solved my problem. But I want to know, why your solution is very-very fragile? May it cause some problems?

Just because there’s no guarantee that it will work on every version of Blender and I feel uncomfortable changing anything inside its guts in this hacky way.

1 Like

Thank you anyway, you solved my problem.