Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/io/io_tspline.cpp
- This file was added.
| /* | |||||
| * This program is free software; you can redistribute it and/or | |||||
| * modify it under the terms of the GNU General Public License | |||||
| * as published by the Free Software Foundation; either version 2 | |||||
| * of the License, or (at your option) any later version. | |||||
| * | |||||
| * This program is distributed in the hope that it will be useful, | |||||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
| * GNU General Public License for more details. | |||||
| * | |||||
| * You should have received a copy of the GNU General Public License | |||||
| * along with this program; if not, write to the Free Software Foundation, | |||||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||||
| * | |||||
| * The Original Code is Copyright (C) 2019 Blender Foundation. | |||||
| * All rights reserved. | |||||
| */ | |||||
| /** \file | |||||
| * \ingroup editor/io | |||||
| */ | |||||
| //#ifdef WITH_USD | |||||
| #include <vector> | |||||
| #include <tspline.h> | |||||
| #include <factory.h> | |||||
| #include <editor.h> | |||||
| #include <tessellator.h> | |||||
| #include <writer.h> | |||||
| #include <rhbuilder.h> | |||||
| #include "MEM_guardedalloc.h" | |||||
| extern "C" { | |||||
| #include "DNA_space_types.h" | |||||
| #include "BKE_context.h" | |||||
| #include "BKE_main.h" | |||||
| #include "BKE_report.h" | |||||
| #include "BKE_editmesh.h" | |||||
| #include "BLI_path_util.h" | |||||
| #include "BLI_string.h" | |||||
| #include "BLI_utildefines.h" | |||||
| #include "BLT_translation.h" | |||||
| #include "ED_object.h" | |||||
| #include "ED_undo.h" | |||||
| #include "RNA_access.h" | |||||
| #include "RNA_define.h" | |||||
| #include "UI_interface.h" | |||||
| #include "UI_resources.h" | |||||
| #include "WM_api.h" | |||||
| #include "WM_types.h" | |||||
| #include "DEG_depsgraph.h" | |||||
| #include "io_tspline.h" | |||||
| //# include "usd.h" | |||||
| } | |||||
| static float null_loc[] = {0, 0, 0}; | |||||
| static float null_rot[] = {0, 0, 0}; | |||||
| static void tspline_import_mesh(bContext *C, RhBuilderPtr &reader, bool newobj = true); | |||||
| static void tspline_import_mesh(bContext *C, RhBuilderPtr &reader, bool newobj) | |||||
| { | |||||
| Main *bmain = CTX_data_main(C); | |||||
| ; | |||||
| BMEditMesh *em; | |||||
| BMesh *bm; | |||||
| TSplinePtr spline = reader->findTSpline(); | |||||
| TTessellator tessellator(spline); | |||||
| tessellator.setResolution(0.01); | |||||
| std::vector<std::string> faces; | |||||
| reader->findTFaceNames(faces); | |||||
| const char *mesh_name = tessellator.interpolateFace(faces[1])->getName().c_str(); | |||||
| Object *obedit = CTX_data_edit_object(C); | |||||
| if (newobj) { | |||||
| // Exit editmode if we're in it | |||||
| if (obedit) { | |||||
| ED_object_editmode_load(bmain, obedit); | |||||
| BLI_assert(!CTX_data_edit_object(C)); | |||||
| } | |||||
| obedit = ED_object_add_type(C, OB_MESH, mesh_name, null_loc, null_rot, false, 0); | |||||
| // rename_id((ID*)obedit, mesh_name); | |||||
| // rename_id((ID*)obedit->data, mesh_name); | |||||
| ED_object_editmode_enter(C, 0); | |||||
| } | |||||
| BLI_assert(obedit); | |||||
| em = BKE_editmesh_from_object(obedit); | |||||
| BLI_assert(em); | |||||
| bm = em->bm; | |||||
| ObjWriter objwriter("tsimport", 0); | |||||
| for (int i = 0; i < faces.size(); i++) { | |||||
| TriMeshPtr trimesh = tessellator.interpolateFace(faces[i]); | |||||
| // objwriter.addMesh(trimesh); | |||||
| //} | |||||
| int ji = 0; | |||||
| int f = 0; | |||||
| // TriMeshPtr trimesh = objwriter.triMesh(); | |||||
| long offset = 0; | |||||
| if (trimesh) { | |||||
| // objwriter.writeObjMesh(trimesh, offset); | |||||
| if (!trimesh) | |||||
| return; | |||||
| int num_v = trimesh->sizePoints(); | |||||
| std::vector<BMVert *> blend_v(num_v); | |||||
| int num_f = trimesh->sizeTriangles(); | |||||
| std::vector<BMFace *> blend_f(num_f); | |||||
| P3dVIterator piter = trimesh->pointIteratorBegin(); | |||||
| for (; piter != trimesh->pointIteratorEnd(); piter++) { | |||||
| Point3DPtr point = *piter; | |||||
| float xyz[3] = {point->x(), point->y(), point->z()}; | |||||
| BMVert *bv = BM_vert_create(bm, xyz, NULL, BM_CREATE_NOP); | |||||
| blend_v[ji] = bv; | |||||
| ji++; | |||||
| // printf("x:%f y:%f z:%f %d\n", point->x(), point->y(), point->z(), i); | |||||
| } | |||||
| // N3dVIterator niter = trimesh->normalIteratorBegin(); | |||||
| // for (; niter != trimesh->normalIteratorEnd(); niter++) { | |||||
| // Vector3DPtr normal = *niter; | |||||
| // wfile << "vn " << normal->i() << " " << normal->j() << " " << normal->k() << std::endl; | |||||
| //} | |||||
| // wfile << "# " << trimesh->sizeNormals() << " normals" << std::endl; | |||||
| TriVIterator titer = trimesh->triangleIteratorBegin(); | |||||
| for (; titer != trimesh->triangleIteratorEnd(); titer++) { | |||||
| TrianglePtr triangle = *titer; | |||||
| BMVert *v0 = blend_v[triangle->point_indices[0] ]; | |||||
| BMVert *v1 = blend_v[triangle->point_indices[1] ]; | |||||
| BMVert *v2 = blend_v[triangle->point_indices[2] ]; | |||||
| // BMVert *v3 = blend_v[triangle->point_indices[2] ]; | |||||
| BMFace *bf = BM_face_create_quad_tri(bm, v0, v1, v2, NULL, NULL, BM_CREATE_NOP); | |||||
| blend_f[f] = bf; | |||||
| f++; | |||||
| } | |||||
| offset += trimesh->sizePoints(); | |||||
| } | |||||
| } | |||||
| //TriMshVIterator iter = objwriter._more_meshes.begin(); | |||||
| //for (; iter != objwriter._more_meshes.end(); iter++) { | |||||
| // trimesh = *iter; | |||||
| // if (trimesh) { | |||||
| // if (!trimesh) | |||||
| // return; | |||||
| // //int num_v = trimesh->sizePoints(); | |||||
| // //std::vector<BMVert *> blend_v(num_v); | |||||
| // P3dVIterator piter = trimesh->pointIteratorBegin(); | |||||
| // | |||||
| // for (; piter != trimesh->pointIteratorEnd(); piter++) { | |||||
| // Point3DPtr point = *piter; | |||||
| // float xyz[3] = {point->x(), point->y(), point->z()}; | |||||
| // BMVert *bv = BM_vert_create(bm, xyz, NULL, BM_CREATE_NOP); | |||||
| // blend_v[i] = bv; | |||||
| // i++; | |||||
| // } | |||||
| // // N3dVIterator niter = trimesh->normalIteratorBegin(); | |||||
| // // for (; niter != trimesh->normalIteratorEnd(); niter++) { | |||||
| // // Vector3DPtr normal = *niter; | |||||
| // // wfile << "vn " << normal->i() << " " << normal->j() << " " << normal->k() << std::endl; | |||||
| // //} | |||||
| // // wfile << "# " << trimesh->sizeNormals() << " normals" << std::endl; | |||||
| // | |||||
| // TriVIterator titer = trimesh->triangleIteratorBegin(); | |||||
| // for (; titer != trimesh->triangleIteratorEnd(); titer++) { | |||||
| // TrianglePtr triangle = *titer; | |||||
| // BMVert *v0 = blend_v[triangle->point_indices[0] + offset ]; | |||||
| // BMVert *v1 = blend_v[triangle->point_indices[1] + offset ]; | |||||
| // BMVert *v2 = blend_v[triangle->point_indices[2] + offset ]; | |||||
| // //BMVert *v3 = blend_v[triangle->point_indices[2] + offset + 1]; | |||||
| // BMFace *bf = BM_face_create_quad_tri( | |||||
| // bm, v0, v1, v2,NULL , NULL, BM_CREATE_NOP); | |||||
| // blend_f[f] = bf; | |||||
| // f++; | |||||
| // } | |||||
| // offset += trimesh->sizePoints(); | |||||
| // } | |||||
| //} | |||||
| //int num_e = ON_e.Count(); | |||||
| //std::vector<BMEdge *> blend_e(num_e); | |||||
| //for (int i = 0; i < num_e; i++) { | |||||
| // ON_2dex *e = ON_e.At(i); | |||||
| // BMEdge *be = BM_edge_create(bm, blend_v[e->i], blend_v[e->j], NULL, BM_CREATE_NOP); | |||||
| // blend_e[i] = be; | |||||
| //} | |||||
| if (newobj) | |||||
| ED_object_editmode_exit(C, EM_FREEDATA); | |||||
| } | |||||
| int tspline_import(bContext *C, wmOperator *op) | |||||
| { | |||||
| char filename[FILE_MAX]; | |||||
| if (!RNA_struct_property_is_set(op->ptr, "filepath")) { | |||||
| BKE_report(op->reports, RPT_ERROR, "No filename given"); | |||||
| return OPERATOR_CANCELLED; | |||||
| } | |||||
| RNA_string_get(op->ptr, "filepath", filename); | |||||
| RhBuilderPtr reader = makePtr<RhBuilder>(filename); | |||||
| tspline_import_mesh(C, reader); | |||||
| ED_undo_push(C, "Imported tsm file"); | |||||
| return OPERATOR_FINISHED; // OPERATOR_CANCELLED | |||||
| } | |||||
| void WM_OT_tspline_import(struct wmOperatorType *ot) | |||||
| { | |||||
| ot->name = "Import Rhino TSM"; | |||||
| ot->description = "Load a Rhino-compatible .tsm file"; | |||||
| ot->idname = "WM_OT_tspline_import"; | |||||
| ot->invoke = WM_operator_filesel; | |||||
| ot->exec = tspline_import; | |||||
| ot->poll = WM_operator_winactive; | |||||
| RNA_def_string( | |||||
| ot->srna, "filter_glob", "*.tsm", 16, "Glob Filter", "Rhino Tspline Extension Glob Filter"); | |||||
| RNA_def_string( | |||||
| ot->srna, "filename_ext", ".tsm", 16, "Rhino Tspline File Extension", "Rhino Tspline File Extension"); | |||||
| WM_operator_properties_filesel(ot, | |||||
| FILE_TYPE_FOLDER, | |||||
| FILE_BLENDER, | |||||
| FILE_OPENFILE, | |||||
| WM_FILESEL_FILEPATH, | |||||
| FILE_DEFAULTDISPLAY, | |||||
| FILE_SORT_ALPHA); | |||||
| } | |||||
| //#endif /* WITH_USD */ | |||||