OSDN Git Service

fix blender plugin packaging
[meshio/pymeshio.git] / blender26-meshio / exporter / vertexarray.py
1 # coding: utf-8
2
3
4 class VertexAttribute(object):
5     __slots__=[
6             'nx', 'ny', 'nz', # normal
7             'u', 'v', # uv
8             ]
9     def __init__(self, nx, ny, nz, u, v):
10         self.nx=nx
11         self.ny=ny
12         self.nz=nz
13         self.u=u
14         self.v=v
15
16     def __str__(self):
17         return "<vkey: %f, %f, %f, %f, %f>" % (
18                 self.nx, self.ny, self.nz, self.u, self.v)
19
20     def __hash__(self):
21         return int(100*(self.nx + self.ny + self.nz + self.u + self.v))
22
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
25
26
27 class VertexKey(object):
28     __slots__=[
29             'obj_index', 'index',
30             ]
31
32     def __init__(self, obj_index, index):
33         self.obj_index=obj_index
34         self.index=index
35
36     def __str__(self):
37         return "<vkey: %d, %d>" % (self.obj_index, self.index)
38
39     def __hash__(self):
40         return self.index*100+self.obj_index
41
42     def __eq__(self, rhs):
43         return self.obj_index==rhs.obj_index and self.index==rhs.index
44
45
46 class VertexArray(object):
47     """
48     頂点配列
49     """
50     __slots__=[
51             'indexArrays',
52             'positions',
53             'attributes', # normal and uv
54             'b0', 'b1', 'weight',
55             'vertexMap',
56             'objectMap',
57             ]
58     def __init__(self):
59         # indexArrays split with each material
60         self.indexArrays={}
61
62         self.positions=[]
63         self.attributes=[]
64         self.b0=[]
65         self.b1=[]
66         self.weight=[]
67
68         self.vertexMap={}
69         self.objectMap={}
70
71     def __str__(self):
72         return "<VertexArray %d positions, %d indexArrays>" % (
73                 len(self.positions), len(self.indexArrays))
74
75     def zip(self):
76         return zip(
77                 self.positions, self.attributes,
78                 self.b0, self.b1, self.weight)
79
80     def each(self):
81         keys=[key for key in self.indexArrays.keys()]
82         keys.sort()
83         for key in keys:
84             yield(key, self.indexArrays[key])
85
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],
90                 uv[0], uv[1])
91         if key in self.vertexMap:
92             if attribute in self.vertexMap[key]:
93                 return self.vertexMap[key][attribute]
94             else:
95                 return self.__addVertex(self.vertexMap[key],
96                         pos, attribute, b0, b1, weight0)
97         else:
98             vertexMapKey={}
99             self.vertexMap[key]=vertexMapKey
100             return self.__addVertex(vertexMapKey,
101                     pos, attribute, b0, b1, weight0)
102
103     def __addVertex(self, vertexMapKey, pos, attribute, b0, b1, weight0):
104         index=len(self.positions)
105         vertexMapKey[attribute]=index
106         # position
107         self.positions.append((pos.x, pos.y, pos.z))
108         # unique attribute
109         self.attributes.append(attribute)
110         # shared attribute
111         self.b0.append(b0)
112         self.b1.append(b1)
113         self.weight.append(weight0)
114         assert(index<=65535)
115         return index
116             
117     def getMappedIndex(self, obj_name, base_index):
118         return self.vertexMap[VertexKey(self.objectMap[obj_name], base_index)]
119
120     def addTriangle(self,
121             object_name, material,
122             base_index0, base_index1, base_index2,
123             pos0, pos1, pos2,
124             n0, n1, n2,
125             uv0, uv1, uv2,
126             b0_0, b0_1, b0_2,
127             b1_0, b1_1, b1_2,
128             weight0, weight1, weight2
129             ):
130         if object_name in self.objectMap:
131             obj_index=self.objectMap[object_name]
132         else:
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)
138
139         if not material in self.indexArrays:
140             self.indexArrays[material]=[]
141         self.indexArrays[material]+=[index0, index1, index2]
142