Skip to content
Snippets Groups Projects
io_import_scene_lwo.py 40.6 KiB
Newer Older
Ken Nign's avatar
Ken Nign committed
                    colf= vcol.data[fi]
                    
                    if len(face) > 2:
                        colf.color1= face[0]
                        colf.color2= face[1]
                        colf.color3= face[2]
                    if len(face) == 4:
                        colf.color4= face[3]
                        
        # Create the UV Maps.
        if len(layer_data.uvmaps) > 0:
            print("Adding "+str(len(layer_data.uvmaps))+" UV Textures")
            for uvmap_key in layer_data.uvmaps:
                map_pack= create_mappack(layer_data, uvmap_key, "UV")
                uvm= me.uv_textures.new(uvmap_key)
                if not uvm:
                    break
                for fi in map_pack:
                    if fi > len(uvm.data):
                        continue
                    face= map_pack[fi]
                    uvf= uvm.data[fi]
                    
                    if len(face) > 2:
                        uvf.uv1= face[0]
                        uvf.uv2= face[1]
                        uvf.uv3= face[2]
                    if len(face) == 4:
                        uvf.uv4= face[3]

        # Now add the NGons.
        if len(ngons) > 0:
            for ng_key in ngons:
                face_offset= len(me.faces)
                ng= ngons[ng_key]
                v_locs= []
                for vi in range(len(ng)):
                    v_locs.append(mathutils.Vector(layer_data.pnts[ngons[ng_key][vi]]))
                tris= PolyFill([v_locs])
                me.faces.add(len(tris))
                for tri in tris:
                    face= me.faces[face_offset]
                    face.vertices_raw[0]= ng[tri[0]]
                    face.vertices_raw[1]= ng[tri[1]]
                    face.vertices_raw[2]= ng[tri[2]]
                    face.material_index= me.faces[ng_key].material_index
                    face.use_smooth= me.faces[ng_key].use_smooth
                    face_offset+= 1
        
        me.update()
        
        # Non-face edges won't show up until Edit Mode cycling
        if has_edges:
            bpy.ops.object.select_all(action='DESELECT')
            ob.users_scene[0].objects.active= ob
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.object.mode_set(mode='OBJECT')
        
        # Unfortunately we can't exlude certain faces from the subdivision.
        if layer_data.has_subds and add_subd_mod:
            ob.modifiers.new(name="Subsurf", type='SUBSURF')
        
        # Should we build an armature from the embedded rig?
        if len(layer_data.bones) > 0 and skel_to_arm:
            bpy.ops.object.armature_add()
            arm_object= bpy.context.active_object
            arm_object.name= "ARM_" + layer_data.name
            arm_object.data.name= arm_object.name
            arm_object.location= layer_data.pivot
            bpy.ops.object.mode_set(mode='EDIT')
            build_armature(layer_data, arm_object.data.edit_bones)
            bpy.ops.object.mode_set(mode='OBJECT')
        
        # Clear out the dictionaries for this layer.
        layer_data.bone_names.clear()
        layer_data.bone_rolls.clear()
        layer_data.wmaps.clear()
        layer_data.colmaps.clear()
        layer_data.uvmaps.clear()
        layer_data.morphs.clear()
        layer_data.surf_tags.clear()
    
    # With the objects made, setup the parents and re-adjust the locations.
    for ob_key in ob_dict:
        if ob_dict[ob_key][1] != -1 and ob_dict[ob_key][1] in ob_dict:
            parent_ob = ob_dict[ob_dict[ob_key][1]]
            ob_dict[ob_key][0].parent= parent_ob[0]
            ob_dict[ob_key][0].location-= parent_ob[0].location
            
    bpy.context.scene.update()
    
    print("Done Importing LWO File")
    
    
from bpy.props import *

class IMPORT_OT_lwo(bpy.types.Operator):
    '''Import LWO Operator.'''
    bl_idname= "import_scene.lwo"
    bl_label= "Import LWO"
    bl_description= "Import a LightWave Object file."
    bl_options= {'REGISTER', 'UNDO'}
    
    filepath= StringProperty(name="File Path", description="Filepath used for importing the LWO file", maxlen=1024, default="")
    
    ADD_SUBD_MOD= BoolProperty(name="Apply SubD Modifier", description="Apply the Subdivision Surface modifier to layers with Subpatches", default= True)
    LOAD_HIDDEN= BoolProperty(name="Load Hidden Layers", description="Load object layers that have been marked as hidden", default= False)
    SKEL_TO_ARM= BoolProperty(name="Create Armature", description="Create an armature from an embedded Skelegon rig", default= True)
    
    def execute(self, context):
        load_lwo(self.properties.filepath,
                 context,
                 self.properties.ADD_SUBD_MOD,
                 self.properties.LOAD_HIDDEN,
                 self.properties.SKEL_TO_ARM)
        return {'FINISHED'}
    
    def invoke(self, context, event):
        wm= context.window_manager
        wm.add_fileselect(self)
        return {'RUNNING_MODAL'}


def menu_func(self, context):
    self.layout.operator(IMPORT_OT_lwo.bl_idname, text="LightWave Object (.lwo)")

def register():
    bpy.types.INFO_MT_file_import.append(menu_func)
    
def unregister():
    bpy.types.INFO_MT_file_import.remove(menu_func)

if __name__ == "__main__":
Guillermo S. Romero's avatar
Guillermo S. Romero committed
    register()