Skip to content

Instantly share code, notes, and snippets.

@oal
Last active August 29, 2015 14:16
Show Gist options
  • Save oal/8de1db987145eb5962fa to your computer and use it in GitHub Desktop.
Save oal/8de1db987145eb5962fa to your computer and use it in GitHub Desktop.
def load_obj(file):
vertices = []
lines = []
with open(file, 'r') as f:
for line in f: # loop over lines in file
if line[0] == 'v': # lines starting with 'v' is a vertex
vertices.append([float(x) for x in line.split(' ')[1:]]) # split on spaces, skip first character (v)
elif line[0] == 'f': # face (4 points)
i = 0
# make pairs of 2 and 2 vertex indices ([0, 1], [1, 2], ...). Subtract 1 to get zero-indexing
indices = [int(x)-1 for x in line.split(' ')[1:]]
for i in range(len(indices)-1):
lines.append([indices[i], indices[i+1]])
return vertices, lines
model = load_obj('untitled.obj')
print 'vertices =', model[0]
print 'lines =', model[1]
'''
# Example usage (in your code)
for line in lines:
p1 = vertices[line[0]] + [1] # [1] is the w component
p2 = vertices[line[1]] + [1]
# Transform and plot...
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment