Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, filename):
self.model = pyassimp.load(filename)
self.vertices = numpy.array(flatten(self.model.meshes[0].vertices), numpy.float32)
self.make_indices()
# NOTE: this drops the W from UVW-coordinates.
self.texcoords = numpy.array([item for sublist in self.model.meshes[0].texcoords[0] for item in sublist[:2]], numpy.float32)
self.normals = numpy.array(flatten(self.model.meshes[0].normals), numpy.float32)
def load_model(filename):
return pyassimp.load(filename)
def __init__(self, filename):
self.model = pyassimp.load(filename)
self.vertices = numpy.array(flatten(self.model.meshes[0].vertices), numpy.float32)
self.make_indices()
# NOTE: this drops the W from UVW-coordinates.
self.texcoords = numpy.array([item for sublist in self.model.meshes[0].texcoords[0] for item in sublist[:2]], numpy.float32)
self.normals = numpy.array(flatten(self.model.meshes[0].normals), numpy.float32)
def __del__(self):
pyassimp.release(self.model)
def __del__(self):
pyassimp.release(self.model)
def load_model(filename):
return pyassimp.load(filename)
Also, you need a very recent version of PyAssimp for this function to work
(the commit was merged into the assimp github master on roughly 9/5/2014)
'''
def LPMesh_to_Trimesh(lp):
return Trimesh(vertices = lp.vertices,
vertex_normals = lp.normals,
faces = lp.faces,
vertex_colors = lp.colors)
if not hasattr(file_obj, 'read'):
# if there is no read attribute, we assume we've been passed a file name
file_type = (str(file_obj).split('.')[-1]).lower()
file_obj = open(file_obj, 'rb')
scene = pyassimp.load(file_obj, file_type=file_type)
meshes = list(map(LPMesh_to_Trimesh, scene.meshes))
pyassimp.release(scene)
if len(meshes) == 1:
return meshes[0]
return meshes
'''
def LPMesh_to_Trimesh(lp):
return Trimesh(vertices = lp.vertices,
vertex_normals = lp.normals,
faces = lp.faces,
vertex_colors = lp.colors)
if not hasattr(file_obj, 'read'):
# if there is no read attribute, we assume we've been passed a file name
file_type = (str(file_obj).split('.')[-1]).lower()
file_obj = open(file_obj, 'rb')
scene = pyassimp.load(file_obj, file_type=file_type)
meshes = list(map(LPMesh_to_Trimesh, scene.meshes))
pyassimp.release(scene)
if len(meshes) == 1:
return meshes[0]
return meshes
# create filename with the correction extension (STL)
basename = os.path.basename(filename)
basename_without_extension = ''.join(basename.split('.')[:-1])
# dirname = os.path.dirname(filename)
# filename_without_extension = dirname + basename_without_extension
# new_filename = filename_without_extension + '.stl'
new_filename = mesh_dirname + '/' + basename_without_extension + '.stl'
# if file does not already exists, convert it
if not os.path.isfile(new_filename):
# use pyassimp to
scene = pyassimp.load(filename)
pyassimp.export(scene, new_filename, file_type='stlb')
pyassimp.release(scene)
#
# export_mesh(trimesh.load(filename), new_filename)
return new_filename
return filename
def convert_mesh(from_filename, to_filename, library='pyassimp', binary=False):
"""
Convert the given file containing the original mesh to the other specified format using the `pyassimp` library.
Args:
from_filename (str): filename of the mesh to convert.
to_filename (str): filename of the converted mesh.
library (str): library to use to convert the meshes. Select between 'pyassimp' and 'trimesh'.
binary (bool): if True, it will be in a binary format. This is only valid for some formats such as STL where
you have the ASCII version 'stl' and the binary version 'stlb'.
"""
if library == 'pyassimp':
scene = pyassimp.load(from_filename)
extension = to_filename.split('.')[-1].lower()
if binary: # for binary add 'b' as a suffix. Ex: '.stlb'
pyassimp.export(scene, to_filename, file_type=extension + 'b')
else:
pyassimp.export(scene, to_filename, file_type=extension)
pyassimp.release(scene)
elif library == 'trimesh':
export_mesh(trimesh.load(from_filename), to_filename)
else:
raise NotImplementedError("The given library '{}' is currently not supported, select between 'pyassimp' and "
"'trimesh'".format(library))