Initial revision
[blender.git] / intern / python / modules / beta / Objects.py
1 from Blender import Scene
2 import Blender.NMesh as _NMesh
3 import Blender.Material as Material
4
5
6 defaultUV = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
7
8 FACEFLAGS = _NMesh.Const
9 DEFAULTFLAGS = FACEFLAGS.LIGHT + FACEFLAGS.DYNAMIC
10
11 curface = None
12 tessfaces = None
13
14 def error():
15         pass
16 def beginPolygon():
17         global curface
18         global tessfaces
19         curface = _NMesh.Face()
20 def endPolygon():
21         global curface
22         global tessfaces
23         tessfaces.append(curface)
24 def addVertex(v):
25         global curface
26         curface.v.append(v)
27         curface.uv.append((v.uvco[0], v.uvco[1]))
28
29 class Face:
30         def __init__(self, vlist):
31                 self.v= vlist
32                 self.uv = []
33                 self.mode = 0
34
35 class shadow:
36         def __setattr__(self, name, val):
37                 setattr(self.data, name, val)
38         def __getattr__(self, name):
39                 return getattr(self.data, name)
40         def __repr__(self):
41                 return repr(self.data)
42
43 ##########################################
44 # replacement xMesh (NMesh shadow class)
45
46 class shadowNVert:  #shadow NMVert class for the tesselator
47         def __init__(self):
48                 self.vert = None
49                 self.uv = []
50         def __len__(self):
51                 return 3
52         def __getitem__(self, i):
53                 return self.vert.co[i]
54
55 def Color(r, g, b, a = 1.0):
56         return _NMesh.Col(255 * r, 255 * g, 255 * b, 255 * a)
57
58 class shadowNMesh:
59         def __init__(self, name = None, default_flags = None):
60                 self.scene = Scene.getCurrent()
61                 self.data = _NMesh.GetRaw()
62                 self.name = name
63                 if default_flags:
64                         flags = default_flags
65                 else:
66                         flags = DEFAULTFLAGS
67                 self.flags = flags
68                 self.smooth = 0
69                 self.faces = []
70                 try:
71                         import tess
72                         self.tess = tess.Tess(256, beginPolygon, endPolygon, error, addVertex)
73                 except:
74                         #print "couldn't import tesselator"
75                         self.tess = None
76                 self.curface = None
77                 self.tessfaces = []
78                 self.recalc_normals = 1
79
80         def __del__(self):
81                 del self.data
82
83         def __getattr__(self, name):
84                 if name == 'vertices':
85                         return self.data.verts
86                 else:
87                         return getattr(self.data, name)
88
89         def __repr__(self):
90                 return "Mesh: %d faces, %d vertices" % (len(self.faces), len(self.verts))
91         def toNMFaces(self, ngon):
92                 # This should be a Publisher only feature...once the tesselation
93                 # is improved. The GLU tesselator of Mesa < 4.0 is crappy...
94                 if not self.tess:
95                         return [] # no faces converted
96                 import tess
97                 i = 0
98                 global tessfaces
99                 tessfaces = []
100                 tess.beginPolygon(self.tess)
101                 for v in ngon.v:
102                         if len(ngon.uv) == len(ngon.v):
103                                 v.uvco = ngon.uv[i]
104                         tess.vertex(self.tess, (v.co[0], v.co[1], v.co[2]), v)
105                         i += 1
106                 tess.endPolygon(self.tess)
107                 return tessfaces
108
109         def hasFaceUV(self, true):
110                 self.data.hasFaceUV(true)
111
112         def addVert(self, v):
113                 vert = _NMesh.Vert(v[0], v[1], v[2]) 
114                 self.data.verts.append(vert)
115                 return vert
116
117         def addFace(self, vlist, flags = None, makedefaultUV = 0):
118                 n = len(vlist)
119                 if n > 4:
120                         face = Face(vlist)
121                 else:
122                         face = _NMesh.Face()
123                         for v in vlist:
124                                 face.v.append(v)
125                         if makedefaultUV:
126                                 face.uv = defaultUV[:n]
127                 self.faces.append(face) 
128                 # turn on default flags:
129                 if not flags:
130                         face.mode = self.flags
131                 else:
132                         face.mode = flags
133                 return face
134
135         def write(self):
136                 from Blender import Object
137                 # new API style: 
138                 self.update()
139                 ob = Object.New(Object.Types.MESH)  # create object 
140                 ob.link(self.data)                  # link mesh data to it
141                 self.scene.link(ob)
142                 return ob
143
144         def update(self):
145                 from Blender.Types import NMFaceType
146                 smooth = self.smooth
147                 for f in self.faces:
148                         if type(f) == NMFaceType:
149                                 f.smooth = smooth
150                                 self.data.faces.append(f)
151                                 f.materialIndex = 0
152                         else: #it's a NGON (shadow face)
153                                 faces = self.toNMFaces(f)
154                                 for nf in faces:
155                                         nf.smooth = smooth
156                                         nf.materialIndex = 0
157                                         self.data.faces.append(nf)
158                                         
159                 if not self.name:
160                         self.name = "Mesh"
161
162         def assignMaterial(self, material):
163                 self.data.materials = [material._object]
164
165 Mesh = shadowNMesh
166 Vert = shadowNVert
167