Skip to content

Instantly share code, notes, and snippets.

@Kruithne
Created March 12, 2019 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kruithne/34af2ada24248ddec83132ac6f35669f to your computer and use it in GitHub Desktop.
Save Kruithne/34af2ada24248ddec83132ac6f35669f to your computer and use it in GitHub Desktop.
WIP fast import script
import bpy
import bmesh
objectFile = "D:\\WoWExport\\world\\wmo\\draenor\\human\\6hu_garrison_inn_v1.obj"
verts = []
normals = []
uv = []
faces = []
material_libs = set()
with open(objectFile, 'rb') as f:
for line in f:
line_split = line.split()
if not line_split:
continue
line_start = line_split[0]
if line_start == b'v':
verts.append([float(v) for v in line_split[1:]])
elif line_start == b'vn':
normals.append([float(v) for v in line_split[1:]])
elif line_start == b'vt':
uv.append([float(v) for v in line_split[1:]])
elif line_start == b'f':
line_split = line_split[1:]
faces.append((
line_split[0].split(b'/')[0],
line_split[1].split(b'/')[0],
line_split[2].split(b'/')[0]
))
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
mesh = bpy.data.meshes.new("mesh")
obj = bpy.data.objects.new("TestObject", mesh)
scene = bpy.context.scene
scene.objects.link(obj)
scene.objects.active = obj
obj.select = True
mesh = bpy.context.object.data
bm = bmesh.new()
i = 0
for v in verts:
vert = bm.verts.new(v)
vert.normal = normals[i]
i = i + 1
bm.verts.ensure_lookup_table()
bm.verts.index_update()
for face in faces:
bm.faces.new((
bm.verts[int(face[0]) - 1],
bm.verts[int(face[1]) - 1],
bm.verts[int(face[2]) - 1]
));
uv_layer = bm.loops.layers.uv.new()
for face in bm.faces:
face.smooth = True
for loop in face.loops:
loop[uv_layer].uv = uv[loop.vert.index]
bm.to_mesh(mesh)
bm.free()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment