Example #1
0
    def execute(self, context):
        global fileidx
        global filelist

        #deletes all the objects
        select_all_objects()
        bpy.ops.object.delete()

        #next object
        print("-- Filename opened:")
        print("idx"+str(fileidx))
        print(filelist[fileidx])

        faces,verts = stl_utils.read_stl(filelist[fileidx])
        filename = filelist[fileidx]
        ob1 = createMesh( filename[len(filename)-14 : len(filename)], verts, [], faces)
        bpy.data.objects[find_object(filename[len(filename)-14 : len(filename)])].select = True
        bpy.context.scene.objects.active = bpy.data.objects[find_object(filename[len(filename)-14 : len(filename)])]

        if(fileidx < len(filelist)):
            fileidx = fileidx + 1
        else:
            fileidx = 0
        

        return {'FINISHED'}
def read_openscad(context, filepath, scale, parameters):
    """ Exports stl using OpenSCAD and imports it. """
    from io_mesh_stl import stl_utils
    from io_mesh_stl import blender_utils
    from mathutils import Matrix
    
    user_preferences = context.user_preferences
    addon_prefs = user_preferences.addons[__name__].preferences
    openscad_path = addon_prefs.filepath
    tempfile_path = os.path.join(os.path.dirname(filepath), TEMPNAME)

    # Export stl from OpenSCAD
    command = "\"\"%s\" -o \"%s\" \"%s\"\"" % \
        (openscad_path, tempfile_path, filepath)
    
    print("Executing command:", command)
    os.system(command)
    
    if os.path.exists(tempfile_path):
        if bpy.ops.object.mode_set.poll():
            bpy.ops.object.mode_set(mode='OBJECT')
    
        if bpy.ops.object.select_all.poll():
            bpy.ops.object.select_all(action='DESELECT')

        global_matrix = Matrix.Scale(scale, 4) # Create 4x4 scale matrix
        obj_name = os.path.basename(filepath).split('.')[0]
        tris, pts = stl_utils.read_stl(tempfile_path)
        blender_utils.create_and_link_mesh(obj_name, tris, pts, global_matrix)
        os.remove(tempfile_path)
    else:
        print("Temporary export file not found:", tempfile_path)
    
    return {'FINISHED'}
Example #3
0
def read_openscad(context, filepath, scale, parameters):
    """ Exports stl using OpenSCAD and imports it. """
    from io_mesh_stl import stl_utils
    from io_mesh_stl import blender_utils
    from mathutils import Matrix

    user_preferences = context.user_preferences
    addon_prefs = user_preferences.addons[__name__].preferences
    openscad_path = addon_prefs.filepath
    tempfile_path = os.path.join(os.path.dirname(filepath), TEMPNAME)

    # Export stl from OpenSCAD
    command = "\"\"%s\" -o \"%s\" \"%s\"\"" % \
        (openscad_path, tempfile_path, filepath)

    print("Executing command:", command)
    os.system(command)

    if os.path.exists(tempfile_path):
        if bpy.ops.object.mode_set.poll():
            bpy.ops.object.mode_set(mode='OBJECT')

        if bpy.ops.object.select_all.poll():
            bpy.ops.object.select_all(action='DESELECT')

        global_matrix = Matrix.Scale(scale, 4)  # Create 4x4 scale matrix
        obj_name = os.path.basename(filepath).split('.')[0]
        tris, pts = stl_utils.read_stl(tempfile_path)
        blender_utils.create_and_link_mesh(obj_name, tris, pts, global_matrix)
        os.remove(tempfile_path)
    else:
        print("Temporary export file not found:", tempfile_path)

    return {'FINISHED'}
