chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / 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
25 from fabmetheus_utilities.geometry.geometry_tools import face
26 from fabmetheus_utilities.geometry.solids import triangle_mesh
27 from fabmetheus_utilities.vector3 import Vector3
28 from fabmetheus_utilities import archive
29 from fabmetheus_utilities import gcodec
30
31 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
32 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
33 __date__ = '$Date: 2008/21/04 $'
34 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
35
36
37 def addFacesGivenText( objText, triangleMesh ):
38         "Add faces given obj text."
39         lines = archive.getTextLines( objText )
40         for line in lines:
41                 splitLine = line.split()
42                 firstWord = gcodec.getFirstWord(splitLine)
43                 if firstWord == 'v':
44                         triangleMesh.vertexes.append( getVertexGivenLine(line) )
45                 elif firstWord == 'f':
46                         addFacesGivenLine( triangleMesh.faces, line )
47
48 def getCarving(fileName=''):
49         "Get the triangle mesh for the obj file."
50         if fileName == '':
51                 return None
52         objText = archive.getFileText(fileName, True, 'rb')
53         if objText == '':
54                 return None
55         triangleMesh = triangle_mesh.TriangleMesh()
56         addFacesGivenText(objText, triangleMesh)
57         return triangleMesh
58
59 def addFacesGivenLine( faces, line ):
60         "Add face given line index and lines."
61         parts = map(lambda p: p.split('/')[0], line.split())
62         for idx in xrange(1, len(parts)-2):
63                 addface = face.Face()
64                 addface.index = len( faces )
65                 addface.vertexIndexes.append(int(parts[1]) - 1)
66                 addface.vertexIndexes.append(int(parts[idx+1]) - 1)
67                 addface.vertexIndexes.append(int(parts[idx+2]) - 1)
68                 faces.append(addface)
69
70 def getVertexGivenLine(line):
71         "Get vertex given obj vertex line."
72         splitLine = line.split()
73         return Vector3( float(splitLine[1]), float( splitLine[2] ), float( splitLine[3] ) )