[SOLVED] Programmatically loading textures into the Editor

I’m having trouble loading in images into the editor from code. I mostly have it working, but the image assets have a few quirks, for example their thumbnails are missing. This is the code I am using:

var loadImage = function(image, onComplete){
	var oReq = new XMLHttpRequest();
	oReq.open("GET", server + image.url, true);
	oReq.responseType = "arraybuffer";

	oReq.onload = function(oEvent) {
	  
	  var assetData = {
			name: image.fileName,
			type: 'texture',
			preload: false,
			source: false,
			file: new Blob([oReq.response], {type: "image/png"}),
            scope: {
                type: 'project',
                id: config.project.id
            }
		};

		editor.call('assets:create', assetData, function(err, id){
			var asset = editor.call('assets:get',id);
			console.log(err, id, asset);
			if(onComplete !== void 0)
				onComplete();
		});
	};

	oReq.send();
};
2 Likes

Instead of assets:create try something like this instead:

editor.call('assets:uploadFile', {
    file: blob,
    name: name, // must end with valid texture extension e.g. myfile.jpg
    parent: editor.call('assets:panel:currentFolder'),
    pipeline: true,
}, function(err, data) {
    console.log(data.asset.id);
});
2 Likes

@vaios This works - thanks!