-
Notifications
You must be signed in to change notification settings - Fork 3
/
assimpobjloader.py
61 lines (50 loc) · 1.95 KB
/
assimpobjloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from pyassimp import load, postprocess
class AssimpObjLoader(object):
def __init__(self, source):
# super(AssimpObjLoader, **kwargs)
# source = kwargs.get('source', '')
if not source:
print "no source given!"
return
self.scene = scene = load(
source, 0
| postprocess.aiProcess_Triangulate
| postprocess.aiProcess_SplitLargeMeshes
| postprocess.aiProcess_GenNormals
)
self.objects = {}
for i, mesh in enumerate(scene.meshes):
self.objects[i] = AssimpMesh(scene, mesh)
class AssimpMesh(object):
def __init__(self, scene, mesh):
self.vertex_format = [
('v_pos', 3, 'float'),
('v_normal', 3, 'float'),
('v_tc0', 2, 'float'),
('v_ambient', 3, 'float'),
('v_diffuse', 3, 'float'),
('v_specular', 3, 'float'),
('v_specular_coeff', 1, 'float'),
('v_transparency', 1, 'float'),
]
self.vertices = []
self.indices = []
self.texture = mesh.material.properties.get(('file', 1L))
ambiant = [0.0, 0.0, 0.0]
diffuse = [1.0, 1.0, 1.0]
specular = [1.0, 1.0, 1.0]
for i, v in enumerate(mesh.vertices):
data = (
list(v) + list(mesh.normals[i]) +
list(mesh.texturecoords[0][i][:2]
if len(mesh.texturecoords) else [0, 0]) +
mesh.material.properties.get(('ambiant', 0L), ambiant) +
mesh.material.properties.get(('diffuse', 0L), diffuse) +
mesh.material.properties.get(('specular', 0L), specular) + [
mesh.material.properties.get(('refracti', 0L), 1),
mesh.material.properties.get(('opacity', 0L), 1)
]
)
self.vertices.extend(data)
for f in mesh.faces:
self.indices.extend(f)