def CreateBlenderTextureFromNode (scene, textureNode, blenderScene, path): tex = Texture.New(scene.GetNodeName(textureNode)) texture = pyScene.pyTexture (scene, textureNode) imageName = path + '/' + texture.GetImageName() image = Blender.Image.Load(imageName) tex.setImage(image) return tex
def CreateBlenderTextureFromNode(scene, textureNode, blenderScene, path): tex = Texture.New(scene.GetNodeName(textureNode)) texture = pyScene.pyTexture(scene, textureNode) imageName = path + '/' + texture.GetImageName() image = Blender.Image.Load(imageName) tex.setImage(image) return tex
def CreateBlenderMaterialFromNode (scene, materialNode, blenderScene): mat = Material.New(scene.GetNodeName(materialNode)) # load the textures for this material childLink = scene.GetFirstChildLink (materialNode) while childLink != None: textureNode = scene.GetNodeFromLink(childLink) if scene.IsTextureNode(textureNode) == True: sourceTexture = pyScene.pyTexture(scene, textureNode) texture = g_textureMap[sourceTexture.GetId()] #mat.setTexture(0, texture) mat.setTexture(0, texture, Texture.TexCo.UV, Texture.MapTo.COL) childLink = scene.GetNextChildLink (materialNode, childLink) return mat
def CreateBlenderMaterialFromNode(scene, materialNode, blenderScene): mat = Material.New(scene.GetNodeName(materialNode)) # load the textures for this material childLink = scene.GetFirstChildLink(materialNode) while childLink != None: textureNode = scene.GetNodeFromLink(childLink) if scene.IsTextureNode(textureNode) == True: sourceTexture = pyScene.pyTexture(scene, textureNode) texture = g_textureMap[sourceTexture.GetId()] #mat.setTexture(0, texture) mat.setTexture(0, texture, Texture.TexCo.UV, Texture.MapTo.COL) childLink = scene.GetNextChildLink(materialNode, childLink) return mat
def LoadAlchemediaScene(filename): ''' Load a newton Alchemedia file ''' scene = pyScene.pyScene() scene.Load (filename) root = scene.GetRoot() # get the active blender scene blenderScene = bpy.data.scenes.active # load all unique textures (path, name) = os.path.split (filename) childLink = scene.GetFirstChildLink (root) while childLink != None: textureNode = scene.GetNodeFromLink(childLink) if scene.IsTextureNode(textureNode) == True: texture = CreateBlenderTextureFromNode (scene, textureNode, blenderScene, path) # add a map key for asigning faces sourceTexture = pyScene.pyTexture(scene, textureNode) g_textureMap[sourceTexture.GetId()] = texture childLink = scene.GetNextChildLink (root, childLink) # import all objects into a blender scene myChidren = [] childLink = scene.GetFirstChildLink (root) while childLink != None: node = scene.GetNodeFromLink(childLink) if scene.IsSceneNode(node) == True: LoadNodesScene(scene, node, blenderScene, myChidren) childLink = scene.GetNextChildLink (root, childLink) # make sure everthing is updated before exiting # I see this in some demos, but nthing seeme to makes the scene render completly blenderScene.update(1)
def LoadAlchemediaScene(filename): ''' Load a newton Alchemedia file ''' scene = pyScene.pyScene() scene.Load(filename) root = scene.GetRoot() # get the active blender scene blenderScene = bpy.data.scenes.active # load all unique textures (path, name) = os.path.split(filename) childLink = scene.GetFirstChildLink(root) while childLink != None: textureNode = scene.GetNodeFromLink(childLink) if scene.IsTextureNode(textureNode) == True: texture = CreateBlenderTextureFromNode(scene, textureNode, blenderScene, path) # add a map key for asigning faces sourceTexture = pyScene.pyTexture(scene, textureNode) g_textureMap[sourceTexture.GetId()] = texture childLink = scene.GetNextChildLink(root, childLink) # import all objects into a blender scene myChidren = [] childLink = scene.GetFirstChildLink(root) while childLink != None: node = scene.GetNodeFromLink(childLink) if scene.IsSceneNode(node) == True: LoadNodesScene(scene, node, blenderScene, myChidren) childLink = scene.GetNextChildLink(root, childLink) # make sure everthing is updated before exiting # I see this in some demos, but nthing seeme to makes the scene render completly blenderScene.update(1)
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 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)