4 class VertexAttribute(object):
6 'nx', 'ny', 'nz', # normal
9 def __init__(self, nx, ny, nz, u, v):
17 return "<vkey: %f, %f, %f, %f, %f>" % (
18 self.nx, self.ny, self.nz, self.u, self.v)
21 return int(100*(self.nx + self.ny + self.nz + self.u + self.v))
23 def __eq__(self, rhs):
24 return self.nx==rhs.nx and self.ny==rhs.ny and self.nz==rhs.nz and self.u==rhs.u and self.v==rhs.v
27 class VertexKey(object):
32 def __init__(self, obj_index, index):
33 self.obj_index=obj_index
37 return "<vkey: %d, %d>" % (self.obj_index, self.index)
40 return self.index*100+self.obj_index
42 def __eq__(self, rhs):
43 return self.obj_index==rhs.obj_index and self.index==rhs.index
46 class VertexArray(object):
53 'attributes', # normal and uv
59 # indexArrays split with each material
72 return "<VertexArray %d positions, %d indexArrays>" % (
73 len(self.positions), len(self.indexArrays))
77 self.positions, self.attributes,
78 self.b0, self.b1, self.weight)
81 keys=[key for key in self.indexArrays.keys()]
84 yield(key, self.indexArrays[key])
86 def __addOrGetIndex(self, obj_index, base_index, pos, normal, uv, b0, b1, weight0):
87 key=VertexKey(obj_index, base_index)
88 attribute=VertexAttribute(
89 normal[0], normal[1], normal[2],
91 if key in self.vertexMap:
92 if attribute in self.vertexMap[key]:
93 return self.vertexMap[key][attribute]
95 return self.__addVertex(self.vertexMap[key],
96 pos, attribute, b0, b1, weight0)
99 self.vertexMap[key]=vertexMapKey
100 return self.__addVertex(vertexMapKey,
101 pos, attribute, b0, b1, weight0)
103 def __addVertex(self, vertexMapKey, pos, attribute, b0, b1, weight0):
104 index=len(self.positions)
105 vertexMapKey[attribute]=index
107 self.positions.append((pos.x, pos.y, pos.z))
109 self.attributes.append(attribute)
113 self.weight.append(weight0)
117 def getMappedIndex(self, obj_name, base_index):
118 return self.vertexMap[VertexKey(self.objectMap[obj_name], base_index)]
120 def addTriangle(self,
121 object_name, material,
122 base_index0, base_index1, base_index2,
128 weight0, weight1, weight2
130 if object_name in self.objectMap:
131 obj_index=self.objectMap[object_name]
133 obj_index=len(self.objectMap)
134 self.objectMap[object_name]=obj_index
135 index0=self.__addOrGetIndex(obj_index, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
136 index1=self.__addOrGetIndex(obj_index, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
137 index2=self.__addOrGetIndex(obj_index, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
139 if not material in self.indexArrays:
140 self.indexArrays[material]=[]
141 self.indexArrays[material]+=[index0, index1, index2]