Example #4
0
    def execute(self, context):
        bpy.ops.scene.mat_to_obj()
        global_matrix = axis_conversion(
            from_forward=context.scene.STL_forward,
            from_up=context.scene.STL_up,
        ).to_4x4() @ Matrix.Scale(context.scene.STL_scale, 4)
        d = os.path.expanduser('~/Desktop/S2B_Temp') + '/'
        up = 0
        new = 0
        for f in os.listdir(d):
            if f[-4:] == '.stl':
                fp = os.path.join(d, f)
                tris, tri_nors, pts = stl_utils.read_stl(fp)
                name = insert_sense(f)
                if name in context.scene.objects:
                    just_link_stl_data(bpy.data.objects[name], tris, [], pts,
                                       global_matrix)
                    bpy.ops.object.select_all(action='SELECT')
                    bpy.ops.object.shade_smooth()
                    bpy.ops.object.select_all(action='DESELECT')
                    self.report({'INFO'}, name + " updated")
                    up += 1

                else:
                    blender_utils.create_and_link_mesh(name, tris, [], pts,
                                                       global_matrix)
                    if len(bpy.data.objects[name].material_slots) < 1:
                        bpy.data.objects[name].data.materials.append(None)
                    bpy.data.objects[name].data.use_auto_smooth = True
                    bpy.ops.object.shade_smooth()
                    self.report({'INFO'}, name + " created")
                    new += 1
        self.report({'INFO'}, str(up) + ' updated, ' + str(new) + ' created.')
        return {'FINISHED'}
def read_openscad(context, filepath, scale, parameters):
    from io_mesh_stl import stl_utils
    from io_mesh_stl import blender_utils
    
    print(OPENSCAD)
    print(filepath)
    print(parameters)

    # Export stl from OpenSCAD
    old_wd = os.getcwd()
    os.chdir(OPENSCAD)
    os.system("openscad -o %s \"%s\"" % (TEMPNAME, filepath))
    
    if os.path.exists(TEMPNAME):
        if bpy.ops.object.mode_set.poll():
            bpy.ops.object.mode_set(mode='OBJECT')
    
        if bpy.ops.object.select_all.poll():
            bpy.ops.object.select_all(action='DESELECT')
    
        objName = TEMPNAME
        tris, pts = stl_utils.read_stl(TEMPNAME)
        blender_utils.create_and_link_mesh(objName, tris, pts, ((0.0, 1.0, 0.0, 0.0),(0.0, 0.0, 1.0, 0.0),(1.0, 0.0, 0.0, 0.0),(0.0, 0.0, 1.0, 0.0)))#magic values
        os.remove(TEMPNAME)
    
    os.chdir(old_wd)
    
    return {'FINISHED'}
Example #6
0
    def execute(self, context):
        global path
        global filelist
        global fileidx

        path = self.directory+"*.stl"
        filelist = glob.glob(path)
        
        #loading the first object
        #deletes all the objects
        select_all_objects()
        bpy.ops.object.delete()

        faces,verts = stl_utils.read_stl(filelist[fileidx])
        filename = filelist[fileidx]
        ob1 = createMesh( filename[len(filename)-14 : len(filename)], verts, [], faces)
        bpy.data.objects[find_object(filename[len(filename)-14 : len(filename)])].select = True
        bpy.context.scene.objects.active = bpy.data.objects[find_object(filename[len(filename)-14 : len(filename)])]

        fileidx = fileidx + 1
        
        return {'FINISHED'}
