def swap_yz(file_in, file_out=None, log=None): """ Swap a mesh "Up" direction betwenn "Y" and "Z" axes. Requires metadata to know what the current "Up" direction is. """ fprefix, scale_meta, up_meta, fext = filename.check_metadata(file_in) if up_meta.upper() == 'Y': # Y to Z: rotate 90d about X up_meta = 'Z' angle = 90.0 elif up_meta.upper() == 'Z': # Z to Y: rotate -90d about X up_meta = 'Y' angle = -90.0 if file_out is None: file_out = '%s(%s%s).%s' % (fprefix, scale_meta, up_meta, fext) script = 'TEMP3D_swapYZ.mlx' _, texture_files_unique, _ = mlx.find_texture_files(fbasename=file_in, log=log) texture = bool(len(texture_files_unique) > 0) output_mask = mlx.default_output_mask(file_out=file_out, texture=texture) mlx.begin(script, file_in=file_in) mlx.transform.rotate(script, axis='x', angle=angle) mlx.end(script) mlx.run(log=log, file_in=file_in, file_out=file_out, script=script, output_mask=output_mask) return file_out
def main(): """Run main script""" script1 = 'TEMP3D_tube1.mlx' mesh1 = 'TEMP3D_tube1.ply' script2 = 'TEMP3D_tube2.mlx' mesh2 = 'TEMP3D_tube2.ply' input3 = [mesh1, mesh2] script3 = 'TEMP3D_join.mlx' final_output = 'ranbow_tubes.ply' mlx.begin(script1) mlx.create.tube_hires(script1, height=165, radius1=1, radius2=0.7, cir_segments=32, rad_segments=1, height_segments=165, center=True) mlx.transform.rotate(script1, 'y', -90) mlx.transform.wrap2cylinder(script1, radius=3, pitch=8) mlx.end(script1) mlx.run(script=script1, file_out=mesh1) mlx.begin(script2) mlx.create.tube_hires(script2, height=300, radius1=1.5, radius2=1, cir_segments=32, rad_segments=1, height_segments=300, center=True) mlx.transform.rotate(script2, 'y', -90) mlx.transform.wrap2cylinder(script2, radius=6, pitch=-8) mlx.end(script2) mlx.run(script=script2, file_out=mesh2) mlx.begin(script3, file_in=input3) mlx.layers.join(script3) mlx.vert_color.cyclic_rainbow(script3, freq=0.8) mlx.transform.rotate(script3, 'y', -90) mlx.transform.wrap2cylinder(script3, radius=20, pitch=0) mlx.end(script3) mlx.run(script=script3, file_out=final_output, file_in=input3) wait = eval( input( '\nPress ENTER to delete TEMP3D* files, or type "n" to keep them: ' )) if wait == '': mlx.util.delete_all('TEMP3D*')
def create_mesh(Meshlab_adr,input_adr,output_adr,filter_adr): meshlabserver_path = Meshlab_adr os.environ['PATH'] = meshlabserver_path + os.pathsep + os.environ['PATH'] mlx.run(file_in=input_adr, file_out=output_adr, output_mask='-m fc vc vn sa', script=filter_adr) return
def main(): """Run main script""" # segments = number of segments to use for circles segments = 50 # star_points = number of points (or sides) of the star star_points = 5 # star_radius = radius of circle circumscribing the star star_radius = 2 # ring_thickness = thickness of the colored rings ring_thickness = 1 # sphere_radius = radius of sphere the shield will be deformed to sphere_radius = 2 * (star_radius + 3 * ring_thickness) # Star calculations: # Visually approximate a star by using multiple diamonds (i.e. scaled # squares) which overlap in the center. For the star calculations, # consider a central polygon with triangles attached to the edges, all # circumscribed by a circle. # polygon_radius = distance from center of circle to polygon edge midpoint polygon_radius = star_radius / \ (1 + math.tan(math.radians(180 / star_points)) / math.tan(math.radians(90 / star_points))) # width = 1/2 width of polygon edge/outer triangle bottom width = polygon_radius * math.tan(math.radians(180 / star_points)) # height = height of outer triangle height = width / math.tan(math.radians(90 / star_points)) # This function always comes first and starts the mlx script mlx.begin() # Create the colored front of the shield using several concentric # annuluses; combine them together and subdivide so we have more vertices # to give a smoother deformation later. mlx.create.annulus(radius=star_radius, cir_segments=segments, color='blue') mlx.create.annulus( radius1=star_radius + ring_thickness, radius2=star_radius, cir_segments=segments, color='red') mlx.create.annulus( radius1=star_radius + 2 * ring_thickness, radius2=star_radius + ring_thickness, cir_segments=segments, color='white') mlx.create.annulus( radius1=star_radius + 3 * ring_thickness, radius2=star_radius + 2 * ring_thickness, cir_segments=segments, color='red') mlx.layers.join() mlx.subdivide.midpoint(iterations=2) # Create the inside surface of the shield & translate down slightly so it # doesn't overlap the front. mlx.create.annulus( radius1=star_radius + 3 * ring_thickness, cir_segments=segments, color='silver') mlx.transform.rotate(axis='y', angle=180) mlx.transform.translate(value=[0, 0, -0.005]) mlx.subdivide.midpoint(iterations=4) # Create a diamond for the center star. First create a plane, specifying # extra vertices to support the final deformation. The length from the # center of the plane to the corners should be 1 for ease of scaling, so # we use a side length of sqrt(2) (thanks Pythagoras!). Rotate the plane # by 45 degrees and scale it to stretch it out per the calculations above, # then translate it into place (including moving it up in z slightly so # that it doesn't overlap the shield front). mlx.create.grid( size=math.sqrt(2), x_segments=10, y_segments=10, center=True, color='white') mlx.transform.rotate(axis='z', angle=45) mlx.transform.scale(value=[width, height, 1]) mlx.transform.translate(value=[0, polygon_radius, 0.001]) # Duplicate the diamond and rotate the duplicates around, generating the # star. for _ in range(1, star_points): mlx.layers.duplicate() mlx.transform.rotate(axis='z', angle=360 / star_points) # Combine everything together and deform using a spherical function. mlx.layers.join() mlx.transform.function( z_func='sqrt(%s-x^2-y^2)-%s+z' % (sphere_radius**2, sphere_radius)) # This function always comes last and ends the mlx script mlx.end() # Run the script using meshlabserver and generate the model mlx.run(file_out="Cap's_shield.ply") wait = eval(input( '\nPress ENTER to delete TEMP3D* files, or type "n" to keep them: ')) if wait == '': mlx.util.delete_all('TEMP3D*')
def hollow_volume(fullpath_in, fullpath_out, log=None, offset=-3, solid_resolution=128, mesh_resolution=128, del_small_parts=False, small_part_ratio=0.1): """ Create hollow (offset) volume using MeshMixer Make Solid approximates your object with small cubes (voxels). This approximation actually happens twice. First we voxelize the shape using solid_resolution as the sampling rate. Then we use a second set of voxels to create a mesh of the first voxel approximation; mesh_resolution is the sampling rate of this second voxelization. These sampling rates can be the same, but they do not have to be. -- MeshMixer manual WARNING: hard coded output mask, must be updated when MeshLab version is """ mix_script = 'TEMP3D_mix_hollow.py' write_mmpy.begin(mix_script) obj_a = write_mmpy.import_mesh( 'obj_a', mix_script, file_in=fullpath_in) obj_b = write_mmpy.make_solid( 'obj_b', mix_script, mesh_object=obj_a, offset=offset, solid_type=2, solid_resolution=solid_resolution, mesh_resolution=mesh_resolution) write_mmpy.export_mesh( None, mix_script, mesh_object=obj_b, file_out=fullpath_out) write_mmpy.end(mix_script) write_mmpy.run(mix_script, log) # When hollowing Kylechessking_flat(-11Z).obj it was found that Blender # could not open the hollow volume; error was: # ValueError: could not convert string to float: b'-1.#IND' # The file had vertex normals in sci notation, i.e. -2.086671657e-006 # however, did not verify if this was the issue. # MeshLab can open them fine, so we will re-save with MeshLab. Even if we # determine the root cause, MeshMixer doesn't have many export options, # so this is still probably the easiest way to fix. # MeshMixer may also create multiple volumes, so we provide the option to delete small components # Convert to binary stl and drop colors with MeshLab # file_out = os.path.basename(fullpath_out) output_mask = mlx.default_output_mask(file_out=file_out, texture=False) if del_small_parts: ml_script = 'TEMP3D_hollow.mlx' mlx.begin(script=ml_script, file_in=file_out) mlx.delete.small_parts(script=ml_script, ratio=small_part_ratio) mlx.end(script=ml_script) else: ml_script = None mlx.run(log=log, file_in=file_out, file_out=file_out, script=ml_script, output_mask=output_mask) return None
def hollow_volume(fullpath_in, fullpath_out, log=None, offset=-3, solid_resolution=128, mesh_resolution=128, del_small_parts=False, small_part_ratio=0.1): """ Create hollow (offset) volume using MeshMixer Make Solid approximates your object with small cubes (voxels). This approximation actually happens twice. First we voxelize the shape using solid_resolution as the sampling rate. Then we use a second set of voxels to create a mesh of the first voxel approximation; mesh_resolution is the sampling rate of this second voxelization. These sampling rates can be the same, but they do not have to be. -- MeshMixer manual WARNING: hard coded output mask, must be updated when MeshLab version is """ mix_script = 'TEMP3D_mix_hollow.py' write_mmpy.begin(mix_script) obj_a = write_mmpy.import_mesh('obj_a', mix_script, file_in=fullpath_in) obj_b = write_mmpy.make_solid('obj_b', mix_script, mesh_object=obj_a, offset=offset, solid_type=2, solid_resolution=solid_resolution, mesh_resolution=mesh_resolution) write_mmpy.export_mesh(None, mix_script, mesh_object=obj_b, file_out=fullpath_out) write_mmpy.end(mix_script) write_mmpy.run(mix_script, log) # When hollowing Kylechessking_flat(-11Z).obj it was found that Blender # could not open the hollow volume; error was: # ValueError: could not convert string to float: b'-1.#IND' # The file had vertex normals in sci notation, i.e. -2.086671657e-006 # however, did not verify if this was the issue. # MeshLab can open them fine, so we will re-save with MeshLab. Even if we # determine the root cause, MeshMixer doesn't have many export options, # so this is still probably the easiest way to fix. # MeshMixer may also create multiple volumes, so we provide the option to delete small components # Convert to binary stl and drop colors with MeshLab # file_out = os.path.basename(fullpath_out) output_mask = mlx.default_output_mask(file_out=file_out, texture=False) if del_small_parts: ml_script = 'TEMP3D_hollow.mlx' mlx.begin(script=ml_script, file_in=file_out) mlx.delete.small_parts(script=ml_script, ratio=small_part_ratio) mlx.end(script=ml_script) else: ml_script = None mlx.run(log=log, file_in=file_out, file_out=file_out, script=ml_script, output_mask=output_mask) return None