def makeSphere(self, nu, nv, x, y, z, rad, mat): yi = self.yi ID = yafrayinterface.new_uintp() yi.startGeometry(); if not yi.startTriMeshPtr(ID, 2+(nu-1)*nv, 2*(nu-1)*nv, False, False): print "error on starting trimesh!\n" yi.addVertex(x, y, z+rad); yi.addVertex(x, y, z-rad); for v in range(0, nv): t = v/float(nv) sin_v = sin(2.0*pi*t) cos_v = cos(2.0*pi*t) for u in range(1, nu): s = u/float(nu); sin_u = sin(pi*s) cos_u = cos(pi*s) yi.addVertex(x + cos_v*sin_u*rad, y + sin_v*sin_u*rad, z + cos_u*rad) for v in range(0, nv): yi.addTriangle( 0, 2+v*(nu-1), 2+((v+1)%nv)*(nu-1), mat ); yi.addTriangle( 1, ((v+1)%nv)*(nu-1)+nu, v*(nu-1)+nu, mat ); for u in range(0, nu-2): yi.addTriangle( 2+v*(nu-1)+u, 2+v*(nu-1)+u+1, 2+((v+1)%nv)*(nu-1)+u, mat ); yi.addTriangle( 2+v*(nu-1)+u+1, 2+((v+1)%nv)*(nu-1)+u+1, 2+((v+1)%nv)*(nu-1)+u, mat ); yi.endTriMesh(); yi.endGeometry(); return yafrayinterface.uintp_value(ID)
def createLight(self, yi, obj, matrix = None, lamp_mat = None, dupliNum = None): name = obj.name if dupliNum != None: name += str(dupliNum) if matrix == None: matrix = obj.getMatrix() pos = matrix[3] dir = matrix[2] up = matrix[1] to = [pos[0] - dir[0], pos[1] - dir[1], pos[2] - dir[2]] yi.paramsClearAll() props = obj.properties["YafRay"] lampType = props["type"] power = props["power"] color = props["color"] print "INFO: Adding Lamp:", name, " type: ", lampType if lampType == "Point": yi.paramsSetString("type", "pointlight") power = 0.5 * power * power elif lampType == "Sphere": radius = props["radius"] power = 0.5*power*power/(radius * radius) if props["createGeometry"] == True: ID = self.makeSphere(24, 48, pos[0], pos[1], pos[2], radius, lamp_mat) yi.paramsSetInt("object", ID) yi.paramsSetString("type", "spherelight") yi.paramsSetInt("samples", props["samples"]) yi.paramsSetFloat("radius", radius) elif lampType == "Spot": light = obj.getData() yi.paramsSetString("type", "spotlight") #print "spot ", light.getSpotSize() yi.paramsSetFloat("cone_angle", light.getSpotSize() / 2) yi.paramsSetFloat("blend", light.getSpotBlend()) yi.paramsSetPoint("to", to[0], to[1], to[2]) power = 0.5*power*power elif lampType == "Sun": yi.paramsSetString("type", "sunlight") yi.paramsSetInt("samples", props["samples"]) yi.paramsSetFloat("angle", props["angle"]) yi.paramsSetPoint("direction", dir[0], dir[1], dir[2]) elif lampType == "Directional": yi.paramsSetString("type", "directional") #if props["infinite"] == True: yi.paramsSetBool("infinite", props["infinite"]) yi.paramsSetFloat("radius", props["radius"]) yi.paramsSetPoint("direction", dir[0], dir[1], dir[2]) elif lampType == "Area": yi.paramsSetString("type", "arealight") areaLight = obj.getData() sizeX = areaLight.getAreaSizeX() #sizeY = areaLight.getAreaSizeY() sizeY = sizeX matrix = matrix.__copy__() matrix.transpose() # generate an untransformed rectangle in the XY plane with # the light's position as the centerpoint and transform it # using its transformation matrix point = Vector(-sizeX/2, -sizeY/2, 0, 1) corner1 = Vector(-sizeX/2, sizeY/2, 0, 1) corner2 = Vector(sizeX/2, sizeY/2, 0, 1) corner3 = Vector(sizeX/2, -sizeY/2, 0, 1) point = matrix * point corner1 = matrix * corner1 corner2 = matrix * corner2 corner3 = matrix * corner3 #print "point: ", point, corner1, corner2, corner3 if props["createGeometry"] == True: ID = yafrayinterface.new_uintp() yi.startGeometry(); yi.startTriMesh(ID, 4, 2, False, False); idx1 = yi.addVertex(point[0], point[1], point[2]); idx2 = yi.addVertex(corner1[0], corner1[1], corner1[2]); idx3 = yi.addVertex(corner2[0], corner2[1], corner2[2]); idx4 = yi.addVertex(corner3[0], corner3[1], corner3[2]); yi.addTriangle(idx1, idx2, idx3, lamp_mat); yi.addTriangle(idx1, idx3, idx4, lamp_mat); yi.endTriMesh(); yi.endGeometry(); yi.paramsSetInt("object", yafrayinterface.uintp_value(ID)); yi.paramsClearAll(); yi.paramsSetString("type", "arealight"); yi.paramsSetInt("samples", props["samples"]) yi.paramsSetPoint("corner", point[0], point[1], point[2]); yi.paramsSetPoint("point1", corner1[0], corner1[1], corner1[2]); yi.paramsSetPoint("point2", corner3[0], corner3[1], corner3[2]); yi.paramsSetPoint("from", pos[0], pos[1], pos[2]) yi.paramsSetColor("color", color[0], color[1], color[2]) yi.paramsSetFloat("power", power) yi.createLight(name)
def createObject(self, yi, materialMap): print "INFO: Adding Object: ",self,self.shape yi.paramsClearAll() ID = yafrayinterface.new_uintp() ID_val = yafrayinterface.uintp_value(ID) smooth = False meshlight = False hasOrco = False hasUV = False yi.startGeometry() yi.startTriMeshPtr(ID, self.nb_vertex(), self.nb_triangles(), hasOrco, hasUV, 0) ind = 0 for v in self.vertices_list: if hasOrco: yi.addVertex(v.co[0], v.co[1], v.co[2],ov[ind][0], ov[ind][1], ov[ind][2] ) ind += 1 else: yi.addVertex(v[0], v[1],v[2]) for f in self.triangles_list: ## if f.smooth == True: ## smooth = True smooth = False ## if meshlight: ymat = ml_mat ## else: ## if renderer["clayRender"] == True: ## ymat = self.materialMap["default"] ## elif obj.getType() == 'Curve': ## curve = obj.getData() ## smooth = True ## if len(curve.getMaterials()) != 0: ## mat = curve.getMaterials()[0] ## if mat in self.materialMap: ## ymat = self.materialMap[mat] ## else: ## ymat = self.materialMap["default"] ## elif len(mesh.materials) != 0: ## mat = mesh.materials[f.mat] ## if mat in self.materialMap: ## ymat = self.materialMap[mat] ## else: ## ymat = self.materialMap["default"] ## else: ## ymat = self.materialMap["default"] ## ymat = self.materialMap["default"] ymat = materialMap[self.material] if hasUV == True: uv0 = yi.addUV(f.uv[0][0], f.uv[0][1]) uv1 = yi.addUV(f.uv[1][0], f.uv[1][1]) uv2 = yi.addUV(f.uv[2][0], f.uv[2][1]) yi.addTriangle(f.v[0].index, f.v[1].index, f.v[2].index, uv0, uv1, uv2, ymat) else: yi.addTriangle(f[0], f[1], f[2], ymat) if len(f) == 4: if hasUV == True: uv3 = yi.addUV(f.uv[3][0], f.uv[3][1]) yi.addTriangle(f.v[2].index, f.v[3].index, f.v[0].index, uv2, uv3, uv0, ymat) else: yi.addTriangle(f.v[2].index, f.v[3].index, f.v[0].index, ymat) yi.endTriMesh() if smooth == True: if mesh.mode & Blender.Mesh.Modes.AUTOSMOOTH: yi.smoothMesh(0, mesh.degr) else: yi.smoothMesh(0, 181) yi.smoothMesh(0, 181) yi.endGeometry() if meshlight: # add mesh light yi.paramsClearAll() yi.paramsSetString("type", "meshlight") yi.paramsSetBool("double_sided", objProp["double_sided"]) c = objProp["color"] yi.paramsSetColor("color", c[0], c[1], c[2]) yi.paramsSetFloat("power", objProp["power"]) yi.paramsSetInt("samples", objProp["samples"]) yi.paramsSetInt("object", yafrayinterface.uintp_value(ID)) yi.createLight(obj.name) yi.paramsClearAll()
def writeObject(self, yi, obj, matrix = None): print "INFO: Adding Object: " + obj.getName() yi.paramsClearAll() objProp = obj.properties["YafRay"] scene = Scene.GetCurrent() renderer = scene.properties["YafRay"]["Renderer"] worldProp = scene.world.properties["YafRay"] mesh = Mesh.New() mesh.getFromObject(obj, 0, 1) # CHECK if the object has an orco mapped texture hasOrco = False for mat in mesh.materials: if mat == None: continue mtextures = mat.getTextures() if hasattr(mat, 'enabledTextures'): for m in mat.enabledTextures: if mtextures[m].texco == Blender.Texture.TexCo.ORCO: hasOrco = True break if hasOrco: # Keep a copy of the untransformed vertex and bring them # into a (-1 -1 -1) (1 1 1) bounding box ov = [] bbMin, bbMax = getBBCorners(obj) # print bbMin, bbMax delta = [] for i in range(3): delta.append(bbMax[i] - bbMin[i]) if delta[i] < 0.0001: delta[i] = 1 # print "delta", delta for v in mesh.verts: normCo = [] for i in range(3): normCo.append(2 * (v.co[i] - bbMin[i]) / delta[i] - 1) ov.append([normCo[0], normCo[1], normCo[2]]) if matrix == None: mesh.transform(obj.getMatrix()) else: mesh.transform(matrix) hasUV = mesh.faceUV ID = yafrayinterface.new_uintp() ID_val = yafrayinterface.uintp_value(ID) smooth = False meshlight = objProp["meshlight"] if meshlight: # add mesh light material yi.paramsClearAll(); yi.paramsSetString("type", "light_mat"); yi.paramsSetBool("double_sided", objProp["double_sided"]) c = objProp["color"]; yi.paramsSetColor("color", c[0], c[1], c[2]) yi.paramsSetFloat("power", objProp["power"]) ml_matname = "ML_" ml_matname += obj.name ml_mat = yi.createMaterial(ml_matname); yi.paramsClearAll() isVolume = objProp["volume"] if isVolume: yi.paramsClearAll(); volregion_type = objProp["volregionType"] if "ExpDensityVolume" == volregion_type: yi.paramsSetString("type", "ExpDensityVolume"); yi.paramsSetFloat("a", objProp["a"]) yi.paramsSetFloat("b", objProp["b"]) elif "UniformVolume" == volregion_type: yi.paramsSetString("type", "UniformVolume"); elif "NoiseVolume" == volregion_type: yi.paramsSetString("type", "NoiseVolume"); yi.paramsSetFloat("sharpness", objProp["sharpness"]) yi.paramsSetFloat("cover", objProp["cover"]) yi.paramsSetFloat("density", objProp["density"]) elif "GridVolume" == volregion_type: yi.paramsSetString("type", "GridVolume"); elif "SkyVolume" == volregion_type: yi.paramsSetString("type", "SkyVolume"); yi.paramsSetFloat("sigma_a", objProp["sigma_a"]) yi.paramsSetFloat("sigma_s", objProp["sigma_s"]) yi.paramsSetFloat("l_e", objProp["l_e"]) yi.paramsSetFloat("g", objProp["g"]) yi.paramsSetInt("attgridScale", worldProp["attgridScale"]) # derive the AABB from the supplied mesh, find the max and # min of the mesh's geometry and use that for the AABB min = [1e10, 1e10, 1e10] max = [-1e10, -1e10, -1e10] for v in mesh.verts: vertLoc = v.co if vertLoc[0] < min[0]: min[0] = vertLoc[0] if vertLoc[1] < min[1]: min[1] = vertLoc[1] if vertLoc[2] < min[2]: min[2] = vertLoc[2] if vertLoc[0] > max[0]: max[0] = vertLoc[0] if vertLoc[1] > max[1]: max[1] = vertLoc[1] if vertLoc[2] > max[2]: max[2] = vertLoc[2] yi.paramsSetFloat("minX", min[0]) yi.paramsSetFloat("minY", min[1]) yi.paramsSetFloat("minZ", min[2]) yi.paramsSetFloat("maxX", max[0]) yi.paramsSetFloat("maxY", max[1]) yi.paramsSetFloat("maxZ", max[2]) yi.createVolumeRegion(obj.name) yi.paramsClearAll() return; yi.startGeometry() yi.startTriMeshPtr(ID, len(mesh.verts), len(mesh.faces), hasOrco, hasUV, 0) ind = 0 for v in mesh.verts: if hasOrco: yi.addVertex(v.co[0], v.co[1], v.co[2],ov[ind][0], ov[ind][1], ov[ind][2] ) ind += 1 else: yi.addVertex(v.co[0], v.co[1], v.co[2]) for f in mesh.faces: if f.smooth == True: smooth = True if meshlight: ymat = ml_mat else: if renderer["clayRender"] == True: ymat = self.materialMap["default"] elif obj.getType() == 'Curve': curve = obj.getData() smooth = True if len(curve.getMaterials()) != 0: mat = curve.getMaterials()[0] if mat in self.materialMap: ymat = self.materialMap[mat] else: ymat = self.materialMap["default"] elif len(mesh.materials) != 0: mat = mesh.materials[f.mat] if mat in self.materialMap: ymat = self.materialMap[mat] else: ymat = self.materialMap["default"] else: ymat = self.materialMap["default"] if hasUV == True: uv0 = yi.addUV(f.uv[0][0], f.uv[0][1]) uv1 = yi.addUV(f.uv[1][0], f.uv[1][1]) uv2 = yi.addUV(f.uv[2][0], f.uv[2][1]) yi.addTriangle(f.v[0].index, f.v[1].index, f.v[2].index, uv0, uv1, uv2, ymat) else: yi.addTriangle(f.v[0].index, f.v[1].index, f.v[2].index, ymat) if len(f) == 4: if hasUV == True: uv3 = yi.addUV(f.uv[3][0], f.uv[3][1]) yi.addTriangle(f.v[2].index, f.v[3].index, f.v[0].index, uv2, uv3, uv0, ymat) else: yi.addTriangle(f.v[2].index, f.v[3].index, f.v[0].index, ymat) yi.endTriMesh() if smooth == True: if mesh.mode & Blender.Mesh.Modes.AUTOSMOOTH: yi.smoothMesh(0, mesh.degr) else: yi.smoothMesh(0, 181) yi.endGeometry() if meshlight: # add mesh light yi.paramsClearAll() yi.paramsSetString("type", "meshlight") yi.paramsSetBool("double_sided", objProp["double_sided"]) c = objProp["color"] yi.paramsSetColor("color", c[0], c[1], c[2]) yi.paramsSetFloat("power", objProp["power"]) yi.paramsSetInt("samples", objProp["samples"]) yi.paramsSetInt("object", yafrayinterface.uintp_value(ID)) yi.createLight(obj.name) yi.paramsClearAll()