Revert to the FUTURE_PYTHON_API, this means:
[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.tess = None
77                 self.curface = None
78                 self.tessfaces = []
79                 self.recalc_normals = 1
80
81         def __del__(self):
82                 del self.data
83
84         def __getattr__(self, name):
85                 if name == 'vertices':
86                         return self.data.verts
87                 else:
88                         return getattr(self.data, name)
89
90         def __repr__(self):
91                 return "Mesh: %d faces, %d vertices" % (len(self.faces), len(self.verts))
92         def toNMFaces(self, ngon):
93                 # This should be a Publisher only feature...once the tesselation
94                 # is improved. The GLU tesselator of Mesa < 4.0 is crappy...
95                 if not self.tess:
96                         return [] # no faces converted
97                 import tess
98                 i = 0
99                 global tessfaces
100                 tessfaces = []
101                 tess.beginPolygon(self.tess)
102                 for v in ngon.v:
103                         if len(ngon.uv) == len(ngon.v):
104                                 v.uvco = ngon.uv[i]
105                         tess.vertex(self.tess, (v.co[0], v.co[1], v.co[2]), v)
106                         i += 1
107                 tess.endPolygon(self.tess)
108                 return tessfaces
109
110         def hasFaceUV(self, true):
111                 self.data.hasFaceUV(true)
112
113         def addVert(self, v):
114                 vert = _NMesh.Vert(v[0], v[1], v[2]) 
115                 self.data.verts.append(vert)
116                 return vert
117
118         def addFace(self, vlist, flags = None, makedefaultUV = 0):
119                 n = len(vlist)
120                 if n > 4:
121                         face = Face(vlist)
122                 else:
123                         face = _NMesh.Face()
124                         for v in vlist:
125                                 face.v.append(v)
126                         if makedefaultUV:
127                                 face.uv = defaultUV[:n]
128                 self.faces.append(face) 
129                 # turn on default flags:
130                 if not flags:
131                         face.mode = self.flags
132                 else:
133                         face.mode = flags
134                 return face
135
136         def write(self):
137                 from Blender import Object
138                 # new API style: 
139                 self.update()
140                 ob = Object.New(Object.Types.MESH)  # create object 
141                 ob.link(self.data)                  # link mesh data to it
142                 self.scene.link(ob)
143                 return ob
144
145         def update(self):
146                 from Blender.Types import NMFaceType
147                 smooth = self.smooth
148                 for f in self.faces:
149                         if type(f) == NMFaceType:
150                                 f.smooth = smooth
151                                 self.data.faces.append(f)
152                                 f.materialIndex = 0
153                         else: #it's a NGON (shadow face)
154                                 faces = self.toNMFaces(f)
155                                 for nf in faces:
156                                         nf.smooth = smooth
157                                         nf.materialIndex = 0
158                                         self.data.faces.append(nf)
159                                         
160                 if not self.name:
161                         self.name = "Mesh"
162
163         def assignMaterial(self, material):
164                 self.data.materials = [material._object]
165
166 Mesh = shadowNMesh
167 Vert = shadowNVert
168