resource_importer_obj.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /**************************************************************************/
  2. /* resource_importer_obj.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "resource_importer_obj.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_saver.h"
  33. #include "scene/3d/importer_mesh_instance_3d.h"
  34. #include "scene/3d/mesh_instance_3d.h"
  35. #include "scene/3d/node_3d.h"
  36. #include "scene/resources/importer_mesh.h"
  37. #include "scene/resources/mesh.h"
  38. #include "scene/resources/surface_tool.h"
  39. uint32_t EditorOBJImporter::get_import_flags() const {
  40. return IMPORT_SCENE;
  41. }
  42. static Error _parse_material_library(const String &p_path, HashMap<String, Ref<StandardMaterial3D>> &material_map, List<String> *r_missing_deps) {
  43. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  44. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Couldn't open MTL file '%s', it may not exist or not be readable.", p_path));
  45. Ref<StandardMaterial3D> current;
  46. String current_name;
  47. String base_path = p_path.get_base_dir();
  48. while (true) {
  49. String l = f->get_line().strip_edges();
  50. if (l.begins_with("newmtl ")) {
  51. //vertex
  52. current_name = l.replace("newmtl", "").strip_edges();
  53. current.instantiate();
  54. current->set_name(current_name);
  55. material_map[current_name] = current;
  56. } else if (l.begins_with("Ka ")) {
  57. //uv
  58. WARN_PRINT("OBJ: Ambient light for material '" + current_name + "' is ignored in PBR");
  59. } else if (l.begins_with("Kd ")) {
  60. //normal
  61. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  62. Vector<String> v = l.split(" ", false);
  63. ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);
  64. Color c = current->get_albedo();
  65. c.r = v[1].to_float();
  66. c.g = v[2].to_float();
  67. c.b = v[3].to_float();
  68. current->set_albedo(c);
  69. } else if (l.begins_with("Ks ")) {
  70. //normal
  71. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  72. Vector<String> v = l.split(" ", false);
  73. ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);
  74. float r = v[1].to_float();
  75. float g = v[2].to_float();
  76. float b = v[3].to_float();
  77. float metalness = MAX(r, MAX(g, b));
  78. current->set_metallic(metalness);
  79. } else if (l.begins_with("Ns ")) {
  80. //normal
  81. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  82. Vector<String> v = l.split(" ", false);
  83. ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
  84. float s = v[1].to_float();
  85. current->set_metallic((1000.0 - s) / 1000.0);
  86. } else if (l.begins_with("d ")) {
  87. //normal
  88. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  89. Vector<String> v = l.split(" ", false);
  90. ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
  91. float d = v[1].to_float();
  92. Color c = current->get_albedo();
  93. c.a = d;
  94. current->set_albedo(c);
  95. if (c.a < 0.99) {
  96. current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  97. }
  98. } else if (l.begins_with("Tr ")) {
  99. //normal
  100. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  101. Vector<String> v = l.split(" ", false);
  102. ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
  103. float d = v[1].to_float();
  104. Color c = current->get_albedo();
  105. c.a = 1.0 - d;
  106. current->set_albedo(c);
  107. if (c.a < 0.99) {
  108. current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  109. }
  110. } else if (l.begins_with("map_Ka ")) {
  111. //uv
  112. WARN_PRINT("OBJ: Ambient light texture for material '" + current_name + "' is ignored in PBR");
  113. } else if (l.begins_with("map_Kd ")) {
  114. //normal
  115. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  116. String p = l.replace("map_Kd", "").replace("\\", "/").strip_edges();
  117. String path;
  118. if (p.is_absolute_path()) {
  119. path = p;
  120. } else {
  121. path = base_path.path_join(p);
  122. }
  123. Ref<Texture2D> texture = ResourceLoader::load(path);
  124. if (texture.is_valid()) {
  125. current->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, texture);
  126. } else if (r_missing_deps) {
  127. r_missing_deps->push_back(path);
  128. }
  129. } else if (l.begins_with("map_Ks ")) {
  130. //normal
  131. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  132. String p = l.replace("map_Ks", "").replace("\\", "/").strip_edges();
  133. String path;
  134. if (p.is_absolute_path()) {
  135. path = p;
  136. } else {
  137. path = base_path.path_join(p);
  138. }
  139. Ref<Texture2D> texture = ResourceLoader::load(path);
  140. if (texture.is_valid()) {
  141. current->set_texture(StandardMaterial3D::TEXTURE_METALLIC, texture);
  142. } else if (r_missing_deps) {
  143. r_missing_deps->push_back(path);
  144. }
  145. } else if (l.begins_with("map_Ns ")) {
  146. //normal
  147. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  148. String p = l.replace("map_Ns", "").replace("\\", "/").strip_edges();
  149. String path;
  150. if (p.is_absolute_path()) {
  151. path = p;
  152. } else {
  153. path = base_path.path_join(p);
  154. }
  155. Ref<Texture2D> texture = ResourceLoader::load(path);
  156. if (texture.is_valid()) {
  157. current->set_texture(StandardMaterial3D::TEXTURE_ROUGHNESS, texture);
  158. } else if (r_missing_deps) {
  159. r_missing_deps->push_back(path);
  160. }
  161. } else if (l.begins_with("map_bump ")) {
  162. //normal
  163. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  164. String p = l.replace("map_bump", "").replace("\\", "/").strip_edges();
  165. String path = base_path.path_join(p);
  166. Ref<Texture2D> texture = ResourceLoader::load(path);
  167. if (texture.is_valid()) {
  168. current->set_feature(StandardMaterial3D::FEATURE_NORMAL_MAPPING, true);
  169. current->set_texture(StandardMaterial3D::TEXTURE_NORMAL, texture);
  170. } else if (r_missing_deps) {
  171. r_missing_deps->push_back(path);
  172. }
  173. } else if (f->eof_reached()) {
  174. break;
  175. }
  176. }
  177. return OK;
  178. }
  179. static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_single_mesh, bool p_generate_tangents, bool p_optimize, Vector3 p_scale_mesh, Vector3 p_offset_mesh, List<String> *r_missing_deps) {
  180. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  181. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Couldn't open OBJ file '%s', it may not exist or not be readable.", p_path));
  182. // Avoid trying to load/interpret potential build artifacts from Visual Studio (e.g. when compiling native plugins inside the project tree)
  183. // This should only match, if it's indeed a COFF file header
  184. // https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
  185. const int first_bytes = f->get_16();
  186. static const Vector<int> coff_header_machines{
  187. 0x0, // IMAGE_FILE_MACHINE_UNKNOWN
  188. 0x8664, // IMAGE_FILE_MACHINE_AMD64
  189. 0x1c0, // IMAGE_FILE_MACHINE_ARM
  190. 0x14c, // IMAGE_FILE_MACHINE_I386
  191. 0x200, // IMAGE_FILE_MACHINE_IA64
  192. };
  193. ERR_FAIL_COND_V_MSG(coff_header_machines.find(first_bytes) != -1, ERR_FILE_CORRUPT, vformat("Couldn't read OBJ file '%s', it seems to be binary, corrupted, or empty.", p_path));
  194. f->seek(0);
  195. Ref<ArrayMesh> mesh;
  196. mesh.instantiate();
  197. bool generate_tangents = p_generate_tangents;
  198. Vector3 scale_mesh = p_scale_mesh;
  199. Vector3 offset_mesh = p_offset_mesh;
  200. int mesh_flags = 0;
  201. Vector<Vector3> vertices;
  202. Vector<Vector3> normals;
  203. Vector<Vector2> uvs;
  204. Vector<Color> colors;
  205. const String default_name = "Mesh";
  206. String name = default_name;
  207. HashMap<String, HashMap<String, Ref<StandardMaterial3D>>> material_map;
  208. Ref<SurfaceTool> surf_tool = memnew(SurfaceTool);
  209. surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
  210. String current_material_library;
  211. String current_material;
  212. String current_group;
  213. uint32_t smooth_group = 0;
  214. bool smoothing = true;
  215. const uint32_t no_smoothing_smooth_group = (uint32_t)-1;
  216. while (true) {
  217. String l = f->get_line().strip_edges();
  218. while (l.length() && l[l.length() - 1] == '\\') {
  219. String add = f->get_line().strip_edges();
  220. l += add;
  221. if (add.is_empty()) {
  222. break;
  223. }
  224. }
  225. if (l.begins_with("v ")) {
  226. //vertex
  227. Vector<String> v = l.split(" ", false);
  228. ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
  229. Vector3 vtx;
  230. vtx.x = v[1].to_float() * scale_mesh.x + offset_mesh.x;
  231. vtx.y = v[2].to_float() * scale_mesh.y + offset_mesh.y;
  232. vtx.z = v[3].to_float() * scale_mesh.z + offset_mesh.z;
  233. vertices.push_back(vtx);
  234. //vertex color
  235. if (v.size() >= 7) {
  236. while (colors.size() < vertices.size() - 1) {
  237. colors.push_back(Color(1.0, 1.0, 1.0));
  238. }
  239. Color c;
  240. c.r = v[4].to_float();
  241. c.g = v[5].to_float();
  242. c.b = v[6].to_float();
  243. colors.push_back(c);
  244. } else if (!colors.is_empty()) {
  245. colors.push_back(Color(1.0, 1.0, 1.0));
  246. }
  247. } else if (l.begins_with("vt ")) {
  248. //uv
  249. Vector<String> v = l.split(" ", false);
  250. ERR_FAIL_COND_V(v.size() < 3, ERR_FILE_CORRUPT);
  251. Vector2 uv;
  252. uv.x = v[1].to_float();
  253. uv.y = 1.0 - v[2].to_float();
  254. uvs.push_back(uv);
  255. } else if (l.begins_with("vn ")) {
  256. //normal
  257. Vector<String> v = l.split(" ", false);
  258. ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
  259. Vector3 nrm;
  260. nrm.x = v[1].to_float();
  261. nrm.y = v[2].to_float();
  262. nrm.z = v[3].to_float();
  263. normals.push_back(nrm);
  264. } else if (l.begins_with("f ")) {
  265. //vertex
  266. Vector<String> v = l.split(" ", false);
  267. ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
  268. //not very fast, could be sped up
  269. Vector<String> face[3];
  270. face[0] = v[1].split("/");
  271. face[1] = v[2].split("/");
  272. ERR_FAIL_COND_V(face[0].size() == 0, ERR_FILE_CORRUPT);
  273. ERR_FAIL_COND_V(face[0].size() != face[1].size(), ERR_FILE_CORRUPT);
  274. for (int i = 2; i < v.size() - 1; i++) {
  275. face[2] = v[i + 1].split("/");
  276. ERR_FAIL_COND_V(face[0].size() != face[2].size(), ERR_FILE_CORRUPT);
  277. for (int j = 0; j < 3; j++) {
  278. int idx = j;
  279. if (idx < 2) {
  280. idx = 1 ^ idx;
  281. }
  282. if (face[idx].size() == 3) {
  283. int norm = face[idx][2].to_int() - 1;
  284. if (norm < 0) {
  285. norm += normals.size() + 1;
  286. }
  287. ERR_FAIL_INDEX_V(norm, normals.size(), ERR_FILE_CORRUPT);
  288. surf_tool->set_normal(normals[norm]);
  289. }
  290. if (face[idx].size() >= 2 && !face[idx][1].is_empty()) {
  291. int uv = face[idx][1].to_int() - 1;
  292. if (uv < 0) {
  293. uv += uvs.size() + 1;
  294. }
  295. ERR_FAIL_INDEX_V(uv, uvs.size(), ERR_FILE_CORRUPT);
  296. surf_tool->set_uv(uvs[uv]);
  297. }
  298. int vtx = face[idx][0].to_int() - 1;
  299. if (vtx < 0) {
  300. vtx += vertices.size() + 1;
  301. }
  302. ERR_FAIL_INDEX_V(vtx, vertices.size(), ERR_FILE_CORRUPT);
  303. Vector3 vertex = vertices[vtx];
  304. if (!colors.is_empty()) {
  305. surf_tool->set_color(colors[vtx]);
  306. }
  307. surf_tool->set_smooth_group(smoothing ? smooth_group : no_smoothing_smooth_group);
  308. surf_tool->add_vertex(vertex);
  309. }
  310. face[1] = face[2];
  311. }
  312. } else if (l.begins_with("s ")) { //smoothing
  313. String what = l.substr(2, l.length()).strip_edges();
  314. bool do_smooth;
  315. if (what == "off") {
  316. do_smooth = false;
  317. } else {
  318. do_smooth = true;
  319. }
  320. if (do_smooth != smoothing) {
  321. smoothing = do_smooth;
  322. if (smoothing) {
  323. smooth_group++;
  324. }
  325. }
  326. } else if (/*l.begins_with("g ") ||*/ l.begins_with("usemtl ") || (l.begins_with("o ") || f->eof_reached())) { //commit group to mesh
  327. //groups are too annoying
  328. if (surf_tool->get_vertex_array().size()) {
  329. //another group going on, commit it
  330. if (normals.size() == 0) {
  331. surf_tool->generate_normals();
  332. }
  333. if (generate_tangents && uvs.size()) {
  334. surf_tool->generate_tangents();
  335. }
  336. surf_tool->index();
  337. print_verbose("OBJ: Current material library " + current_material_library + " has " + itos(material_map.has(current_material_library)));
  338. print_verbose("OBJ: Current material " + current_material + " has " + itos(material_map.has(current_material_library) && material_map[current_material_library].has(current_material)));
  339. if (material_map.has(current_material_library) && material_map[current_material_library].has(current_material)) {
  340. Ref<StandardMaterial3D> &material = material_map[current_material_library][current_material];
  341. if (!colors.is_empty()) {
  342. material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  343. }
  344. surf_tool->set_material(material);
  345. }
  346. mesh = surf_tool->commit(mesh, mesh_flags);
  347. if (!current_material.is_empty()) {
  348. mesh->surface_set_name(mesh->get_surface_count() - 1, current_material.get_basename());
  349. } else if (!current_group.is_empty()) {
  350. mesh->surface_set_name(mesh->get_surface_count() - 1, current_group);
  351. }
  352. print_verbose("OBJ: Added surface :" + mesh->surface_get_name(mesh->get_surface_count() - 1));
  353. surf_tool->clear();
  354. surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
  355. }
  356. if (l.begins_with("o ") || f->eof_reached()) {
  357. if (!p_single_mesh) {
  358. if (mesh->get_surface_count() > 0) {
  359. mesh->set_name(name);
  360. r_meshes.push_back(mesh);
  361. mesh.instantiate();
  362. }
  363. name = default_name;
  364. current_group = "";
  365. current_material = "";
  366. }
  367. }
  368. if (f->eof_reached()) {
  369. break;
  370. }
  371. if (l.begins_with("o ")) {
  372. name = l.substr(2, l.length()).strip_edges();
  373. }
  374. if (l.begins_with("usemtl ")) {
  375. current_material = l.replace("usemtl", "").strip_edges();
  376. }
  377. if (l.begins_with("g ")) {
  378. current_group = l.substr(2, l.length()).strip_edges();
  379. }
  380. } else if (l.begins_with("mtllib ")) { //parse material
  381. current_material_library = l.replace("mtllib", "").strip_edges();
  382. if (!material_map.has(current_material_library)) {
  383. HashMap<String, Ref<StandardMaterial3D>> lib;
  384. String lib_path = current_material_library;
  385. if (lib_path.is_relative_path()) {
  386. lib_path = p_path.get_base_dir().path_join(current_material_library);
  387. }
  388. Error err = _parse_material_library(lib_path, lib, r_missing_deps);
  389. if (err == OK) {
  390. material_map[current_material_library] = lib;
  391. }
  392. }
  393. }
  394. }
  395. if (p_single_mesh) {
  396. r_meshes.push_back(mesh);
  397. }
  398. return OK;
  399. }
  400. Node *EditorOBJImporter::import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err) {
  401. List<Ref<Mesh>> meshes;
  402. Error err = _parse_obj(p_path, meshes, false, p_flags & IMPORT_GENERATE_TANGENT_ARRAYS, false, Vector3(1, 1, 1), Vector3(0, 0, 0), r_missing_deps);
  403. if (err != OK) {
  404. if (r_err) {
  405. *r_err = err;
  406. }
  407. return nullptr;
  408. }
  409. Node3D *scene = memnew(Node3D);
  410. for (const Ref<Mesh> &m : meshes) {
  411. Ref<ImporterMesh> mesh;
  412. mesh.instantiate();
  413. mesh->set_name(m->get_name());
  414. for (int i = 0; i < m->get_surface_count(); i++) {
  415. mesh->add_surface(m->surface_get_primitive_type(i), m->surface_get_arrays(i), Array(), Dictionary(), m->surface_get_material(i));
  416. }
  417. ImporterMeshInstance3D *mi = memnew(ImporterMeshInstance3D);
  418. mi->set_mesh(mesh);
  419. mi->set_name(m->get_name());
  420. scene->add_child(mi, true);
  421. mi->set_owner(scene);
  422. }
  423. if (r_err) {
  424. *r_err = OK;
  425. }
  426. return scene;
  427. }
  428. void EditorOBJImporter::get_extensions(List<String> *r_extensions) const {
  429. r_extensions->push_back("obj");
  430. }
  431. EditorOBJImporter::EditorOBJImporter() {
  432. }
  433. ////////////////////////////////////////////////////
  434. String ResourceImporterOBJ::get_importer_name() const {
  435. return "wavefront_obj";
  436. }
  437. String ResourceImporterOBJ::get_visible_name() const {
  438. return "OBJ As Mesh";
  439. }
  440. void ResourceImporterOBJ::get_recognized_extensions(List<String> *p_extensions) const {
  441. p_extensions->push_back("obj");
  442. }
  443. String ResourceImporterOBJ::get_save_extension() const {
  444. return "mesh";
  445. }
  446. String ResourceImporterOBJ::get_resource_type() const {
  447. return "Mesh";
  448. }
  449. int ResourceImporterOBJ::get_format_version() const {
  450. return 1;
  451. }
  452. int ResourceImporterOBJ::get_preset_count() const {
  453. return 0;
  454. }
  455. String ResourceImporterOBJ::get_preset_name(int p_idx) const {
  456. return "";
  457. }
  458. void ResourceImporterOBJ::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  459. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_tangents"), true));
  460. r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "scale_mesh"), Vector3(1, 1, 1)));
  461. r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "offset_mesh"), Vector3(0, 0, 0)));
  462. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "optimize_mesh"), true));
  463. }
  464. bool ResourceImporterOBJ::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  465. return true;
  466. }
  467. Error ResourceImporterOBJ::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  468. List<Ref<Mesh>> meshes;
  469. Error err = _parse_obj(p_source_file, meshes, true, p_options["generate_tangents"], p_options["optimize_mesh"], p_options["scale_mesh"], p_options["offset_mesh"], nullptr);
  470. ERR_FAIL_COND_V(err != OK, err);
  471. ERR_FAIL_COND_V(meshes.size() != 1, ERR_BUG);
  472. String save_path = p_save_path + ".mesh";
  473. err = ResourceSaver::save(meshes.front()->get(), save_path);
  474. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Mesh to file '" + save_path + "'.");
  475. r_gen_files->push_back(save_path);
  476. return OK;
  477. }
  478. ResourceImporterOBJ::ResourceImporterOBJ() {
  479. }