chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / fabmetheus_utilities / fabmetheus_tools / interpret_plugins / obj.py
1 """
2 This page is in the table of contents.
3 The obj.py script is an import translator plugin to get a carving from an obj file.
4
5 An example obj file is box.obj in the models folder.
6
7 An import plugin is a script in the interpret_plugins folder which has the function getCarving.  It is meant to be run from the interpret tool.  To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
8
9 The getCarving function takes the file name of an obj file and returns the carving.
10
11 From wikipedia, OBJ (or .OBJ) is a geometry definition file format first developed by Wavefront Technologies for its Advanced Visualizer animation package:
12 http://en.wikipedia.org/wiki/Obj
13
14 The Object File specification is at:
15 http://local.wasp.uwa.edu.au/~pbourke/dataformats/obj/
16
17 An excellent link page about obj files is at:
18 http://people.sc.fsu.edu/~burkardt/data/obj/obj.html
19
20 """
21
22
23 from __future__ import absolute_import
24 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
25 import __init__
26
27 from fabmetheus_utilities.geometry.geometry_tools import face
28 from fabmetheus_utilities.geometry.solids import triangle_mesh
29 from fabmetheus_utilities.vector3 import Vector3
30 from fabmetheus_utilities import archive
31 from fabmetheus_utilities import gcodec
32 from struct import unpack
33
34 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
35 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
36 __date__ = '$Date: 2008/21/04 $'
37 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
38
39
40 def addFacesGivenText( objText, triangleMesh ):
41         "Add faces given obj text."
42         lines = archive.getTextLines( objText )
43         for line in lines:
44                 splitLine = line.split()
45                 firstWord = gcodec.getFirstWord(splitLine)
46                 if firstWord == 'v':
47                         triangleMesh.vertexes.append( getVertexGivenLine(line) )
48                 elif firstWord == 'f':
49                         triangleMesh.faces.append( getFaceGivenLine( line, triangleMesh ) )
50
51 def getCarving(fileName=''):
52         "Get the triangle mesh for the obj file."
53         if fileName == '':
54                 return None
55         objText = archive.getFileText(fileName, True, 'rb')
56         if objText == '':
57                 return None
58         triangleMesh = triangle_mesh.TriangleMesh()
59         addFacesGivenText(objText, triangleMesh)
60         return triangleMesh
61
62 def getFaceGivenLine( line, triangleMesh ):
63         "Add face given line index and lines."
64         faceGivenLine = face.Face()
65         faceGivenLine.index = len( triangleMesh.faces )
66         splitLine = line.split()
67         for vertexStringIndex in xrange( 1, 4 ):
68                 vertexString = splitLine[ vertexStringIndex ]
69                 vertexStringWithSpaces = vertexString.replace('/', ' ')
70                 vertexStringSplit = vertexStringWithSpaces.split()
71                 vertexIndex = int( vertexStringSplit[0] ) - 1
72                 faceGivenLine.vertexIndexes.append(vertexIndex)
73         return faceGivenLine
74
75 def getVertexGivenLine(line):
76         "Get vertex given obj vertex line."
77         splitLine = line.split()
78         return Vector3( float(splitLine[1]), float( splitLine[2] ), float( splitLine[3] ) )