Skip to content
Snippets Groups Projects
import_obj.py 43.6 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 # use face_vert_loc_indices and face_vert_tex_indices previously defined and used the obj_face
    
                    line_split= line.split()
    
                else:
                    line_split= line[2:].split()
    
                    face_vert_loc_indices= []
                    face_vert_tex_indices= []
    
                    face_vert_loc_indices,\
                    face_vert_tex_indices,\
    
                    context_material,\
                    context_smooth_group,\
                    context_object\
                    ))
    
                if strip_slash(line_split):
                    context_multi_line = 'l'
                else:
                    context_multi_line = ''
    
                isline= line.startswith('l')
    
                for v in line_split:
                    vert_loc_index= int(v)-1
    
    
                    # Make relative negative vert indices absolute
    
                    if vert_loc_index < 0:
                        vert_loc_index= len(verts_loc) + vert_loc_index + 1
    
    
                    face_vert_loc_indices.append(vert_loc_index)
    
    
            elif line.startswith('s'):
                if CREATE_SMOOTH_GROUPS:
                    context_smooth_group= line_value(line.split())
                    if context_smooth_group=='off':
                        context_smooth_group= None
                    elif context_smooth_group: # is not None
                        unique_smooth_groups[context_smooth_group]= None
    
            elif line.startswith('o'):
                if SPLIT_OBJECTS:
                    context_object= line_value(line.split())
                    # unique_obects[context_object]= None
    
            elif line.startswith('g'):
                if SPLIT_GROUPS:
                    context_object= line_value(line.split())
                    # print 'context_object', context_object
                    # unique_obects[context_object]= None
                elif POLYGROUPS:
                    context_vgroup = line_value(line.split())
                    if context_vgroup and context_vgroup != '(null)':
                        vertex_groups.setdefault(context_vgroup, [])
                    else:
                        context_vgroup = None # dont assign a vgroup
    
            elif line.startswith('usemtl'):
                context_material= line_value(line.split())
                unique_materials[context_material]= None
            elif line.startswith('mtllib'): # usemap or usemat
                material_libs = list(set(material_libs) | set(line.split()[1:])) # can have multiple mtllib filenames per line, mtllib can appear more than once, so make sure only occurance of material exists
    
                # Nurbs support
            elif line.startswith('cstype '):
                context_nurbs['cstype']= line_value(line.split()) # 'rat bspline' / 'bspline'
            elif line.startswith('curv ') or context_multi_line == 'curv':
                line_split= line.split()
    
                curv_idx = context_nurbs['curv_idx'] = context_nurbs.get('curv_idx', []) # incase were multiline
    
                if not context_multi_line:
                    context_nurbs['curv_range'] = float_func(line_split[1]), float_func(line_split[2])
                    line_split[0:3] = [] # remove first 3 items
    
                if strip_slash(line_split):
                    context_multi_line = 'curv'
                else:
                    context_multi_line = ''
    
    
                for i in line_split:
                    vert_loc_index = int(i)-1
    
                    if vert_loc_index < 0:
                        vert_loc_index= len(verts_loc) + vert_loc_index + 1
    
                    curv_idx.append(vert_loc_index)
    
            elif line.startswith('parm') or context_multi_line == 'parm':
                line_split= line.split()
    
                if context_multi_line:
                    context_multi_line = ''
                else:
                    context_parm = line_split[1]
                    line_split[0:2] = [] # remove first 2
    
                if strip_slash(line_split):
                    context_multi_line = 'parm'
                else:
                    context_multi_line = ''
    
                if context_parm.lower() == 'u':
                    context_nurbs.setdefault('parm_u', []).extend( [float_func(f) for f in line_split] )
                elif context_parm.lower() == 'v': # surfaces not suported yet
                    context_nurbs.setdefault('parm_v', []).extend( [float_func(f) for f in line_split] )
                # else: # may want to support other parm's ?
    
            elif line.startswith('deg '):
                context_nurbs['deg']= [int(i) for i in line.split()[1:]]
            elif line.startswith('end'):
                # Add the nurbs curve
                if context_object:
                    context_nurbs['name'] = context_object
                nurbs.append(context_nurbs)
                context_nurbs = {}
                context_parm = ''
    
            ''' # How to use usemap? depricated?
            elif line.startswith('usema'): # usemap or usemat
                context_image= line_value(line.split())
            '''
    
        file.close()
        time_new= time.time()
    #     time_new= sys.time()
        print('%.4f sec' % (time_new-time_sub))
        time_sub= time_new
    
    
        print('\tloading materials and images...')
        create_materials(filepath, material_libs, unique_materials, unique_material_images, IMAGE_SEARCH)
    
        time_new= time.time()
    #     time_new= sys.time()
        print('%.4f sec' % (time_new-time_sub))
        time_sub= time_new
    
        if not ROTATE_X90:
            verts_loc[:] = [(v[0], v[2], -v[1]) for v in verts_loc]
    
        # deselect all
        bpy.ops.object.select_all(action='DESELECT')
    
        scene = context.scene
    #     scn.objects.selected = []
        new_objects= [] # put new objects here
    
        print('\tbuilding geometry...\n\tverts:%i faces:%i materials: %i smoothgroups:%i ...' % ( len(verts_loc), len(faces), len(unique_materials), len(unique_smooth_groups) ))
        # Split the mesh by objects/materials, may
        if SPLIT_OBJECTS or SPLIT_GROUPS:    SPLIT_OB_OR_GROUP = True
        else:                                SPLIT_OB_OR_GROUP = False
    
        for verts_loc_split, faces_split, unique_materials_split, dataname in split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP):
            # Create meshes from the data, warning 'vertex_groups' wont support splitting
            create_mesh(new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc_split, verts_tex, faces_split, unique_materials_split, unique_material_images, unique_smooth_groups, vertex_groups, dataname)
    
        # nurbs support
        for context_nurbs in nurbs:
            create_nurbs(context_nurbs, verts_loc, new_objects)
    
        # Create new obj
        for obj in new_objects:
            base = scene.objects.link(obj)
            base.select = True
    
        scene.update()
    
    
        axis_min= [ 1000000000]*3
        axis_max= [-1000000000]*3
    
    #     if CLAMP_SIZE:
    #         # Get all object bounds
    #         for ob in new_objects:
    #             for v in ob.getBoundBox():
    #                 for axis, value in enumerate(v):
    #                     if axis_min[axis] > value:    axis_min[axis]= value
    #                     if axis_max[axis] < value:    axis_max[axis]= value
    
    #         # Scale objects
    #         max_axis= max(axis_max[0]-axis_min[0], axis_max[1]-axis_min[1], axis_max[2]-axis_min[2])
    #         scale= 1.0
    
    #         while CLAMP_SIZE < max_axis * scale:
    #             scale= scale/10.0
    
    #         for ob in new_objects:
    #             ob.setSize(scale, scale, scale)
    
        # Better rotate the vert locations
        #if not ROTATE_X90:
        #    for ob in new_objects:
        #        ob.RotX = -1.570796326794896558
    
        time_new= time.time()
    #    time_new= sys.time()
    
        print('finished importing: %r in %.4f sec.' % (filepath, (time_new-time_main)))
        return {'FINISHED'}
    
    
    # NOTES (all line numbers refer to 2.4x import_obj.py, not this file)
    # check later: line 489
    # can convert now: edge flags, edges: lines 508-528
    # ngon (uses python module BPyMesh): 384-414
    # NEXT clamp size: get bound box with RNA
    # get back to l 140 (here)
    # search image in bpy.config.textureDir - load_image
    # replaced BPyImage.comprehensiveImageLoad with a simplified version that only checks additional directory specified, but doesn't search dirs recursively (obj_image_load)
    # bitmask won't work? - 132
    # uses bpy.sys.time()
    
    if __name__ == "__main__":
        register()