def blenderstructure(self, ): nat = len(self.xyz.atomtypes) # create atoms for i in range(0, nat): loc = Mathutils.Vector(self.xyz.coord[i]) me = Mesh.Primitives.Icosphere(spheresubdivisions, atomicradii[self.xyz.atomtypes[i]]) me.materials = [materials[self.xyz.atomtypes[i]]] for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, 'Atom') obj.setLocation(loc) # form bonds between atoms for i in range(0, nat): for j in range(i + 1, nat): vec1 = Mathutils.Vector(self.xyz.coord[i]) vec2 = Mathutils.Vector(self.xyz.coord[j]) vec = vec2 - vec1 distcovalent = covalentradii[self.xyz.atomtypes[ i]] + covalentradii[self.xyz.atomtypes[j]] print "vec.length = %s distcovalent = %s" % (vec.length, distcovalent) if (vec.length - distcovalent) <= 0.10 * distcovalent: me = Mesh.Primitives.Tube(32, stickradius, vec.length) for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, 'Bond') axis = Mathutils.CrossVecs(Vector([0, 0, 1]), vec) angle = Mathutils.AngleBetweenVecs(Vector([0, 0, 1]), vec) rotmat = Mathutils.RotationMatrix(angle, 4, "R", axis) obj.setMatrix(obj.matrix * rotmat) obj.setLocation((vec1 + vec2) * 0.5) # vectors scale = 1.0 #1000.0 #1.0 #1000.0 for i in range(0, nat): loc = Mathutils.Vector(self.xyz.coord[i]) vec = Mathutils.Vector(self.xyz.vectors[i]) * scale # arrow tail me = Mesh.Primitives.Tube(32, arrowradius, vec.length) me.materials = [materials["arrow"]] for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, "Arrow-Tail") axis = Mathutils.CrossVecs(Vector([0, 0, 1]), vec) angle = Mathutils.AngleBetweenVecs(Vector([0, 0, 1]), vec) rotmat = Mathutils.RotationMatrix(angle, 4, "R", axis) obj.setMatrix(obj.matrix * rotmat) obj.setLocation(loc + 0.5 * vec) # arrow head me = Mesh.Primitives.Cone(32, 2 * arrowradius, 0.5) me.materials = [materials["arrow"]] for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, "Arrow-Head") axis = Mathutils.CrossVecs(Vector([0, 0, 1]), vec) angle = Mathutils.AngleBetweenVecs(Vector([0, 0, 1]), vec) rotmat = Mathutils.RotationMatrix(angle + 180.0, 4, "R", axis) obj.setMatrix(obj.matrix * rotmat) obj.setLocation(loc + vec)
def blenderstructure(self, ): for i in range(len(self.xyz.atomtypes)): me = Mesh.Primitives.Icosphere(spheresubdivisions, atomicradii[self.xyz.atomtypes[i]]) me.materials = [materials[self.xyz.atomtypes[i]]] for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, 'Mesh') obj.setLocation(self.xyz.coord[i][0], self.xyz.coord[i][1], self.xyz.coord[i][2]) for i in range(len(self.xyz.atomtypes)): for j in range(i + 1, len(self.xyz.atomtypes)): vec1 = Mathutils.Vector(self.xyz.coord[i]) vec2 = Mathutils.Vector(self.xyz.coord[j]) vec = vec2 - vec1 distcovalent = covalentradii[self.xyz.atomtypes[ i]] + covalentradii[self.xyz.atomtypes[j]] if (vec.length - distcovalent) <= 0.10 * distcovalent: me = Mesh.Primitives.Tube(32, stickradius, vec.length) for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, 'Cylinder') axis = Mathutils.CrossVecs(Vector([0, 0, 1]), vec) angle = Mathutils.AngleBetweenVecs(Vector([0, 0, 1]), vec) rotmat = Mathutils.RotationMatrix(angle, 4, "R", axis) obj.setMatrix(obj.matrix * rotmat) obj.setLocation((vec1 + vec2) * 0.5)
def cross(v_id, edges): def get_vec(edge): if edge.v1 == v_id: return edge.v2.co + edge.v1.co return edge.v1.co + edge.v2.co edges = list(edges) vec1 = get_vec(edges[0]) vec2 = get_vec(edges[1]) return Mathutils.CrossVecs(vec1, vec2).normalize()
def gui(): glClearColor(0,0,0,1) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) glEnable(GL_DEPTH_TEST) glViewport(0,0,500,500) glMatrixMode(GL_PROJECTION) glLoadIdentity() #glFrustum(-100,100,-100,100,30,500) glOrtho(-10,10,-10,10,-500,500) vect3=vect norm=sqrt(vect3[0]**2+vect3[1]**2+vect3[2]**2) vect3=vect3/norm vect2=Vector(0,1,0)-vect3[1]*vect3 norm=sqrt(vect2[0]**2+vect2[1]**2+vect2[2]**2) vect2=vect2/norm vecCros=Mathutils.CrossVecs(vect3,vect2) norm=sqrt(vecCros[0]**2+vecCros[1]**2+vecCros[2]**2) vect1=vecCros/norm buf2=Buffer(GL_FLOAT,16,[vect1[0],vect1[1],vect1[2],0, vect2[0],vect2[1],vect2[2],0, vect3[0],vect3[1],vect3[2],0, 0,0,0,1]) #buf2=Buffer(GL_FLOAT,16,[1,0,0,v1, # 0,1,0,v2, # 0,0,1,v3, # 0,0,0,1]) # glMultMatrixf(buf) glTranslatef(0,0,-50) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # glRotatef(a,1,1,1) glMultMatrixf(buf2) myCube() changeA()
def animatedensity(self, filenamelist, renderingpath, isovalue): context = self.scene.getRenderingContext() context.extensions = True context.renderPath = renderingpath #context.imageType=Render.JPEG context.sFrame = 1 context.eFrame = 1 context.sizeX = width context.sizeY = height cubeobject = cube(filenamelist[0]) cubeobject.readCube() atoms = [] ipokeytypeloc = Object.IpoKeyTypes.LOC ipokeytyperot = Object.IpoKeyTypes.ROT for i in range(len(cubeobject.atomtypes)): me = Mesh.Primitives.Icosphere( spheresubdivisions, atomicradii.get(cubeobject.atomtypes[i], 2.0)) me.materials = [ materials.get(cubeobject.atomtypes[i], materials["default"]) ] for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, 'Atom') obj.setLocation(cubeobject.coord[i][0], cubeobject.coord[i][1], cubeobject.coord[i][2]) atoms.append(obj) bonds = [] for i in range(len(cubeobject.atomtypes)): for j in range(i + 1, len(cubeobject.atomtypes)): vec1 = Mathutils.Vector(cubeobject.coord[i]) vec2 = Mathutils.Vector(cubeobject.coord[j]) vec = vec2 - vec1 distcovalent = covalentradii.get( cubeobject.atomtypes[i], 2.0) + covalentradii.get( cubeobject.atomtypes[j], 2.0) if (vec.length - distcovalent) <= 0.10 * distcovalent: me = Mesh.Primitives.Tube(32, stickradius, vec.length) for face in me.faces: face.smooth = True obj = self.scene.objects.new(me, 'Cylinder') axis = Mathutils.CrossVecs(Vector([0, 0, 1]), vec) angle = Mathutils.AngleBetweenVecs(Vector([0, 0, 1]), vec) rotmat = Mathutils.RotationMatrix(angle, 4, "R", axis) obj.setMatrix(obj.matrix * rotmat) obj.setLocation((vec1 + vec2) * 0.5) bonds.append([i, j, vec, obj, vec.length]) self.isosurface(cubeobject, isovalue) context.render() filename = "RENDER/image_0000.jpg" context.saveRenderedImage(filename) for j in range(1, len(filenamelist)): print(("Rendering:", j)) filename = "RENDER/image_%04d.jpg" % (j) for ob in self.scene.objects: if "Lobe" in ob.name: self.scene.unlink(ob) cubeobject = cube(filenamelist[j]) cubeobject.readCube() for i in range(len(atoms)): atoms[i].setLocation(cubeobject.coord[i][0], cubeobject.coord[i][1], cubeobject.coord[i][2]) atoms[i].insertIpoKey(ipokeytypeloc) for i in range(len(bonds)): vec1 = Mathutils.Vector(cubeobject.coord[bonds[i][0]]) vec2 = Mathutils.Vector(cubeobject.coord[bonds[i][1]]) vec = vec2 - vec1 dist = vec.length axis = Mathutils.CrossVecs(bonds[i][2], vec) angle = Mathutils.AngleBetweenVecs(bonds[i][2], vec) rotmat = Mathutils.RotationMatrix(angle, 4, "R", axis) bonds[i][3].setMatrix(bonds[i][3].matrix * rotmat) bonds[i][3].setLocation((vec1 + vec2) * 0.5) bonds[i][3].setSize(1.0, 1.0, dist / bonds[i][4]) bonds[i][3].insertIpoKey(ipokeytypeloc) bonds[i][3].insertIpoKey(ipokeytyperot) bonds[i][2] = vec bonds[i][4] = dist self.isosurface(cubeobject, isovalue) context.render() context.saveRenderedImage(filename)