Package fabmetheus_utilities :: Package fabmetheus_tools :: Package interpret_plugins :: Module obj
[hide private]
[frames] | no frames]

Source Code for Module fabmetheus_utilities.fabmetheus_tools.interpret_plugins.obj

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