def ExportMesh(scene, sceneNode, blenderScene, blenderObjectMesh): '''convert a blender mesh to an alchemedia node''' # get the blender mesh from teh object blenderMesh = blenderObjectMesh.data # create the mesh node meshNode = scene.CreateMeshNode(sceneNode) meshInfo = pyScene.pyMesh(scene, meshNode) meshInfo.SetName(blenderObjectMesh.getName()) # add the mesh materials hasMaterial = False materialMap = [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ] materials = blenderMesh.getMaterials(1) for i, material in enumerate(materials): if material != None: hasMaterial = True materialMap[i] = i materialNode = scene.CreateMaterialNode(meshNode, i) materialInfo = pyScene.pyMaterial(scene, materialNode) # export any texture for texture in material.getTextures(): if texture and texture.tex.type == Blender.Texture.Types.IMAGE: image = texture.tex.image if image != None: # create a texture and attache to the material name = image.getName() textureNode = scene.CreateTextureNode(name) scene.AddReference(materialNode, textureNode) # link the texture ID to and the materail textureInfo = pyScene.pyTexture(scene, textureNode) materialInfo.SetAmbientTextId(textureInfo.GetId()) materialInfo.SetDiffuseTextId(textureInfo.GetId()) # if the mesh do not have material add a default material if hasMaterial == False: materialMap[0] = 0 materialNode = scene.CreateMaterialNode(meshNode, 0) # create the vertex List and face arrays faceCount = len(blenderMesh.faces) vertexCount = len(blenderMesh.verts) faceIndexCountArray = pyScene.intArray(faceCount) vertexArray = pyScene.doubleArray(vertexCount * 3) faceMaterialIndexArray = pyScene.intArray(faceCount) vertexIndexArray = pyScene.intArray(faceCount * 4) normalArray = pyScene.doubleArray(faceCount * 4 * 3) uvArray = pyScene.doubleArray(faceCount * 4 * 2) # copy vertex points from the vertex list for i, v in enumerate(blenderMesh.verts): vertexArray[i * 3 + 0] = v.co.x vertexArray[i * 3 + 1] = v.co.y vertexArray[i * 3 + 2] = v.co.z # copy face data from the face list index = 0 if blenderMesh.hasFaceUV() == True: # mesh with uv mapping for i, face in enumerate(blenderMesh.faces): pointCount = len(face.v) faceMaterialIndexArray[i] = face.mat faceIndexCountArray[i] = pointCount for j, point in enumerate(face): vertexIndexArray[index + j] = point.index normalIndex = (index + j) * 3 normalArray[normalIndex + 0] = point.no.x normalArray[normalIndex + 1] = point.no.y normalArray[normalIndex + 2] = point.no.z uvIndex = (index + j) * 2 uvArray[uvIndex + 0] = face.uv[j][0] uvArray[uvIndex + 1] = face.uv[j][1] index = index + pointCount else: # mesh have no uv mapping for i, face in enumerate(blenderMesh.faces): pointCount = len(face.v) faceMaterialIndexArray[i] = face.mat faceIndexCountArray[i] = pointCount for j, point in enumerate(face): vertexIndexArray[index + j] = point.index normalIndex = (index + j) * 3 normalArray[normalIndex + 0] = point.no.x normalArray[normalIndex + 1] = point.no.y normalArray[normalIndex + 2] = point.no.z uvIndex = (index + j) * 2 uvArray[uvIndex + 0] = 0 uvArray[uvIndex + 1] = 0 index = index + pointCount # create the alchemedia mesh meshInfo.BuildFromVertexListIndexList(faceCount, faceIndexCountArray, faceMaterialIndexArray, vertexArray, vertexIndexArray, normalArray, uvArray) # normal are completly bogus in blender, just recalculate proper normals #meshInfo.SmoothNormals (blenderMesh.degr) meshInfo.SmoothNormals(30)
def CreateBlenderMeshObjectFromNode (scene, meshNode, blenderScene): # Get a alchemedia mesh for the node and buidl a blender mesh mesh = pyScene.pyMesh (scene, meshNode) # create a blender mesh newMesh = bpy.data.meshes.new(scene.GetNodeName(meshNode)) # Create all verte and face data vertexList = [] vertexCount = mesh.GetVertexCount() for i in range(0, vertexCount): vertex = mesh.GetVertex (i) vertexList.append([vertex.x, vertex.y, vertex.z]) faceList = [] faceNode = mesh.GetFirstTriangle() while faceNode != None: face = mesh.GetTriangle (faceNode); faceList.append([face.p0.vertex, face.p1.vertex, face.p2.vertex]) faceNode = mesh.GetNextTriangle(faceNode) newMesh.verts.extend(vertexList) # add vertices to mesh newMesh.faces.extend(faceList) # add faces to the mesh (also adds edges) # create all materials from this mesh materialIndex = 0 materialMap = {} meshMaterials = newMesh.materials childLink = scene.GetFirstChildLink (meshNode) while childLink != None: childNode = scene.GetNodeFromLink(childLink) if scene.IsMaterialNode(childNode) == True: # make a blender material and a alchemdia material material = CreateBlenderMaterialFromNode (scene, childNode, blenderScene) meshMaterials.append(material) # add a map key for asigning faces sourceMaterial = pyScene.pyMaterial(scene, childNode) materialMap[sourceMaterial.GetId()] = materialIndex materialIndex = materialIndex + 1 childLink = scene.GetNextChildLink (meshNode, childLink) newMesh.materials = meshMaterials # In a secund pass asign material and uv mapping to faces faceIndex = 0 newMesh.faceUV= True faceNode = mesh.GetFirstTriangle() while faceNode != None: face = mesh.GetTriangle (faceNode); newMesh.faces[faceIndex].mat = materialMap[face.materialIndex] uv0 = mesh.GetUV0(face.p0.uv0) uv1 = mesh.GetUV0(face.p1.uv0) uv2 = mesh.GetUV0(face.p2.uv0) newMesh.faces[faceIndex].uv = [Vector(uv0.x, uv0.y), Vector(uv1.x, uv1.y), Vector(uv2.x, uv2.y)] faceIndex = faceIndex + 1 faceNode = mesh.GetNextTriangle(faceNode) # link mesh to blend objects object = blenderScene.objects.new(newMesh, scene.GetNodeName(meshNode)) # calculate normal after mesh is parented #newMesh.mode |= Blender.Mesh.Modes.AUTOSMOOTH #newMesh.degr = 30 #for face in newMesh.faces: # face.smooth = 1 #newMesh.calcNormals() return object
def ExportMesh(scene, sceneNode, blenderScene, blenderObjectMesh): '''convert a blender mesh to an alchemedia node''' # get the blender mesh from teh object blenderMesh = blenderObjectMesh.data # create the mesh node meshNode = scene.CreateMeshNode(sceneNode) meshInfo = pyScene.pyMesh(scene, meshNode) meshInfo.SetName (blenderObjectMesh.getName()) # add the mesh materials hasMaterial = False materialMap = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; materials = blenderMesh.getMaterials (1) for i, material in enumerate (materials): if material != None: hasMaterial = True materialMap[i] = i; materialNode = scene.CreateMaterialNode(meshNode, i) materialInfo = pyScene.pyMaterial(scene, materialNode) # export any texture for texture in material.getTextures(): if texture and texture.tex.type == Blender.Texture.Types.IMAGE: image = texture.tex.image if image != None: # create a texture and attache to the material name = image.getName() textureNode = scene.CreateTextureNode(name) scene.AddReference (materialNode, textureNode) # link the texture ID to and the materail textureInfo = pyScene.pyTexture(scene, textureNode) materialInfo.SetAmbientTextId (textureInfo.GetId()) materialInfo.SetDiffuseTextId (textureInfo.GetId()) # if the mesh do not have material add a default material if hasMaterial == False: materialMap[0] = 0; materialNode = scene.CreateMaterialNode(meshNode, 0) # create the vertex List and face arrays faceCount = len(blenderMesh.faces) vertexCount = len(blenderMesh.verts) faceIndexCountArray = pyScene.intArray(faceCount) vertexArray = pyScene.doubleArray(vertexCount * 3) faceMaterialIndexArray = pyScene.intArray(faceCount) vertexIndexArray = pyScene.intArray(faceCount * 4) normalArray = pyScene.doubleArray(faceCount * 4 * 3) uvArray = pyScene.doubleArray(faceCount * 4 * 2) # copy vertex points from the vertex list for i, v in enumerate(blenderMesh.verts): vertexArray[i * 3 + 0] = v.co.x vertexArray[i * 3 + 1] = v.co.y vertexArray[i * 3 + 2] = v.co.z # copy face data from the face list index = 0 if blenderMesh.hasFaceUV() == True: # mesh with uv mapping for i, face in enumerate(blenderMesh.faces): pointCount = len(face.v) faceMaterialIndexArray[i] = face.mat faceIndexCountArray[i] = pointCount for j, point in enumerate(face): vertexIndexArray[index + j] = point.index normalIndex = (index + j) * 3 normalArray[normalIndex + 0] = point.no.x normalArray[normalIndex + 1] = point.no.y normalArray[normalIndex + 2] = point.no.z uvIndex = (index + j) * 2 uvArray[uvIndex + 0] = face.uv[j][0] uvArray[uvIndex + 1] = face.uv[j][1] index = index + pointCount else: # mesh have no uv mapping for i, face in enumerate(blenderMesh.faces): pointCount = len(face.v) faceMaterialIndexArray[i] = face.mat faceIndexCountArray[i] = pointCount for j, point in enumerate(face): vertexIndexArray[index + j] = point.index normalIndex = (index + j) * 3 normalArray[normalIndex + 0] = point.no.x normalArray[normalIndex + 1] = point.no.y normalArray[normalIndex + 2] = point.no.z uvIndex = (index + j) * 2 uvArray[uvIndex + 0] = 0 uvArray[uvIndex + 1] = 0 index = index + pointCount # create the alchemedia mesh meshInfo.BuildFromVertexListIndexList(faceCount, faceIndexCountArray, faceMaterialIndexArray, vertexArray, vertexIndexArray, normalArray, uvArray); # normal are completly bogus in blender, just recalculate proper normals #meshInfo.SmoothNormals (blenderMesh.degr) meshInfo.SmoothNormals (30)
def CreateBlenderMeshObjectFromNode(scene, meshNode, blenderScene): # Get a alchemedia mesh for the node and buidl a blender mesh mesh = pyScene.pyMesh(scene, meshNode) # create a blender mesh newMesh = bpy.data.meshes.new(scene.GetNodeName(meshNode)) # Create all verte and face data vertexList = [] vertexCount = mesh.GetVertexCount() for i in range(0, vertexCount): vertex = mesh.GetVertex(i) vertexList.append([vertex.x, vertex.y, vertex.z]) faceList = [] faceNode = mesh.GetFirstTriangle() while faceNode != None: face = mesh.GetTriangle(faceNode) faceList.append([face.p0.vertex, face.p1.vertex, face.p2.vertex]) faceNode = mesh.GetNextTriangle(faceNode) newMesh.verts.extend(vertexList) # add vertices to mesh newMesh.faces.extend(faceList) # add faces to the mesh (also adds edges) # create all materials from this mesh materialIndex = 0 materialMap = {} meshMaterials = newMesh.materials childLink = scene.GetFirstChildLink(meshNode) while childLink != None: childNode = scene.GetNodeFromLink(childLink) if scene.IsMaterialNode(childNode) == True: # make a blender material and a alchemdia material material = CreateBlenderMaterialFromNode(scene, childNode, blenderScene) meshMaterials.append(material) # add a map key for asigning faces sourceMaterial = pyScene.pyMaterial(scene, childNode) materialMap[sourceMaterial.GetId()] = materialIndex materialIndex = materialIndex + 1 childLink = scene.GetNextChildLink(meshNode, childLink) newMesh.materials = meshMaterials # In a secund pass asign material and uv mapping to faces faceIndex = 0 newMesh.faceUV = True faceNode = mesh.GetFirstTriangle() while faceNode != None: face = mesh.GetTriangle(faceNode) newMesh.faces[faceIndex].mat = materialMap[face.materialIndex] uv0 = mesh.GetUV0(face.p0.uv0) uv1 = mesh.GetUV0(face.p1.uv0) uv2 = mesh.GetUV0(face.p2.uv0) newMesh.faces[faceIndex].uv = [ Vector(uv0.x, uv0.y), Vector(uv1.x, uv1.y), Vector(uv2.x, uv2.y) ] faceIndex = faceIndex + 1 faceNode = mesh.GetNextTriangle(faceNode) # link mesh to blend objects object = blenderScene.objects.new(newMesh, scene.GetNodeName(meshNode)) # calculate normal after mesh is parented #newMesh.mode |= Blender.Mesh.Modes.AUTOSMOOTH #newMesh.degr = 30 #for face in newMesh.faces: # face.smooth = 1 #newMesh.calcNormals() return object