def makeMesh(self, name, pose, filename): scene = pyassimp.load(filename) if not scene.meshes: rospy.logerr('Unable to load mesh') return mesh = Mesh() for face in scene.meshes[0].faces: triangle = MeshTriangle() if len(face.indices) == 3: triangle.vertex_indices = [face.indices[0], face.indices[1], face.indices[2]] mesh.triangles.append(triangle) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] point.y = vertex[1] point.z = vertex[2] mesh.vertices.append(point) pyassimp.release(scene) o = CollisionObject() o.header.stamp = rospy.Time.now() o.header.frame_id = self._fixed_frame o.id = name o.meshes.append(mesh) o.mesh_poses.append(pose) o.operation = o.ADD return o
def makeMesh(self, name, pose, filename): if not use_pyassimp: rospy.logerr( 'pyassimp is broken on your platform, cannot load meshes') return scene = pyassimp.load(filename) if not scene.meshes: rospy.logerr('Unable to load mesh') return mesh = Mesh() for face in scene.meshes[0].faces: triangle = MeshTriangle() if len(face.indices) == 3: triangle.vertex_indices = [ face.indices[0], face.indices[1], face.indices[2] ] mesh.triangles.append(triangle) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] point.y = vertex[1] point.z = vertex[2] mesh.vertices.append(point) pyassimp.release(scene) o = CollisionObject() o.header.stamp = rospy.Time.now() o.header.frame_id = self._fixed_frame o.id = name o.meshes.append(mesh) o.mesh_poses.append(pose) o.operation = o.ADD return o
def __make_mesh(self, name, pose, filename): co = CollisionObject() scene = pyassimp.load(filename) if not scene.meshes: raise MoveItCommanderException("There are no meshes in the file") co.operation = CollisionObject.ADD co.id = name co.header = pose.header mesh = Mesh() for face in scene.meshes[0].faces: triangle = MeshTriangle() if len(face.indices) == 3: triangle.vertex_indices = [face.indices[0], face.indices[1], face.indices[2]] mesh.triangles.append(triangle) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] point.y = vertex[1] point.z = vertex[2] mesh.vertices.append(point) co.meshes = [mesh] co.mesh_poses = [pose.pose] pyassimp.release(scene) return co
def __make_mesh(self, name, pose, filename, scale = (1, 1, 1)): co = CollisionObject() scene = pyassimp.load(filename) if not scene.meshes: raise MoveItCommanderException("There are no meshes in the file") co.operation = CollisionObject.ADD co.id = name co.header = pose.header mesh = Mesh() for face in scene.meshes[0].faces: triangle = MeshTriangle() if len(face.indices) == 3: triangle.vertex_indices = [face.indices[0], face.indices[1], face.indices[2]] mesh.triangles.append(triangle) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0]*scale[0] point.y = vertex[1]*scale[1] point.z = vertex[2]*scale[2] mesh.vertices.append(point) co.meshes = [mesh] co.mesh_poses = [pose.pose] pyassimp.release(scene) return co
def importFromFileToMesh(path): scene = pyassimp.load(path) if len(scene.meshes) == 0: print "assimp_importer: found no mesh, abort" return None elif len(scene.meshes) > 1: print "assimp_importer: found %s meshes, abort", len(scene.meshes) return None else: faces = scene.meshes[0].faces vertices = scene.meshes[0].vertices result = ROSMeshTriangleMesh() for vertex in vertices: ros_point = ROSPoint() ros_point.x = vertex[0] ros_point.y = vertex[1] ros_point.z = vertex[2] result.vertices.append(ros_point) for face in faces: if len(face.indices) != 3: print "WARNING: Triangle contained $s instead of 3 indicies", len(face.indices) ros_triangle = ROSTriangleIndices() ros_triangle.vertex_indices = [face.indices[0], face.indices[1], face.indices[2]] result.triangles.append(ros_triangle) pyassimp.release(scene) return result
def _make_mesh(self, name, path, scale, pos, quat=None, frame_id=None): if not use_pyassimp: raise RuntimeError('pyassimp is broken, cannot load meshes') scene = pyassimp.load(path) if not scene.meshes: pyassimp.release(scene) raise RuntimeError('Unable to load mesh "{}"'.format(path)) mesh = Mesh() for face in scene.meshes[0].faces: triangle = MeshTriangle() if isinstance(face, np.ndarray): indices = face.tolist() else: indices = face.indices if len(indices) == 3: triangle.vertex_indices = list(indices) mesh.triangles.append(triangle) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] * scale[0] point.y = vertex[1] * scale[1] point.z = vertex[2] * scale[2] mesh.vertices.append(point) pyassimp.release(scene) return self._add_command(name, self._make_pose(pos, quat), mesh=mesh, frame_id=frame_id)
def main(): scene = pyassimp.load(options.filename, aiProcessFlags) for index, mesh in enumerate(scene.meshes): if(options.bJSON): writeMeshJSON(mesh, scene.materials[mesh.mMaterialIndex], scene.mRootNode[0].mTransformation) else: writeMesh(mesh) # Finally release the model pyassimp.release(scene)
def main(filename=None): filename = filename or DEFAULT_MODEL scene = pyassimp.load(filename) #the model we load print "MODEL:", filename print #write some statistics print "SCENE:" print " meshes:", len(scene.meshes) print " materials:", len(scene.materials) print " textures:", len(scene.textures) print print "MESHES:" for index, mesh in enumerate(scene.meshes): print " MESH", index + 1 print " material:", mesh.mMaterialIndex + 1 print " vertices:", len(mesh.vertices) print " first 3 verts:", mesh.vertices[:3] #if len(mesh.normals): # print " first 3 normals:", mesh.normals[:3] print " colors:", len(mesh.colors) tc = mesh.texcoords print " texture-coords 1:", len(tc[0]), "first3:", tc[0][:3] print " texture-coords 2:", len(tc[1]), "first3:", tc[1][:3] print " texture-coords 3:", len(tc[2]), "first3:", tc[2][:3] print " texture-coords 4:", len(tc[3]), "first3:", tc[3][:3] print " uv-component-count:", len(mesh.mNumUVComponents) print " faces:", len( mesh.faces), "first:", [f.indices for f in mesh.faces[:3]] print " bones:", len( mesh.bones), "first:", [b.mName for b in mesh.bones[:3]] print print "MATERIALS:" for index, material in enumerate(scene.materials): print " MATERIAL", index + 1 properties = pyassimp.GetMaterialProperties(material) for key in properties: print " %s: %s" % (key, properties[key]) print print "TEXTURES:" for index, texture in enumerate(scene.textures): print " TEXTURE", index + 1 print " width:", texture.mWidth print " height:", texture.mHeight print " hint:", texture.achFormatHint print " data (size):", texture.mWidth * texture.mHeight # Finally release the model pyassimp.release(scene)
def main(filename=None): filename = filename or DEFAULT_MODEL scene = pyassimp.load(filename) #the model we load print("MODEL:", filename) print() #write some statistics print("SCENE:") print(" meshes:", len(scene.meshes)) print(" materials:", len(scene.materials)) print(" textures:", len(scene.textures)) print() print("MESHES:") for index, mesh in enumerate(scene.meshes): print(" MESH", index+1) print(" material:", mesh.mMaterialIndex+1) print(" vertices:", len(mesh.vertices)) print(" first 3 verts:", mesh.vertices[:3]) #if len(mesh.normals): # print " first 3 normals:", mesh.normals[:3] print(" colors:", len(mesh.colors)) tc = mesh.texcoords print(" texture-coords 1:", len(tc[0]), "first3:", tc[0][:3]) print(" texture-coords 2:", len(tc[1]), "first3:", tc[1][:3]) print(" texture-coords 3:", len(tc[2]), "first3:", tc[2][:3]) print(" texture-coords 4:", len(tc[3]), "first3:", tc[3][:3]) print(" uv-component-count:", len(mesh.mNumUVComponents)) print(" faces:", len(mesh.faces), "first:", [f.indices for f in mesh.faces[:3]]) print(" bones:", len(mesh.bones), "first:", [b.mName for b in mesh.bones[:3]]) print() print("MATERIALS:") for index, material in enumerate(scene.materials): print(" MATERIAL", index+1) properties = pyassimp.GetMaterialProperties(material) for key in properties: print(" %s: %s" % (key, properties[key])) print() print("TEXTURES:") for index, texture in enumerate(scene.textures): print(" TEXTURE", index+1) print(" width:", texture.mWidth) print(" height:", texture.mHeight) print(" hint:", texture.achFormatHint) print(" data (size):", texture.mWidth*texture.mHeight) # Finally release the model pyassimp.release(scene)
def Load(fileName): Model = os.path.join(os.path.dirname(__file__), fileName) scene = pyassimp.load(Model) newModel = Mesh() for index, mesh in enumerate(scene.meshes): #On suppose qu'il n'y a qu'un mesh par fichier, la fonction ne renvoyant qu'un mesh for index, vertices in enumerate(mesh.vertices): newModel.m_vertices.insert(index, vector3(vertices[0],vertices[1],vertices[2])) for index, face in enumerate(mesh.faces): newModel.m_faces.insert(index, vector3(face.indices[0],face.indices[1],face.indices[2])) for index, normal in enumerate(mesh.normals): newModel.m_normals.insert(index, vector3(normal[0],normal[1],normal[2])) pyassimp.release(scene) return newModel
def main(): scene = pyassimp.load(MODEL) # the model we load print "MODEL:", MODEL print # write some statistics print "SCENE:" print " meshes:", len(scene.meshes) print " materials:", len(scene.materials) print " textures:", len(scene.textures) print print "MESHES:" for index, mesh in enumerate(scene.meshes): print " MESH", index + 1 print " material:", mesh.mMaterialIndex + 1 print " vertices:", len(mesh.vertices) print " first:", mesh.vertices[:3] print " colors:", len(mesh.colors) tc = mesh.texcoords print " texture-coords 1:", len(tc[0]), "first:", tc[0][:3] print " texture-coords 2:", len(tc[1]), "first:", tc[1][:3] print " texture-coords 3:", len(tc[2]), "first:", tc[2][:3] print " texture-coords 4:", len(tc[3]), "first:", tc[3][:3] print " uv-component-count:", len(mesh.mNumUVComponents) print " faces:", len(mesh.faces), "first:", [f.indices for f in mesh.faces[:3]] print " bones:", len(mesh.bones), "first:", [b.mName for b in mesh.bones[:3]] print print "MATERIALS:" for index, material in enumerate(scene.materials): print " MATERIAL", index + 1 properties = pyassimp.GetMaterialProperties(material) for key in properties: print " %s: %s" % (key, properties[key]) print print "TEXTURES:" for index, texture in enumerate(scene.textures): print " TEXTURE", index + 1 print " width:", texture.mWidth print " height:", texture.mHeight print " hint:", texture.achFormatHint print " data (size):", texture.mWidth * texture.mHeight # Finally release the model pyassimp.release(scene)
def __make_mesh(name, pose, filename, scale=(1, 1, 1)): co = CollisionObject() if pyassimp is False: raise MoveItCommanderException( "Pyassimp needs patch https://launchpadlibrarian.net/319496602/patchPyassim.txt" ) scene = pyassimp.load(filename) if not scene.meshes or len(scene.meshes) == 0: raise MoveItCommanderException("There are no meshes in the file") if len(scene.meshes[0].faces) == 0: raise MoveItCommanderException("There are no faces in the mesh") co.operation = CollisionObject.ADD co.id = name co.header = pose.header mesh = Mesh() first_face = scene.meshes[0].faces[0] if hasattr(first_face, "__len__"): for face in scene.meshes[0].faces: if len(face) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face[0], face[1], face[2]] mesh.triangles.append(triangle) elif hasattr(first_face, "indices"): for face in scene.meshes[0].faces: if len(face.indices) == 3: triangle = MeshTriangle() triangle.vertex_indices = [ face.indices[0], face.indices[1], face.indices[2], ] mesh.triangles.append(triangle) else: raise MoveItCommanderException( "Unable to build triangles from mesh due to mesh object structure" ) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] * scale[0] point.y = vertex[1] * scale[1] point.z = vertex[2] * scale[2] mesh.vertices.append(point) co.meshes = [mesh] co.mesh_poses = [pose.pose] pyassimp.release(scene) return co
def make_mesh(self, co, pose, filename, scale=(1.0, 1.0, 1.0)): try: scene = pyassimp.load(filename) except AssimpError as e: rospy.logerr("Assimp error: %s", e) return False if not scene.meshes or len(scene.meshes) == 0: raise MoveItCommanderException("There are no meshes in the file") if len(scene.meshes[0].faces) == 0: raise MoveItCommanderException("There are no faces in the mesh") mesh = Mesh() first_face = scene.meshes[0].faces[0] if hasattr(first_face, '__len__'): for face in scene.meshes[0].faces: if len(face) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face[0], face[1], face[2]] mesh.triangles.append(triangle) elif hasattr(first_face, 'indices'): for face in scene.meshes[0].faces: if len(face.indices) == 3: triangle = MeshTriangle() triangle.vertex_indices = [ face.indices[0], face.indices[1], face.indices[2] ] mesh.triangles.append(triangle) else: raise MoveItCommanderException( "Unable to build triangles from mesh due to mesh object structure" ) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] * scale[0] point.y = vertex[1] * scale[1] point.z = vertex[2] * scale[2] mesh.vertices.append(point) if not isinstance(co.meshes, list): co.meshes = [] co.meshes += [mesh] if not isinstance(co.mesh_poses, list): co.mesh_poses = [] co.mesh_poses += [pose.pose] pyassimp.release(scene) return co
def make_mesh(co, name, pose, filename, scale=(1, 1, 1)): #print("make_mesh(name=%s filename=%s)" % (name, filename)) scene = pyassimp.load(filename) if not scene.meshes or len(scene.meshes) == 0: raise MoveItCommanderException("There are no meshes in the file") if len(scene.meshes[0].faces) == 0: raise MoveItCommanderException("There are no faces in the mesh") co.operation = CollisionObject.ADD co.id = name co.header = pose.header mesh = Mesh() first_face = scene.meshes[0].faces[0] if hasattr(first_face, '__len__'): for face in scene.meshes[0].faces: if len(face) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face[0], face[1], face[2]] mesh.triangles.append(triangle) elif hasattr(first_face, 'indices'): for face in scene.meshes[0].faces: if len(face.indices) == 3: triangle = MeshTriangle() triangle.vertex_indices = [ face.indices[0], face.indices[1], face.indices[2] ] mesh.triangles.append(triangle) else: raise MoveItCommanderException( "Unable to build triangles from mesh due to mesh object structure") for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] * scale[0] point.y = vertex[1] * scale[1] point.z = vertex[2] * scale[2] mesh.vertices.append(point) if not isinstance(co.meshes, list): co.meshes = [] co.meshes += [mesh] if not isinstance(co.mesh_poses, list): co.mesh_poses = [] co.mesh_poses += [pose.pose] pyassimp.release(scene) return co
def make_mesh(co, name, pose, filename, scale = (1, 1, 1)): #print("make_mesh(name=%s filename=%s)" % (name, filename)) scene = pyassimp.load(filename) if not scene.meshes or len(scene.meshes) == 0: raise MoveItCommanderException("There are no meshes in the file") if len(scene.meshes[0].faces) == 0: raise MoveItCommanderException("There are no faces in the mesh") co.operation = CollisionObject.ADD co.id = name co.header = pose.header mesh = Mesh() first_face = scene.meshes[0].faces[0] if hasattr(first_face, '__len__'): for face in scene.meshes[0].faces: if len(face) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face[0], face[1], face[2]] mesh.triangles.append(triangle) elif hasattr(first_face, 'indices'): for face in scene.meshes[0].faces: if len(face.indices) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face.indices[0], face.indices[1], face.indices[2]] mesh.triangles.append(triangle) else: raise MoveItCommanderException("Unable to build triangles from mesh due to mesh object structure") for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0]*scale[0] point.y = vertex[1]*scale[1] point.z = vertex[2]*scale[2] mesh.vertices.append(point) if not isinstance(co.meshes, list): co.meshes = [] co.meshes += [mesh] if not isinstance(co.mesh_poses, list): co.mesh_poses = [] co.mesh_poses += [pose.pose] pyassimp.release(scene) return co
def create_mesh(name, pose, filename, scale=(1, 1, 1), frame_id='/world_frame'): co = CollisionObject() scene = pyassimp.load(str(exo.Tools.parsePath(filename))) if not scene.meshes or len(scene.meshes) == 0: raise Exception("There are no meshes in the file") if len(scene.meshes[0].faces) == 0: raise Exception("There are no faces in the mesh") co.operation = CollisionObject.ADD co.id = name co.header.frame_id = frame_id mesh = Mesh() first_face = scene.meshes[0].faces[0] if hasattr(first_face, '__len__'): for face in scene.meshes[0].faces: if len(face) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face[0], face[1], face[2]] mesh.triangles.append(triangle) elif hasattr(first_face, 'indices'): for face in scene.meshes[0].faces: if len(face.indices) == 3: triangle = MeshTriangle() triangle.vertex_indices = [ face.indices[0], face.indices[1], face.indices[2] ] mesh.triangles.append(triangle) else: raise Exception("Unable to build triangles from mesh") for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] * scale[0] point.y = vertex[1] * scale[1] point.z = vertex[2] * scale[2] mesh.vertices.append(point) co.meshes = [mesh] co.mesh_poses = [pose] pyassimp.release(scene) return co
def makeMesh(self, name, ps, filename): """ Make a mesh collision object :param name: Name of the object :param ps: A pose stamped object pose message :param filename: The mesh file to load """ if not use_pyassimp: rospy.logerr("pyassimp is broken on your platform, cannot load meshes") return scene = pyassimp.load(filename) if not scene.meshes: rospy.logerr("Unable to load mesh") return mesh = Mesh() for face in scene.meshes[0].faces: triangle = MeshTriangle() if len(face.indices) == 3: triangle.vertex_indices = [ face.indices[0], face.indices[1], face.indices[2], ] mesh.triangles.append(triangle) for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] point.y = vertex[1] point.z = vertex[2] mesh.vertices.append(point) pyassimp.release(scene) o = CollisionObject() o.header.stamp = rospy.Time.now() o.header.frame_id = ps.header.frame_id o.id = name o.meshes.append(mesh) o.mesh_poses.append(ps.pose) o.operation = o.ADD return o
def __make_mesh(name, pose, filename, scale=(1, 1, 1)): co = CollisionObject() if pyassimp is False: raise MoveItCommanderException( "Pyassimp needs patch https://launchpadlibrarian.net/319496602/patchPyassim.txt") scene = pyassimp.load(filename) if not scene.meshes or len(scene.meshes) == 0: raise MoveItCommanderException("There are no meshes in the file") if len(scene.meshes[0].faces) == 0: raise MoveItCommanderException("There are no faces in the mesh") co.operation = CollisionObject.ADD co.id = name co.header = pose.header mesh = Mesh() first_face = scene.meshes[0].faces[0] if hasattr(first_face, '__len__'): for face in scene.meshes[0].faces: if len(face) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face[0], face[1], face[2]] mesh.triangles.append(triangle) elif hasattr(first_face, 'indices'): for face in scene.meshes[0].faces: if len(face.indices) == 3: triangle = MeshTriangle() triangle.vertex_indices = [face.indices[0], face.indices[1], face.indices[2]] mesh.triangles.append(triangle) else: raise MoveItCommanderException("Unable to build triangles from mesh due to mesh object structure") for vertex in scene.meshes[0].vertices: point = Point() point.x = vertex[0] * scale[0] point.y = vertex[1] * scale[1] point.z = vertex[2] * scale[2] mesh.vertices.append(point) co.meshes = [mesh] co.mesh_poses = [pose.pose] pyassimp.release(scene) return co
def importFromFileToTIN(path): scene = pyassimp.load(path) results = [] for index, mesh in enumerate(scene.meshes): prefix = 'TIN(' postfix = ')' infix = '' triangles = mesh.faces vertices = mesh.vertices triangle_strings = [] for triangle in triangles: indices = triangle.indices triangle_strings.append('((%f %f %f, %f %f %f, %f %f %f, %f %f %f))' \ % (vertices[indices[0]].x, vertices[indices[0]].y, vertices[indices[0]].z, \ vertices[indices[1]].x, vertices[indices[1]].y, vertices[indices[1]].z, \ vertices[indices[2]].x, vertices[indices[2]].y, vertices[indices[2]].z, \ vertices[indices[0]].x, vertices[indices[0]].y, vertices[indices[0]].z)) infix = ",".join(triangle_strings) tin_string = prefix + infix + postfix results.append(tin_string) pyassimp.release(scene) return results
def __del__(self): pyassimp.release(self.model)
# normals if any(mesh.normals): normals = attribute(doc, 'normal', mesh.normals) meshElement.appendChild(normals) # texture coordinates for texIndex, texCoords in enumerate(mesh.texcoords): if any(texCoords): name = 'texture[%d]' % texIndex tElement = attribute(doc, name, texCoords) meshElement.appendChild(tElement) # faces if any(mesh.faces): faces = doc.createElement('element'); for faceIndex, face in enumerate(mesh.faces): typename = 'int' + str(face.mNumIndices) faceElement = doc.createElement(typename) for i in range(0, face.mNumIndices): faceElement.setAttribute(intAttrName[i], str(face.indices[i])) faces.appendChild(faceElement) meshElement.appendChild(faces) obj.appendChild(meshElement) # [todo] materials # [todo] bones doc.appendChild(obj) print doc.toprettyxml() pyassimp.release(scene)