[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
This commit is contained in:
Syres916
2021-06-10 16:58:32 +01:00
committed by GitHub
parent ee6a9b8c7d
commit 6195043f65

View File

@@ -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