Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'''
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
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
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
for index, material in enumerate(scene.materials):
print(" MATERIAL (id:" + str(index+1) + ")")
for key, value in material.properties.items():
print(" %s: %s" % (key, value))
print
print("TEXTURES:")
for index, texture in enumerate(scene.textures):
print(" TEXTURE" + str(index+1))
print(" width:" + str(texture.width))
print(" height:" + str(texture.height))
print(" hint:" + str(texture.achformathint))
print(" data (size):" + str(len(texture.data)))
# Finally release the model
pyassimp.release(scene)
if id(m) not in mesh_id:
continue
# create kwargs for graph.update
edge = {'frame_from': node_name,
'frame_to': str(id(m)) + str(node_name),
'matrix': np.eye(4),
'geometry': mesh_id[id(m)]}
transforms.append(edge)
# add any children to the queue to be visited
for child in node.children:
queue.appendleft((node_name, child))
# release the loaded scene
pyassimp.release(scene)
# if we opened the file in this function close it
if opened:
file_obj.close()
# create kwargs for trimesh.exchange.load.load_kwargs
result = {'class': 'Scene',
'geometry': meshes,
'graph': transforms,
'base_frame': 'world'}
return result
def __del__(self):
if self.rootAssimpNodeNode:
assimp.release(self.rootAssimpNodeNode)
scene = self.scene
#log some statistics
logger.info(" meshes: %d" % len(scene.meshes))
logger.info(" total faces: %d" % sum([len(mesh.faces) for mesh in scene.meshes]))
logger.info(" materials: %d" % len(scene.materials))
self.bb_min, self.bb_max = get_bounding_box(self.scene)
logger.info(" bounding box:" + str(self.bb_min) + " - " + str(self.bb_max))
self.scene_center = [(a + b) / 2. for a, b in zip(self.bb_min, self.bb_max)]
for index, mesh in enumerate(scene.meshes):
self.prepare_gl_buffers(mesh)
# Finally release the model
pyassimp.release(scene)
logger.info("Ready for 3D rendering!")