Example #7
0
    def execute(self, context):
      
      # .. todo:: Support rotation as usual in object addons?
      # crystal axes
      e1 = [1,0,0]
      e2 = [0,1,0]
      e3 = [0,0,1]
      
      if self.cell_type == 'RCD':
        # .. todo:: set to cursor location (check how addCube & co initialize it -> It is set when object_data_add(context, mesh, operator=self) is called, which means ideally a mesh should be created and then passed to that function. It should take care of rotation&co too.)
        ## get cursor location for placement
        #cursor_location3 = numpy.array(bpy.context.scene.cursor_location)
        #self.setLocation(cursor_location3)
      
        location = Vector(self.location)
        
        obj_list = []
        if self.shift_cell:
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([3/4, 1/4, 1/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([1/4, 3/4, 1/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([1/4, 1/4, 3/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([3/4, 3/4, 3/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
        else:
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([1/4, 1/4, 1/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([3/4, 3/4, 1/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([3/4, 1/4, 3/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
          obj_list.extend( add_tetra(self, location=location+self.size*Vector([1/4, 3/4, 3/4]), size=self.size/2, name='Tetra', cylinder_radius=self.radius) )
        
        common_mesh = obj_list[0].data
        for obj in obj_list[1:]:
          obj.data = common_mesh
          
        selectObjects(obj_list)
        bpy.ops.object.join()
        obj_blender = context.active_object
        obj_blender.name='RCD'
        setOrigin(obj_blender, self.location)
        bpy.ops.object.transform_apply(location=False, rotation=True, scale=False)
        
        e1 = self.size*Vector([1,0,0])
        e2 = self.size*Vector([0,1,0])
        e3 = self.size*Vector([0,0,1])
      elif self.cell_type == 'RCD111_inverse':
        if self.shift_cell:
          STLfile = os.path.join(pathlib.Path.home(), 'Development/script_inception_public/src/blender_scripts/STL-files/RCD111_inverse_shifted_centred.stl')
        else:
          STLfile = os.path.join(pathlib.Path.home(), 'Development/script_inception_public/src/blender_scripts/STL-files/RCD111_inverse.stl')
        objName = bpy.path.display_name(os.path.basename(STLfile))
        tris, tri_nors, pts = stl_utils.read_stl(STLfile)
        tri_nors = None
        axis_forward='Y'
        axis_up='Z'
        global_scale = 1
        global_matrix = axis_conversion(from_forward=axis_forward, from_up=axis_up).to_4x4() * Matrix.Scale(global_scale, 4)
        print(global_matrix)
        blender_utils.create_and_link_mesh(objName, tris, tri_nors, pts, global_matrix)
        obj_blender = context.active_object
        
        # get cursor location for placement
        obj_blender.location = numpy.array(bpy.context.scene.cursor_location)
        
        obj_bfdtd = bfdtd.RCD.RCD_HexagonalLattice()
        obj_bfdtd.setOuterRadius(self.radius)
        obj_bfdtd.setCubicUnitCellSize(self.size)
        obj_bfdtd.setShifted(self.shift_cell)
        obj_bfdtd.setUnitCellType(2)
        e1, e2, e3 = obj_bfdtd.getLatticeVectors()
        
      else:
        if 'RCD111' in self.cell_type:
          obj_bfdtd = bfdtd.RCD.RCD_HexagonalLattice()
        else:
          obj_bfdtd = bfdtd.RCD.FRD_HexagonalLattice()
        obj_bfdtd.setOuterRadius(self.radius)
        obj_bfdtd.setCubicUnitCellSize(self.size)
        obj_bfdtd.setShifted(self.shift_cell)
        if 'v1' in self.cell_type:
          obj_bfdtd.setUnitCellType(1)
        else:
          obj_bfdtd.setUnitCellType(2)

        if self.RCD111_v2_advanced:
          obj_bfdtd.fillBox(self.RCD111_v2_location, self.RCD111_v2_size)
          # obj_bfdtd.fillBox([self.RCD111_v2_Nx, self.RCD111_v2_Ny, self.RCD111_v2_Nz], [self.RCD111_v2_Nx, self.RCD111_v2_Ny, self.RCD111_v2_Nz])
          # obj_bfdtd.createRectangularArraySymmetrical(self.RCD111_v2_Nx, self.RCD111_v2_Ny, self.RCD111_v2_Nz)
          add_block(self, location3 = self.RCD111_v2_location, size3 = self.RCD111_v2_size, name='box_to_fill', wiremode=True)
        
        obj_blender = obj_bfdtd.createBlenderObject(self, context)
        e1, e2, e3 = obj_bfdtd.getLatticeVectors()
        
      obj_blender.name = self.cell_type
      if self.create_array:
        add_array_modifier(obj_blender, 'array-modifier-X', self.array_size_X, e1)
        add_array_modifier(obj_blender, 'array-modifier-Y', self.array_size_Y, e2)
        add_array_modifier(obj_blender, 'array-modifier-Z', self.array_size_Z, e3)

      # add unit-cell bounding box
      if self.add_unit_cell_BB:
        obj_lattice_cell = add_lattice_cell(self, e1, e2, e3, name='lattice_cell', shift_origin=self.shift_origin, wiremode=True)
        selectObjects([obj_blender, obj_lattice_cell], active_object=obj_lattice_cell, context = context)
        bpy.ops.object.parent_set(type='OBJECT', keep_transform=False)

      return {'FINISHED'}
Example #8
0
def load_object():
    """ Loads an object """
    faces,verts = stl_utils.read_stl(virginobjectpath+'basic_cilinder_30_4_quad'+'.stl')
    ob1 = createMesh('virgin', verts, [], faces)