1
$\begingroup$

I'm trying to script a plugin which imports some GLTF objects, positions them in the scene and reexports the scene as one gltf. But I'm running into a couple of problems:

  1. When I'm exporting the scene in gltf, there's no .bin and there are no textures written in the GLTF.

  2. I have tried to import and export a single GLTF, and I have noticed that the import shader is not the same as the export one... More precisely, the MetallicPBR node of this plugin https://github.com/ksons/gltf-blender-importer is different from the metallic roughness node of this plugin https://github.com/KhronosGroup/glTF-Blender-Exporter. I have noticed that when I re-plug the textures into the node appended from the Blender-exporter master, I have my textures exported and written into the gltf.

But it doesn't explain why I have no bin exported when I'm trying the first scenario.

So two questions: 1. Do you know why I haven't any .bin exported? 2. Is there a means to export my textures correctly without having to replug them in the code?

Here's my code:

import bpy
import os
import math
import shutil
import glob
import time

def Run():

    FolderSetup()
    Construct()
    Import(Construct.gltf_list, Construct.gltf_amount)
    Place()
    Export('.gltf')
    Clean()


def FolderSetup():
    Tempfolder = os.path.join('C:\\', 'Users', 'Maelys', 'Desktop', 'Validation-test', 'Temp')
    FolderSetup.rootfolder = os.path.join('C:\\', 'Users', 'Maelys', 'Desktop', 'Validation-test')
    file_tails = ["*.obj", "*.mtl", "*.jpeg", "*.jpg", "*.gltf", "*.bin", "*.png"]
    file_list=[]
    file_paths=[]
    folders = []


    file_tails_length = len(file_tails)

    for i in range (0,file_tails_length):
        file = file_tails[i]
        file_list.extend(glob.glob(os.path.join(Tempfolder, '**', file ), recursive=True))

    file_list_length = len(file_list)



    for i in range (0, file_list_length):

        file = file_list[i]
        filename = os.path.basename(file)
        filepath = os.path.realpath(file)
        dirname = os.path.dirname(os.path.abspath(filepath))
        folders.append(dirname)

        shutil.move(os.path.join(dirname, filename), os.path.join(FolderSetup.rootfolder, filename))


    shutil.rmtree(Tempfolder)




def Construct():
    Construct.MyScene = bpy.context.scene

    if "Cube" in Construct.MyScene.objects:
        bpy.data.objects["Cube"].select = True
        bpy.ops.object.delete()

    if "Lamp" in Construct.MyScene.objects:
        bpy.data.objects["Lamp"].select = True
        bpy.ops.object.delete()

    Construct.path_to_obj_dir = FolderSetup.rootfolder

    # get list of all files in directory
    file_list = sorted(os.listdir(Construct.path_to_obj_dir))

    # get a list of files ending in 'obj'
    Construct.obj_list = [item for item in file_list if item.endswith('.obj')]
    Construct.gltf_list = [item for item in file_list if item.endswith('.gltf')]
    Construct.obj_amount = len(Construct.obj_list)
    Construct.gltf_amount = len(Construct.gltf_list)



def Import(listtype, amount):

    # IMPORT OBJECTS FROM FOLDER AND ARRANGE THEM IN A LINE
    for i in range(0, amount):
        item = listtype[i]
        path_to_file = os.path.join(Construct.path_to_obj_dir, item)
        bpy.ops.import_scene.obj(filepath=path_to_file)

    Import.imported_objects = Construct.MyScene.objects

    print(listtype)

def Place():
    pos = [0, 0, 0]
    prevpos = pos
    prevsizeX = Import.imported_objects[0].dimensions.x
    prevsizeY = Import.imported_objects[0].dimensions.y

    for object in Import.imported_objects[1:]:
        sizeX = object.dimensions.x
        sizeY = object.dimensions.y

        pos[0] = prevpos[0] + (prevsizeX / 2) + (sizeX / 2) + 2
        bpy.context.scene.objects.active = object
        object.location.x = pos[0]
        prevpos = pos
        prevsizeX = sizeX
        prevsizeY = sizeY

    #
    # RECENTER OBJECTS

    view3d = bpy.context.screen.areas[4]
    context_override = {'window': bpy.context.window,
                        'screen': bpy.context.screen,
                        'area': view3d}
    bpy.ops.view3d.snap_cursor_to_center(context_override)

    bpy.ops.object.select_all(action='SELECT')

    bpy.ops.view3d.snap_selected_to_cursor(context_override, use_offset=True)


def Export(type):
    Export.time = time.strftime("%Y-%m-%d_%H%M") + "_"
    export_folderpath = Construct.path_to_obj_dir
    export_filepath = os.path.join(export_folderpath, Export.time + "Validation" + type)

    bpy.ops.object.select_all(action='SELECT')

    if type == '.obj':
        bpy.ops.export_scene.obj(filepath=export_filepath, use_selection=True)

    elif type == '.gltf':
        bpy.ops.export_scene.gltf(filepath=export_filepath)


def Clean():

    # CLEAN
    export_obj_list = []
    export_mtl_list = []
    export_gltf_list=[]
    export_bin_list=[]


    export_obj_list.extend(glob.glob(os.path.join(Construct.path_to_obj_dir, '*.obj')))
    export_mtl_list.extend(glob.glob(os.path.join(Construct.path_to_obj_dir, '*.mtl')))
    export_gltf_list.extend(glob.glob(os.path.join(Construct.path_to_obj_dir, '*.gltf')))
    export_bin_list.extend(glob.glob(os.path.join(Construct.path_to_obj_dir, '*.bin')))

    print(export_bin_list)

    for i in export_obj_list:
        filename = os.path.basename(i)

        if filename != Export.time + "Validation" + '.obj':
            os.remove(os.path.join(Construct.path_to_obj_dir, i))

    for j in export_mtl_list:
        filename = os.path.basename(j)
        if filename != Export.time + "Validation" + '.mtl':
            os.remove(os.path.join(Construct.path_to_obj_dir, j))


    for k in export_gltf_list:
        filename = os.path.basename(k)
        if filename != Export.time + "Validation" + '.gltf':
            os.remove(os.path.join(Construct.path_to_obj_dir,k))



    for l in export_bin_list:
        filename = os.path.basename(l)
        if filename != Export.time + "Validation" + '.bin':
            os.remove(os.path.join(Construct.path_to_obj_dir, l))






Run()

Thank you very much :)

$\endgroup$
  • $\begingroup$ For some reason I couldn't write "Hello" at the beginning of my post so Hello everyone :) $\endgroup$ – Popi Sep 6 '18 at 16:05
  • $\begingroup$ The current glTF importer/exporter includes a test suite that uses Mocha and Docker to run Blender from the command line and automatically round-trips some glTFs for testing. It's complex but it could be good reference for how it can be done. $\endgroup$ – emackey Feb 16 '19 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.