import Blender from Blender import * import math from math import * import random #Script from Joris Calvat #ammonite class ammonite(object): def __init__(self): self.mesh=Mesh.New('Cube') #nombre de tours [0.x < x.x] self.tours=3.0 #coeficient de puissance [0 < x] self.puissance=2 #coeficient du diametre entre deux separations [0.5 < 1.0] self.coef_cos=0.9 #nombre de points pour un cercle [3 < x] self.points=6 #angle entre chaque cellule self.saut=12.0 #largeur self.coef_larg=1.2 #coef de largeur interne [0.x < 0.x] self.coef_larg_interne=0.90 #rotation du cercle, pas très utile self.rot_cercle=0.0 #décalage vertical self.coef_decalage=0.0 self.sub_cos=2.00 self.dessiner() def dessiner(self): i=720.0 self.tours=self.tours+2.0 pos_cos=0.0 while i<=self.tours*360: larg_a=self.largeur_anneau(i+self.saut, pos_cos+1) larg_b=self.largeur_anneau(i, pos_cos) moy_a=self.position_anneau(i+self.saut) moy_b=self.position_anneau(i) j=1 while j<=self.points: moy_a=self.position_anneau(i+self.saut) moy_b=self.position_anneau(i) #Exterieur ptext_a1=point(0.0, 0.0, larg_a) ptext_a2=point(0.0, 0.0, larg_a) ptext_b1=point(0.0, 0.0, larg_b) ptext_b2=point(0.0, 0.0, larg_b) ptext_a1.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) ptext_a2.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) ptext_b1.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) ptext_b2.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) ptext_a1.translate(moy_a, 0.0, moy_a*self.coef_decalage) ptext_a2.translate(moy_a, 0.0, moy_a*self.coef_decalage) ptext_b1.translate(moy_b, 0.0, moy_b*self.coef_decalage) ptext_b2.translate(moy_b, 0.0, moy_b*self.coef_decalage) ptext_a1.rotation(0.0, 0.0, i+self.saut) ptext_a2.rotation(0.0, 0.0, i+self.saut) ptext_b1.rotation(0.0, 0.0, i) ptext_b2.rotation(0.0, 0.0, i) f1=face(ptext_a1, ptext_a2, ptext_b1, ptext_b2) f1.dessiner(self.mesh) #Interieur ptint_a1=point(0.0, 0.0, larg_a*self.coef_larg_interne) ptint_a2=point(0.0, 0.0, larg_a*self.coef_larg_interne) ptint_b1=point(0.0, 0.0, larg_b*self.coef_larg_interne) ptint_b2=point(0.0, 0.0, larg_b*self.coef_larg_interne) ptint_a1.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) ptint_a2.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) ptint_b1.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) ptint_b2.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) if(pos_cos==0.0): m_a=self.position_anneau(i+self.saut) m_b=self.position_anneau(i+self.saut*(1-self.coef_larg_interne)) ptint_a1.translate(m_a, 0.0, m_a*self.coef_decalage) ptint_a2.translate(m_a, 0.0, m_a*self.coef_decalage) ptint_b1.translate(m_b, 0.0, m_b*self.coef_decalage) ptint_b2.translate(m_b, 0.0, m_b*self.coef_decalage) ptint_a1.rotation(0.0, 0.0, i+self.saut) ptint_a2.rotation(0.0, 0.0, i+self.saut) ptint_b1.rotation(0.0, 0.0, i+self.saut*(1-self.coef_larg_interne)) ptint_b2.rotation(0.0, 0.0, i+self.saut*(1-self.coef_larg_interne)) elif(pos_cos+1.0==self.sub_cos): m_a=self.position_anneau(i+self.saut*self.coef_larg_interne) m_b=self.position_anneau(i) ptint_a1.translate(m_a, 0.0, m_a*self.coef_decalage) ptint_a2.translate(m_a, 0.0, m_a*self.coef_decalage) ptint_b1.translate(m_b, 0.0, m_b*self.coef_decalage) ptint_b2.translate(m_b, 0.0, m_b*self.coef_decalage) ptint_a1.rotation(0.0, 0.0, i+self.saut*self.coef_larg_interne) ptint_a2.rotation(0.0, 0.0, i+self.saut*self.coef_larg_interne) ptint_b1.rotation(0.0, 0.0, i) ptint_b2.rotation(0.0, 0.0, i) else: m_a=self.position_anneau(i+self.saut) m_b=self.position_anneau(i) ptint_a1.translate(m_a, 0.0, m_a*self.coef_decalage) ptint_a2.translate(m_a, 0.0, m_a*self.coef_decalage) ptint_b1.translate(m_b, 0.0, m_b*self.coef_decalage) ptint_b2.translate(m_b, 0.0, m_b*self.coef_decalage) ptint_a1.rotation(0.0, 0.0, i+self.saut) ptint_a2.rotation(0.0, 0.0, i+self.saut) ptint_b1.rotation(0.0, 0.0, i) ptint_b2.rotation(0.0, 0.0, i) f1=face(ptint_a1, ptint_a2, ptint_b1, ptint_b2) f1.dessiner(self.mesh) if(pos_cos==0.0): #Interieur cloison pt1=point(0.0, 0.0, larg_b*self.coef_larg_interne) pt2=point(0.0, 0.0, larg_b*self.coef_larg_interne) pt3=point(0.0, 0.0, 0.0) pt1.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) pt2.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) moy_b=self.position_anneau(i+self.saut*(1-self.coef_larg_interne)) pt1.translate(moy_b, 0.0, moy_b*self.coef_decalage) pt2.translate(moy_b, 0.0, moy_b*self.coef_decalage) pt3.translate(moy_b, 0.0, moy_b*self.coef_decalage) pt1.rotation(0.0, 0.0, i+self.saut*(1-self.coef_larg_interne)) pt2.rotation(0.0, 0.0, i+self.saut*(1-self.coef_larg_interne)) pt3.rotation(0.0, 0.0, i+self.saut*(1-self.coef_larg_interne)+self.saut*0.3) f1=face(pt1, pt2, pt3, pt3) f1.dessiner(self.mesh) if(i==720.0): pt1=point(0.0, 0.0, larg_b) pt2=point(0.0, 0.0, larg_b) pt3=point(0.0, 0.0, 0.0) pt1.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) pt2.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) moy_b=self.position_anneau(i) pt1.translate(moy_b, 0.0, moy_b*self.coef_decalage) pt2.translate(moy_b, 0.0, moy_b*self.coef_decalage) pt3.translate(moy_b, 0.0, moy_b*self.coef_decalage) pt1.rotation(0.0, 0.0, i) pt2.rotation(0.0, 0.0, i) pt3.rotation(0.0, 0.0, i-self.saut*0.3) f1=face(pt1, pt2, pt3, pt3) f1.dessiner(self.mesh) elif(pos_cos+1.0==self.sub_cos): pt1=point(0.0, 0.0, larg_a*self.coef_larg_interne) pt2=point(0.0, 0.0, larg_a*self.coef_larg_interne) pt3=point(0.0, 0.0, 0.0) pt1.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) pt2.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) moy_a=self.position_anneau(i+self.saut*self.coef_larg_interne) pt1.translate(moy_a, 0.0, moy_a*self.coef_decalage) pt2.translate(moy_a, 0.0, moy_a*self.coef_decalage) pt3.translate(moy_a, 0.0, moy_a*self.coef_decalage) pt1.rotation(0.0, 0.0, i+self.saut*self.coef_larg_interne) pt2.rotation(0.0, 0.0, i+self.saut*self.coef_larg_interne) pt3.rotation(0.0, 0.0, i+self.saut*self.coef_larg_interne+self.saut*0.3) f1=face(pt1, pt2, pt3, pt3) f1.dessiner(self.mesh) if i+self.saut>self.tours*360: #Fermeture if(pos_cos+1.0==self.sub_cos): pt1=point(0.0, 0.0, larg_a) pt2=point(0.0, 0.0, larg_a) pt3=point(0.0, 0.0, 0.0) pt1.rotation(0.0, self.rot_cercle+360.0*j/self.points, 0.0) pt2.rotation(0.0, self.rot_cercle+360.0*(j-1)/self.points, 0.0) ptmoy=self.position_anneau(i+self.saut) pt1.translate(ptmoy, 0.0, moy_a*self.coef_decalage) pt2.translate(ptmoy, 0.0, moy_a*self.coef_decalage) pt3.translate(ptmoy, 0.0, moy_a*self.coef_decalage) pt1.rotation(0.0, 0.0, i+self.saut) pt2.rotation(0.0, 0.0, i+self.saut) pt3.rotation(0.0, 0.0, i+self.saut+self.saut*0.3) f1=face(pt1, pt2, pt3, pt3) f1.dessiner(self.mesh) else: f1=face(ptext_a1, ptext_a2, ptint_a2, ptint_a1) f1.dessiner(self.mesh) j=j+1 i=i+self.saut pos_cos=pos_cos+1.0 if pos_cos>=self.sub_cos: pos_cos=0.0 scn=Scene.GetCurrent() # link object to current scene Blender.Window.EditMode(1) Blender.Window.EditMode(0,"Script remDoubles",1) self.ob=scn.objects.new(self.mesh, 'Cube') self.mesh.sel= True self.mesh.remDoubles(0.001) self.mesh.recalcNormals(0) #self.mesh.smooth() mod = self.ob.modifiers.append(Modifier.Types.SUBSURF) mod[Modifier.Settings.LEVELS] = 4 scn.objects.selected = [] Window.RedrawAll() def largeur_anneau(self, i, pos_cos): dist_a_prec=self.distance(i-360.0) dist_a=self.distance(i+self.saut) larg_a=(dist_a-dist_a_prec)/2.0 larg_a=larg_a*self.coef_cos+larg_a*math.cos( ((pos_cos)/self.sub_cos*360.0)/57.29577951308232) *(1-self.coef_cos)/2.0 larg_a=larg_a*self.coef_larg return larg_a def position_anneau(self, i): dist_a_prec=self.distance(i-360.0+self.saut) dist_a=self.distance(i+self.saut) moy_a=(dist_a_prec+dist_a)/2.0 return moy_a def distance(self, i): if(i<0.0): return 0.0 else: h=(i)/360.0 h_init=h for j in range(0, self.puissance): h=h*h_init h=h*0.1 return h def creer_point_loge(self, centre, pt_initial, coef): tmp_pt_bis=pt_initial.dupliquer(-centre._x, -centre._y, -centre._z) tmp_pt_bis.resize(coef, coef, coef) tmp_pt_bis.translate(centre._x, centre._y, centre._z) return tmp_pt_bis def moyenne_points(self, pt_a, pt_b, coef_a): coef_b=1.0-coef_a #tmp_point=point((pt_a._x+pt_b._x)/2.0, (pt_a._y+pt_b._y)/2.0, (pt_a._z+pt_b._z)/2.0) tmp_point=point(pt_a._x*coef_a+pt_b._x*coef_b, pt_a._y*coef_a+pt_b._y*coef_b, pt_a._z*coef_a+pt_b._z*coef_b) return tmp_point #POINT class point(object): def __init__(self, x, y, z, suivant=None): self._x=x self._y=y self._z=z self._suivant=suivant self._fin_plancher=None def rotation(self, x, y, z): x=x/57.29577951308232 y=y/57.29577951308232 z=z/57.29577951308232 cos_x=math.cos(x) sin_x=math.sin(x) cos_y=math.cos(y) sin_y=math.sin(y) cos_z=math.cos(z) sin_z=math.sin(z) if x!=0: x_tmp=self._x y_tmp=cos_x*self._y+sin_x*self._z z_tmp=(-sin_x)*self._y+cos_x*self._z self._x=x_tmp self._y=y_tmp self._z=z_tmp if y!=0: x_tmp=cos_y*self._x+sin_y*self._z y_tmp=self._y z_tmp=(-sin_y)*self._x+cos_y*self._z self._x=x_tmp self._y=y_tmp self._z=z_tmp if z!=0: x_tmp=cos_z*self._x+(-sin_z)*self._y y_tmp=sin_z*self._x+cos_z*self._y z_tmp=self._z self._x=x_tmp self._y=y_tmp self._z=z_tmp def translate(self, x, y, z): if x!=0: self._x=self._x+x; if y!=0: self._y=self._y+y; if z!=0: self._z=self._z+z; def resize(self, x, y, z): self._x=self._x*x self._y=self._y*y self._z=self._z*z def dessiner(self, mesh): mesh.verts.append(NMesh.Vert(self._x,self._y,self._z)) def assigner_suivant(self, suivant): self._suivant=suivant def tracer(self, me): self.dessiner(me) if self._suivant: self._suivant.tracer(me) def dupliquer(self, x, y, z): return point(self._x+x, self._y+y, self._z+z) #Face class face(object): def __init__(self, pt1, pt2, pt3, pt4=None): self.pt1=pt1 self.pt2=pt2 self.pt3=pt3 self.pt4=pt4 def dessiner(self, me): f=NMesh.Face() coords=[[self.pt1._x,self.pt1._y,self.pt1._z], [self.pt2._x,self.pt2._y,self.pt2._z], [self.pt3._x,self.pt3._y,self.pt3._z], [self.pt4._x,self.pt4._y,self.pt4._z]] faces= [ [3,2,1,0], [0,1,4], [1,2,4], [2,3,4], [3,0,4] ] me.verts.extend(coords) v1= me.verts[-1] v2= me.verts[-2] v3= me.verts[-3] v4= me.verts[-4] me.faces.extend([(v1,v2,v3,v4)]) #me.faces.extend(faces) mon_ammonite=ammonite()