Пример #1
0
    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)
Пример #2
0
 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)
Пример #3
0
    def MakeVertexRing(vertex_list, angle):
        m = Mathutils.RotationMatrix(angle, 3, "z")
        for i in xrange(MinorDivisions):
            phi = (pi * 2 * i / MinorDivisions) + StartMinorAngle
            x = MajorRadius + MinorRadius * cos(phi)
            y = 0.0
            z = MinorRadius * sin(phi)

            v = Mathutils.Vector([x, y, z])
            v = Mathutils.VecMultMat(v, m)

            vertex_list.append(NMesh.Vert(v.x, v.y, v.z))
Пример #4
0
def exportFrame(_armature, i, bone, out, swap):
    #if (i == 30):
    #    print "===Frame :"+ str(i)+"========"
    Blender.Set("curframe",i)
    pose = _armature.getPose()
    armpos = _armature.mat

    identity = Mathutils.Matrix([1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1])
    rotation = Mathutils.RotationMatrix(-90, 4, 'x')
    swapyz = rotation * identity


    if (swap == True):
        initial = Mathutils.Matrix(bone.matrix['ARMATURESPACE']) * armpos * swapyz
        initial.invert()
        posematrix = Mathutils.Matrix(pose.bones[bone.name].poseMatrix) * armpos * swapyz
    else:
        initial = Mathutils.Matrix(bone.matrix['ARMATURESPACE']) * armpos
        initial.invert()
        posematrix = Mathutils.Matrix(pose.bones[bone.name].poseMatrix) * armpos

    
    finalmatrix = initial * posematrix

    #if (i==30):
    #    print _armature.mat
    
    floats = array('f')
    #if (i == 30):
    #    print finalmatrix
    for a in range(4):
        for b in range(4):
            floats.append(finalmatrix[a][b])

    floats.tofile(out)
    
    #export only pose matrix
    """poser = array('f')
Пример #5
0
def analogAction(x):
    cube.value = x
    angle = x
    cube.localOrientation = Mathutils.RotationMatrix(angle, 3, 'z')
    # deprecated: setOrientation(orn)
    print "Analog Value",x," Angle:",angle
Пример #6
0
    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)
Пример #7
0
def calcArc(center, radius, start, end, arc_res, triples):
    """calculate Points (or BezierTriples) for ARC/CIRCLEs representation.
	
	Given parameters of the ARC/CIRCLE,
	returns points/segments (or BezierTriples) and centerPoint
	"""
    # center is currently set by object
    # if start > end: start = start - 360
    if end > 360: end = end % 360.0

    startmatrix = Mathutils.RotationMatrix(-start, 3, "Z")
    startpoint = startmatrix * Mathutils.Vector(radius, 0, 0)
    endmatrix = Mathutils.RotationMatrix(-end, 3, "Z")
    endpoint = endmatrix * Mathutils.Vector(radius, 0, 0)

    if end < start: end += 360.0
    angle = end - start
    #length = radians(angle) * radius

    if not triples:  #IF mesh-representation -----------
        print 'mesh-representation\n'
        if arc_res > 1024: arc_res = 1024
        elif arc_res < 4: arc_res = 4
        pieces = int(abs(angle) /
                     (360.0 / arc_res))  # set a fixed step of ARC_RESOLUTION
        if pieces < 3: pieces = 3
        step = angle / pieces  # set step so pieces * step = degrees in arc
        stepmatrix = Mathutils.RotationMatrix(-step, 3, "Z")

        points = [startpoint]
        point = startpoint
        for i in xrange(int(pieces) - 1):
            point = stepmatrix * point
            points.append(point)
        points.append(endpoint)

        if center:
            centerVec = Mathutils.Vector(center)
            #points = [point + centerVec for point in points()]
            points = [point + centerVec for point in points]
        # vector to point convertion:
        points = [list(point) for point in points]
        return points

    else:  #IF curve-representation ---------------
        if arc_res > 32: arc_res = 32
        elif arc_res < 3: arc_res = 3
        pieces = int(abs(angle) /
                     (360.0 / arc_res))  # set a fixed step of ARC_RESOLUTION
        if pieces < 2: pieces = 2
        step = angle / pieces  # set step so pieces * step = degrees in arc
        stepmatrix = Mathutils.RotationMatrix(-step, 3, "Z")

        # correct Bezier curves representation for free segmented circles/arcs
        step2 = radians(step * 0.5)
        bulg = radius * (1 - cos(step2))
        deltaY = 4.0 * bulg / (3.0 * sin(step2))
        print 'deb:calcArcCurve: bulg, deltaY:\n', bulg, deltaY  #---------
        print 'deb:calcArcCurve: step:\n', step  #---------
        handler0 = Mathutils.Vector(0.0, -deltaY, 0.0)

        points = [startpoint]
        handler = startmatrix * handler0
        endhandler = endmatrix * handler0
        handlers1 = [startpoint + handler]
        handlers2 = [startpoint - handler]
        point = Mathutils.Vector(startpoint)
        for i in xrange(int(pieces) - 1):
            point = stepmatrix * point
            handler = stepmatrix * handler
            handler1 = point + handler
            handler2 = point - handler
            points.append(point)
            handlers1.append(handler1)
            handlers2.append(handler2)
        points.append(endpoint)
        handlers1.append(endpoint + endhandler)
        handlers2.append(endpoint - endhandler)
        VectorTriples = [
            list(h1) + list(p) + list(h2)
            for h1, p, h2 in zip(handlers1, points, handlers2)
        ]
        print 'deb:calcArcCurve: handlers1:\n', handlers1  #---------
        print 'deb:calcArcCurve: points:\n', points  #---------
        print 'deb:calcArcCurve: handlers2:\n', handlers2  #---------
        print 'deb:calcArcCurve: VectorTriples:\n', VectorTriples  #---------
        return VectorTriples