main.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*jshint esversion: 6 */
  2. const three = require("./threejs/blend_three.js");
  3. const parser = require("./parser/parser.js")();
  4. function loadFile(blender_file, res, rej){
  5. three_module = three(blender_file);
  6. //TODO: Report any errors with ThreeJS before continuing.
  7. res({
  8. file : blender_file,
  9. three : three_module
  10. });
  11. }
  12. /* This represents a parsed blendfile instance if parsing is successful. It will accept a string or a binary data object. Strings must be a valid URI to a blender file. Binary data may be in the form of an ArrayBuffer, TypedArray, or a Blob. Binary data must also contain the binary data of a blender file.*/
  13. JSBLEND = (fileuri_or_filedata, name = "")=>{
  14. const promise = new Promise(
  15. (res, rej) =>{
  16. parser.onParseReady = (blender_file, error) => {
  17. if(error){
  18. rej(error);
  19. }else{
  20. loadFile(blender_file, res, rej);
  21. }
  22. };
  23. //If fileuri_or_filedata is a string, attempt to load the file asynchronously
  24. if(typeof fileuri_or_filedata == "string"){
  25. let request = new XMLHttpRequest();
  26. request.open("GET", fileuri_or_filedata, true);
  27. request.responseType = 'blob';
  28. request.onload = () => {
  29. let file = request.response;
  30. parser.loadBlendFromBlob(new Blob([file]), fileuri_or_filedata);
  31. };
  32. request.send();
  33. return;
  34. }
  35. if(typeof fileuri_or_filedata == "object"){
  36. //Attempt to load from blob or array buffer;
  37. if(fileuri_or_filedata instanceof ArrayBuffer){
  38. parser.loadBlendFromArrayBuffer(fileuri_or_filedata, name);
  39. return;
  40. }
  41. if(fileuri_or_filedata instanceof Blob){
  42. parser.loadBlendFromBlob(fileuri_or_filedata, name);
  43. return;
  44. }
  45. }
  46. //Unknown file type passed -> abort and reject
  47. rej("Unsupported file type passed to JSBlend ${fileuri_or_filedata}");
  48. }
  49. );
  50. return promise;
  51. };