-
Notifications
You must be signed in to change notification settings - Fork 11
/
importer3D.py
61 lines (55 loc) · 1.62 KB
/
importer3D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf8 -*-
__title__ = "Collection of 3D Mesh importers"
__author__ = "Jens M. Plonka"
__url__ = "https://www.github.com/jmplonka/Importer3D"
import os, FreeCAD, import3DS, importLWO, importMAX, importMB, importGSM
def decode(name):
"decodes encoded strings"
try:
decodedName = (name.decode("utf8"))
except UnicodeDecodeError:
try:
decodedName = (name.decode("latin1"))
except UnicodeDecodeError:
FreeCAD.Console.PrintError("Error: Couldn't determine character encoding")
decodedName = name
return decodedName
def read(doc, filename):
name, ext = os.path.splitext(filename)
ext = ext.lower()
if (ext == '.lwo'):
importLWO.read(doc, filename)
elif (ext == '.3ds'):
import3DS.read(doc, filename)
elif (ext == '.max'):
importMAX.read(doc, filename)
# elif (ext == '.ma'):
# importMaya.read(doc, filename)
elif (ext == '.mb'):
importMB.read(doc, filename)
elif (ext == '.gsm'):
importGSM.read(doc, filename)
else:
FreeCAD.Console.PrintError("No suitable reader found for ext=%s\n" %(ext))
return
def insert(filename, docname):
'''
Called when freecad wants to import a file into an existing project.
'''
try:
doc = FreeCAD.getDocument(docname)
FreeCAD.ActiveDocument = doc
read(doc, filename)
return doc
except:
return open(filename)
def open(filename):
'''
Called when freecad wants to open a file as a new project.
'''
docname = (os.path.splitext(os.path.basename(filename))[0]).encode("utf8")
doc = FreeCAD.newDocument(docname)
doc.Label = decode(docname)
FreeCAD.ActiveDocument = doc
read(doc, filename)
return doc