From 80731db3c0a80b0aa592ef169b106aa6e24704d6 Mon Sep 17 00:00:00 2001 From: Syres916 <46537884+Syres916@users.noreply.github.com> Date: Thu, 10 Jun 2021 16:58:32 +0100 Subject: [PATCH] [Arch] Wavefront Obj Import files from... ...other programs such as Meshroom that don't contain an object name line. There is a bug report but bizarrely it's completely the reverse of what is currently occurring https://tracker.freecadweb.org/view.php?id=2336 --- src/Mod/Arch/importOBJ.py | 101 ++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 43 deletions(-) diff --git a/src/Mod/Arch/importOBJ.py b/src/Mod/Arch/importOBJ.py index e3de27e35f..0dd361d186 100644 --- a/src/Mod/Arch/importOBJ.py +++ b/src/Mod/Arch/importOBJ.py @@ -19,7 +19,7 @@ #* * #*************************************************************************** -import FreeCAD, DraftGeomUtils, Part, Draft, Arch, Mesh, MeshPart, os, sys +import FreeCAD, DraftGeomUtils, Part, Draft, Arch, Mesh, MeshPart, os, sys, codecs, ntpath # import numpy as np if FreeCAD.GuiUp: from DraftTools import translate @@ -155,7 +155,6 @@ def export(exportList,filename,colors=None): optionally colors can be a dict containing ["objectName:colorTuple"] pairs for use in non-GUI mode.""" - import codecs outfile = codecs.open(filename,"wb",encoding="utf8") ver = FreeCAD.Version() outfile.write("# FreeCAD v" + ver[0] + "." + ver[1] + " build" + ver[2] + " Arch module\n") @@ -293,6 +292,12 @@ def open(filename): return insert(filename,doc.Name) def insert(filename,docname): + + meshName = ntpath.basename(filename) + for i in meshName.split(): + if "." in i: + i = i.split(".")[0] + meshName = i "called when freecad wants to import a file" try: doc = FreeCAD.getDocument(docname) @@ -306,48 +311,58 @@ def insert(filename,docname): activeobject = None material = None colortable = {} + content_array = [] for line in infile: - line = line.strip() - if line[:7] == "mtllib ": - matlib = os.path.join(os.path.dirname(filename),line[7:]) - if os.path.exists(matlib): - with pythonopen(matlib,"r") as matfile: - mname = None - color = None - trans = None - for mline in matfile: - mline = mline.strip() - if mline[:7] == "newmtl ": - if mname and color: - colortable[mname] = [color,trans] - color = None - trans = None - mname = mline[7:] - elif mline[:3] == "Kd ": - color = tuple([float(i) for i in mline[3:].split()]) - elif mline[:2] == "d ": - trans = int(float(mline[2:])*100) - if mname and color: - colortable[mname] = [color,trans] - elif line[:2] == "o ": - if activeobject: - makeMesh(doc,activeobject,verts,facets,material,colortable) - material = None - facets = [] - activeobject = line[2:] - elif line[:2] == "v ": - verts.append([float(i) for i in line[2:].split()]) - elif line[:2] == "f ": - fa = [] - for i in line[2:].split(): - if "/" in i: - i = i.split("/")[0] - fa.append(int(i)) - facets.append(fa) - elif line[:7] == "usemtl ": - material = line[7:] - if activeobject: - makeMesh(doc,activeobject,verts,facets,material,colortable) + content_array.append(line) + activeobjectExists = False + for line in content_array: + line = line.strip() + if line[:2] == "o ": + activeobjectExists = True + if not activeobjectExists: + activeobject = meshName + for line in content_array: + line = line.strip() + if line[:7] == "mtllib ": + matlib = os.path.join(os.path.dirname(filename),line[7:]) + if os.path.exists(matlib): + with pythonopen(matlib,"r") as matfile: + mname = None + color = None + trans = None + for mline in matfile: + mline = mline.strip() + if mline[:7] == "newmtl ": + if mname and color: + colortable[mname] = [color,trans] + color = None + trans = None + mname = mline[7:] + elif mline[:3] == "Kd ": + color = tuple([float(i) for i in mline[3:].split()]) + elif mline[:2] == "d ": + trans = int(float(mline[2:])*100) + if mname and color: + colortable[mname] = [color,trans] + elif line[:2] == "o ": + if activeobject: + makeMesh(doc,activeobject,verts,facets,material,colortable) + material = None + facets = [] + activeobject = line[2:] + elif line[:2] == "v ": + verts.append([float(i) for i in line[2:].split()]) + elif line[:2] == "f ": + fa = [] + for i in line[2:].split(): + if "/" in i: + i = i.split("/")[0] + fa.append(int(i)) + facets.append(fa) + elif line[:7] == "usemtl ": + material = line[7:] + if activeobject: + makeMesh(doc,activeobject,verts,facets,material,colortable) FreeCAD.Console.PrintMessage(translate("Arch","Successfully imported") + ' ' + decode(filename) + "\n") return doc