How to convert Blender file to j30 binary *outside* of the SDK/IDE?

This is what we do in our editor.

At the moment we only really use it to convert .obj files but it should handle whatever you want and have a loader for. Use as you wish if you wish.

Also note that it registers a FileLocator path for the parent folder of the input file and then at the end it unregisters it.

import com.jme3.asset.AssetManager;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.scene.Spatial;
import com.jme3.util.TangentBinormalGenerator;

import java.io.File;
import java.io.IOException;

public class ModelConverter {

    private final AssetManager assetManager;

    public ModelConverter(final AssetManager assetManager) {
        this.assetManager = assetManager;
    }

    public void convert(final File inputFile, final File outputFile, final boolean generateTangents) {
        final String parentPath = inputFile.getParent();
        assetManager.registerLocator(parentPath, FileLocator.class);

        try {
            final Spatial modelToImport = assetManager.loadModel(inputFile.getName());

            if (modelToImport != null) {
                if (generateTangents) {
                    TangentBinormalGenerator.generate(modelToImport);
                }

                final BinaryExporter binaryExporter = new BinaryExporter();

                if (outputFile.getParentFile().exists() || outputFile.getParentFile().mkdirs()) {
                    try {
                        binaryExporter.save(modelToImport, outputFile);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        } finally {
            assetManager.unregisterLocator(parentPath, FileLocator.class);
        }
    }
}
1 Like