if args.no_mtl:
	output_material = False
if args.no_vt:
	output_vt = False
if args.no_vn:
	output_vn = False
	
for fpath in (filename_primitive, filename_visual):
	if not os.path.exists(fpath):
		print "Failed to find %s" % fpath
		sys.exit(1)


#Unpack XML file
#subprocess.call(["php", "xml-convert.php", filename_visual, filename_visual.replace('.visual', '.xml2')])
wot.unpackXml(filename_visual, filename_visual.replace('.visual', '.xml'))

filename_visual = filename_visual.replace('.visual', '.xml')

tree = ET.parse(filename_visual)
root = tree.getroot()

geometry = root.find("renderSet").find("geometry")

visual_name_list = []
visual_textures = []
for set in root.findall("renderSet"):
	visual_name_list.append([
		set.find("geometry").find("vertices").text,
		set.find("geometry").find("primitive").text
	])
示例#2
0
def main(filename_primitive):
    flgSkinned = False
    flgLegacy = False
    flgVisualFound = True
    #	pdb.set_trace()
    filename = os.path.splitext(filename_primitive)[0]
    filename_visual = '%s.visual' % filename
    if filename_primitive.endswith('_processed'):
        filename_visual += '_processed'
    filename_obj = '%s.obj' % filename
    filename_mtl = '%s.mtl' % filename

    flgNewFormat = False

    scale_x = 1
    scale_y = 1
    scale_z = 1

    transform_x = 0
    transform_y = 0
    transform_z = 0

    compress = False

    output_material = True
    output_vt = True
    output_vn = True

    textures_path = ''  #'../../../../../'

    if args.modeLegacy:
        flgLegacy = True
    if args.visual != None:
        filename_visual = args.visual
    if args.obj != None:
        filename_obj = args.obj
        filename_mtl = filename_obj.replace(".obj", ".mtl")
    if args.mtl != None:
        filename_mtl = args.mtl
    if args.textures != None:
        textures_path = args.textures

    if args.scalex != None:
        scale_x = float(args.scalex)
    if args.scaley != None:
        scale_y = float(args.scaley)
    if args.scalez != None:
        scale_z = float(args.scalez)

    if args.transx != None:
        transform_x = float(args.transx)
    if args.transy != None:
        transform_y = float(args.transy)
    if args.transz != None:
        transform_z = float(args.transz)

    if args.compress:
        compress = True
    if args.no_mtl:
        output_material = False
    if args.no_vt:
        output_vt = False
    if args.no_vn:
        output_vn = False

    if not os.path.exists(filename_primitive):
        print("Failed to find %s" % filename_primitive)
        sys.exit(1)
    if not os.path.exists(filename_visual):
        print("Failed to find %s, material ignored" % filename_visual)
        flgVisualFound = False
        output_material = False


#Unpack XML file
#subprocess.call(["php", "xml-convert.php", filename_visual, filename_visual.replace('.visual', '.xml2')])
    visual_name_list = []
    visual_textures = []
    if flgVisualFound:
        wot.unpackXml(filename_visual,
                      filename_visual.replace('.visual', '.xml'))

        filename_visual = filename_visual.replace('.visual', '.xml')

        tree = ET.parse(filename_visual)
        root = tree.getroot()

        geometry = root.find("renderSet").find("geometry")

        for set in root.findall("renderSet"):
            visual_name_list.append([
                set.find("geometry").find("vertices").text,
                set.find("geometry").find("primitive").text
            ])

            group_textures = {}
            for item in geometry.findall("primitiveGroup"):
                textures = {
                    "diffuse": None,
                    "ident": item.find("material").find("identifier").text
                }
                for prop in item.find("material").findall("property"):
                    if prop.text.strip() == "diffuseMap":
                        textures['diffuse'] = prop.find("Texture").text.strip()
                group_textures[item.text.strip()] = textures
            visual_textures.append(group_textures)
        os.unlink(filename_visual)

    tree = None
    root = None

    with open(filename_primitive, 'rb') as mainFP:

        #alternative routine to convert primitives_processed to old .primitives
        if flgLegacy:
            s

        mainFP.seek(-4, 2)
        table_start = unpack('i', mainFP.read(4))[0]
        mainFP.seek(-4 - table_start, 2)
        sections = {}

        has_color = False
        has_uv2 = False
        position = 4
        sub_groups = 0
        uv2_section = ""

        #
        #read section table
        #
        while True:
            data = mainFP.read(4)
            if data == None or len(data) != 4:
                break

            section_size = unpack('I', data)[0]

            #Skip dummy bytes
            data = mainFP.read(16)

            data = mainFP.read(4)
            if data == None or len(data) != 4:
                break

            section_name_length = unpack('I', data)[0]
            section_name = mainFP.read(section_name_length).decode('UTF-8')

            print("Section [ %s ]" % section_name)

            if not flgVisualFound:
                if section_name not in visual_name_list:
                    if 'vertices' in section_name:
                        if section_name.endswith(
                                '.vertices'):  #normal format with meshname
                            objName = section_name.split('.vertices')[0]
                            visual_name_list.append(
                                [objName + '.vertices', objName + '.indices'])
                        else:  #rare format without meshname
                            visual_name_list.append([
                                section_name,
                                section_name.replace('vertices', 'indices')
                            ])

            if 'vertices' in section_name:
                sub_groups += 1
    #			print 'sub_groups = '+str(sub_groups)

            if 'colour' in section_name:
                has_color = True

            if 'uv2' in section_name:
                has_uv2 = True
                u2_section = section_name

            section = {
                'position': position,
                'size': section_size,
                'name': section_name
            }

            position += section_size
            if section_size % 4 > 0:
                position += 4 - section_size % 4  #dword alignment

            if section_name_length % 4 > 0:  #dword alignment
                mainFP.read(4 - section_name_length % 4)

            sections[section_name] = section

        sg = sub_groups - 1
        pl_flag = False

        subgroups = []

        while sub_groups > 0:  # for each 'vertices' section
            sub_groups -= 1

            groups = []
            subgroups.append(groups)

            name_vertices = visual_name_list[(sg - sub_groups)][0].strip()
            name_indicies = visual_name_list[(sg - sub_groups)][1].strip()

            ind_scale = 2
            stride = 32

            if sub_groups > 0:
                pl_flag = True
            section_vertices = sections[name_vertices]
            section_indicies = sections[name_indicies]

            mainFP.seek(section_indicies['position'])

            #warning: tailing bytes after indicies_subname not parsed (just padding junks)
            indicies_subname = ''
            i = 0
            while i < 64:
                ch = mainFP.read(1)
                if ord(ch) == 0:
                    break
                indicies_subname += ch.decode('UTF-8', errors='ignore')
                i += 1

            mainFP.seek(section_indicies['position'] + 64)

            #todo: is this the flag for oversize primitives? 65535+ polygons
            if "list32" in indicies_subname:
                ind_scale = 4

            indicies_count = unpack("I", mainFP.read(4))[0]
            indicies_groups = unpack("H", mainFP.read(2))[0]

            #print "subname", indicies_subname, "count", indicies_count, "groups", indicies_groups

            offset = indicies_count * ind_scale + 72
            mainFP.seek(section_indicies['position'] + offset)

            pGroups = []
            i = 0
            while i < indicies_groups:
                pGroups.append({
                    'id': i,
                    'startIndex': unpack("I", mainFP.read(4))[0],
                    'nPrimitives': unpack("I", mainFP.read(4))[0],
                    'startVertex': unpack("I", mainFP.read(4))[0],
                    'nVertices': unpack("I", mainFP.read(4))[0]
                })

                i += 1
            mainFP.seek(section_vertices['position'])

            vertices_subname = ''
            format_string = ''
            vertices_subname = mainFP.read(64).split(b'\x00')[0].decode(
                'UTF-8')
            #set 0.10.0 new format flag
            if "BPVT" in vertices_subname:
                flgNewFormat = True
                mainFP.read(4)  #null dword used to be vertex length
                format_string = mainFP.read(64).split(b'\x00')[0].decode(
                    'UTF-8')
                print('format_string=%s' % format_string)
    #			pdb.set_trace()

            vertices_count = unpack(
                "I", mainFP.read(4)
            )[0]  #total vertices count for current section, including all primitive groups in that section

            total_vertices = 0
            for group in pGroups:
                total_vertices += group['nVertices']

            total_polygons = 0
            for group in pGroups:
                total_polygons += group['nPrimitives']

            print("subname = %s\ncount = %s\npolys = %s" %
                  (vertices_subname, vertices_count, total_polygons))
            if "xyznuviiiwwtb" in vertices_subname:
                stride = 37
            if 'iiiww' in format_string:
                flgSkinned = True
            if format_string == 'set3/xyznuviiiwwtbpc':
                stride = 40
            """
			@TODO:
			if has_uv2:
				get_u2(vertices_count, uv2_section)
			"""

            big_l = indicies_groups
            k = 0
            i = 0

            while k < big_l:

                groups.append(pGroups[k])
                groups[k]['vertices'] = []

                pos = groups[k]['nVertices']
                v = 0
                while v < pos:
                    vert = Vertice()
                    #					pdb.set_trace()
                    vert.x = unpack('f',
                                    mainFP.read(4))[0] * scale_x + transform_x
                    vert.y = unpack('f',
                                    mainFP.read(4))[0] * scale_y + transform_y
                    vert.z = unpack('f',
                                    mainFP.read(4))[0] * scale_z + transform_z
                    vert.n = unpack('I', mainFP.read(4))[0]
                    vert.u = unpack('f', mainFP.read(4))[0]
                    vert.v = unpack('f', mainFP.read(4))[0]
                    if MIRROR_ENABLED and not flgSkinned:
                        vert.x = -vert.x

                    if stride == 32:
                        vert.t = unpack('I', mainFP.read(4))[0]
                        vert.bn = unpack('I', mainFP.read(4))[0]
                    elif stride == 37:  #old format skinned
                        vert.index_1 = ord(
                            mainFP.read(1))  #bone id for weight1
                        vert.index_2 = ord(
                            mainFP.read(1))  #bone id for weight2
                        vert.index_3 = ord(
                            mainFP.read(1))  #bone id for remaing weight??
                        vert.weight_1 = ord(mainFP.read(1))
                        vert.weight_2 = ord(mainFP.read(1))
                        vert.t = unpack('I', mainFP.read(4))[0]
                        vert.bn = unpack('I', mainFP.read(4))[0]
                    elif stride == 40:  #new format skinned. Data structure is guess work
                        vert.index_1 = ord(
                            mainFP.read(1))  #bone id for weight1
                        vert.index_2 = ord(
                            mainFP.read(1))  #bone id for weight2
                        vert.index_3 = ord(
                            mainFP.read(1))  #bone id for remaing weight??
                        vert.indexB_1 = ord(mainFP.read(
                            1))  #bone id for weight1 in another primitives?
                        vert.indexB_2 = ord(mainFP.read(
                            1))  #bone id for weight2 in another primitives?
                        vert.indexB_3 = ord(
                            mainFP.read(1)
                        )  #bone id for remaing weight in another primitives?
                        vert.weight_1 = ord(mainFP.read(1))
                        vert.weight_2 = ord(mainFP.read(1))
                        vert.t = unpack('I', mainFP.read(4))[0]
                        vert.bn = unpack('I', mainFP.read(4))[0]

    #					if vert.indexB_1 + vert.indexB_2 + vert.indexB_3:
    #						print 'mark'
    #						pdb.set_trace()

                    groups[k]['vertices'].append(vert)

                    v += 1
                    i += 1

                k += 1

            for group in groups:
                mainFP.seek(section_indicies['position'] +
                            group['startIndex'] * ind_scale + 72)

                group['indicies'] = []

                i = 0
                cnt = group['nPrimitives']
                while i < cnt:
                    p1 = None
                    p2 = None
                    p3 = None

                    if ind_scale != 2:
                        p2 = unpack('I', mainFP.read(4))[0]
                        p1 = unpack('I', mainFP.read(4))[0]
                        p3 = unpack('I', mainFP.read(4))[0]
                    else:
                        p2 = unpack('H', mainFP.read(2))[0]
                        p1 = unpack('H', mainFP.read(2))[0]
                        p3 = unpack('H', mainFP.read(2))[0]

                    group['indicies'].append({
                        'v1': p1,
                        'v2': p2,
                        'v3': p3
                    })

                    i += 1

        mtlc = ""
        objc = ""

        objc += "#Exported by Python script\n"
        #		objc += "#YOU SUCK\n\n"
        objc += "mtllib " + filename_mtl + "\n"

        mtlc += "#Exported by Python script\n"
        #		mtlc += "#YOU SUCK\n\n"

        total_vertices = 0

        sub_index = 0
        for subgroup in subgroups:
            if output_material:
                groups_textures = visual_textures[sub_index]
            else:
                groups_textures = {}

            for group in subgroup:
                material_name = "Material_%d_%d" % (sub_index, group['id'])
                object_name = "Group_%d_%d" % (sub_index, group['id'])

                if str(group['id']) in groups_textures:
                    textures = groups_textures[str(group['id'])]
                else:
                    textures = None

                object_name_print = object_name
                if textures != None and textures['ident'] != None:
                    object_name_print = textures['ident']

                if output_material:
                    if str(group['id']) in groups_textures:
                        textures = groups_textures[str(group['id'])]
                    else:
                        textures = None
                    mtlc += "\r\nnewmtl %s\n" % material_name
                    mtlc += "\tNs 20.0\n"
                    mtlc += "\tNi 1.0000\n"
                    mtlc += "\td 1.0000\n"
                    mtlc += "\tTr 1.0000\n"
                    mtlc += "\tTf 1.0000 1.0000 1.0000\n"
                    mtlc += "\tillum 1\n"
                    mtlc += "\tKa 0.2 0.2 0.2\n"
                    mtlc += "\tKd 0.3 0.3 0.3\n"
                    mtlc += "\tKs 0.4 0.4 0.4\n"
                    mtlc += "\tKe 0.0 0.0 0.0\n"
                    if textures != None and textures['diffuse'] != None and len(
                            textures['diffuse']) > 0:
                        mtlc += "\tmap_Kd " + textures_path + textures[
                            'diffuse'] + "\n"

    #			objc += "o %s\n" % object_name
                objc += "g %s|%s\n" % (object_name_print, object_name)

                for vertice in group['vertices']:
                    objc += "v %f %f %f\n" % (vertice.x, vertice.y, vertice.z)

                if output_vn:
                    x_cnt = 1
                    for vertice in group['vertices']:
                        #					pdb.set_trace()
                        if flgNewFormat:
                            n = unpackNormal_tag3(vertice.n)
                        else:
                            n = unpackNormal(vertice.n)
                        if MIRROR_ENABLED and not flgSkinned:
                            n['x'] = -n['x']
                        objc += "vn %f %f %f\n" % (n['x'], n['y'], n['z'])
                        x_cnt += 1

                if output_vt:
                    for vertice in group['vertices']:
                        objc += "vt %f %f 0.0\n" % (vertice.u, 1 - vertice.v)

                if output_material:
                    objc += "usemtl %s\n" % material_name
                    objc += "s 1\n"

                format = 0
                if not output_vn and not output_vt:
                    format = 1
                if output_vn and not output_vt:
                    format = 2
                if not output_vn and output_vt:
                    format = 3
                for indicie in group['indicies']:
                    l1 = total_vertices + indicie['v1'] + 1 - group[
                        'startVertex']
                    l2 = total_vertices + indicie['v2'] + 1 - group[
                        'startVertex']
                    l3 = total_vertices + indicie['v3'] + 1 - group[
                        'startVertex']

                    if flgSkinned:  #skinned primitives need to invert face vertex sequence
                        lx = l1
                        l1 = l3
                        l3 = lx
                    else:
                        if MIRROR_ENABLED:  #if mirror is introduced, non-skinned model also need to invert vertex sequence.
                            lx = l1
                            l1 = l3
                            l3 = lx

                    if format == 0:
                        objc += "f %d/%d/%d %d/%d/%d %d/%d/%d\n" % (
                            l3, l3, l3, l2, l2, l2, l1, l1, l1)
                    elif format == 1:
                        objc += "f %d %d %d\n" % (l3, l2, l1)
                    elif format == 2:
                        objc += "f %d//%d %d//%d %d//%d\n" % (l3, l3, l2, l2,
                                                              l1, l1)
                    elif format == 3:
                        objc += "f %d/%d %d/%d %d/%d\n" % (l3, l3, l2, l2, l1,
                                                           l1)

                total_vertices += group['nVertices']
            sub_index += 1

        if compress:
            objc = zlib.compress(objc)
            mtlc = zlib.compress(mtlc)

        with open(filename_obj, 'w') as fobj:
            fobj.write(objc)

        if output_material:
            with open(filename_mtl, 'w') as fmtl:
                fmtl.write(mtlc)
def main(filename_primitive):
	flgSkinned = False
	flgLegacy = False
	flgVisualFound = True
#	pdb.set_trace()
	filename = os.path.splitext(filename_primitive)[0]
	filename_visual	= '%s.visual' % filename
	if filename_primitive.endswith('_processed'):
		filename_visual	+= '_processed'
	filename_obj = '%s.obj' % filename
	filename_mtl = '%s.mtl' % filename
	
	flgNewFormat = False
	
	scale_x = 1
	scale_y = 1
	scale_z = 1
	
	transform_x = 0
	transform_y = 0
	transform_z = 0

	compress = False

	output_material = True
	output_vt = True
	output_vn = True

	textures_path = ''  #'../../../../../'

	if args.modeLegacy:
		flgLegacy = True
	if args.visual != None:
		filename_visual = args.visual
	if args.obj != None:
		filename_obj = args.obj
		filename_mtl = filename_obj.replace(".obj", ".mtl")
	if args.mtl != None:
		filename_mtl = args.mtl
	if args.textures != None:
		textures_path = args.textures
	
	if args.scalex != None:
		scale_x = float(args.scalex)
	if args.scaley != None:
		scale_y = float(args.scaley)
	if args.scalez != None:
		scale_z = float(args.scalez)

	if args.transx != None:
		transform_x = float(args.transx)
	if args.transy != None:
		transform_y = float(args.transy)
	if args.transz != None:
		transform_z = float(args.transz)
		
	if args.compress:
		compress = True
	if args.no_mtl:
		output_material = False
	if args.no_vt:
		output_vt = False
	if args.no_vn:
		output_vn = False
	
	if not os.path.exists(filename_primitive):
		print("Failed to find %s" % filename_primitive)
		sys.exit(1)
	if not os.path.exists(filename_visual):
		print("Failed to find %s, material ignored" % filename_visual)
		flgVisualFound = False
		output_material = False


#Unpack XML file
#subprocess.call(["php", "xml-convert.php", filename_visual, filename_visual.replace('.visual', '.xml2')])
	visual_name_list = []
	visual_textures = []
	if flgVisualFound:
		wot.unpackXml(filename_visual, filename_visual.replace('.visual', '.xml'))
	
		filename_visual = filename_visual.replace('.visual', '.xml')
	
		tree = ET.parse(filename_visual)
		root = tree.getroot()
	
		geometry = root.find("renderSet").find("geometry")
	
		for set in root.findall("renderSet"):
			visual_name_list.append([
				set.find("geometry").find("vertices").text,
				set.find("geometry").find("primitive").text
			])
	
			group_textures = {}
			for item in geometry.findall("primitiveGroup"):
				textures = {
					"diffuse": None,
					"ident": item.find("material").find("identifier").text
				}
				for prop in item.find("material").findall("property"):
					if prop.text.strip() == "diffuseMap":
						textures['diffuse'] = prop.find("Texture").text.strip()
				group_textures[item.text.strip()] = textures
			visual_textures.append(group_textures)
		os.unlink(filename_visual)

	tree = None
	root = None
	
		
	with open(filename_primitive, 'rb') as mainFP:
		
		#alternative routine to convert primitives_processed to old .primitives
		if flgLegacy:
			s



		mainFP.seek(-4, 2)
		table_start = unpack('i', mainFP.read(4))[0]
		mainFP.seek(- 4 - table_start, 2)
		sections = {}
		
		has_color = False
		has_uv2 = False
		position = 4
		sub_groups = 0
		uv2_section = ""

	
		#
		#read section table
		#
		while True:		
			data = mainFP.read(4)
			if data == None or len(data) != 4:
				break
			
			section_size = unpack('I', data)[0]
			
			#Skip dummy bytes
			data = mainFP.read(16)
			
			data = mainFP.read(4)
			if data == None or len(data) != 4:
				break
			
			section_name_length = unpack('I', data)[0]
			section_name = mainFP.read(section_name_length).decode('UTF-8')
		
			print("Section [ %s ]" % section_name)
		
			if not flgVisualFound:
				if section_name not in visual_name_list:
					if 'vertices' in section_name:
						if section_name.endswith('.vertices'):		#normal format with meshname 
							objName = section_name.split('.vertices')[0]
							visual_name_list.append([objName+'.vertices',
																		 objName+'.indices'])
						else:																			#rare format without meshname
							visual_name_list.append([section_name,
																		 section_name.replace('vertices','indices')])
			
			if 'vertices' in section_name:
				sub_groups += 1
	#			print 'sub_groups = '+str(sub_groups)
		
			if 'colour' in section_name:
				has_color = True
				
			if 'uv2' in section_name:
				has_uv2 = True
				u2_section = section_name
		
			section = {
				'position': position,
				'size': section_size,
				'name': section_name
			}
			
			position += section_size
			if section_size % 4 > 0:
				position += 4 - section_size % 4	#dword alignment
			
			if section_name_length % 4 > 0:			#dword alignment
				mainFP.read(4 - section_name_length % 4)
			
			sections[section_name] = section
		

		sg = sub_groups - 1
		pl_flag = False
		
		subgroups = []
		
		while sub_groups > 0:		# for each 'vertices' section
			sub_groups -= 1
			
			groups = []
			subgroups.append(groups)
		
			name_vertices = visual_name_list[(sg - sub_groups)][0].strip()
			name_indicies = visual_name_list[(sg - sub_groups)][1].strip()

			ind_scale = 2
			stride = 32
		
			if sub_groups > 0:
				pl_flag = True
			section_vertices = sections[name_vertices]
			section_indicies = sections[name_indicies]
		
			mainFP.seek(section_indicies['position'])
		
			#warning: tailing bytes after indicies_subname not parsed (just padding junks)
			indicies_subname = ''
			i = 0
			while i < 64:
				ch = mainFP.read(1)
				if ord(ch) == 0:
					break
				indicies_subname += ch.decode('UTF-8', errors='ignore')
				i += 1
			
			mainFP.seek(section_indicies['position'] + 64)
		
			#todo: is this the flag for oversize primitives? 65535+ polygons
			if "list32" in indicies_subname:
				ind_scale = 4
			
			indicies_count = unpack("I", mainFP.read(4))[0]
			indicies_groups = unpack("H", mainFP.read(2))[0]
			
			#print "subname", indicies_subname, "count", indicies_count, "groups", indicies_groups
			
			offset = indicies_count * ind_scale + 72
			mainFP.seek(section_indicies['position'] + offset)
		
			pGroups = []
			i = 0
			while i < indicies_groups:
				pGroups.append({
					'id': i,
					'startIndex': unpack("I", mainFP.read(4))[0],
					'nPrimitives': unpack("I", mainFP.read(4))[0],
					'startVertex': unpack("I", mainFP.read(4))[0],
					'nVertices': unpack("I", mainFP.read(4))[0]
				})
		
				i += 1
			mainFP.seek(section_vertices['position'])
		
			vertices_subname = ''
			format_string = ''
			vertices_subname = mainFP.read(64).split(b'\x00')[0].decode('UTF-8')
	#set 0.10.0 new format flag			
			if "BPVT" in vertices_subname:
				flgNewFormat = True
				mainFP.read(4)	#null dword used to be vertex length
				format_string = mainFP.read(64).split(b'\x00')[0].decode('UTF-8')
				print('format_string=%s' % format_string)
	#			pdb.set_trace()
	
			vertices_count = unpack("I", mainFP.read(4))[0]		#total vertices count for current section, including all primitive groups in that section
			
			total_vertices = 0
			for group in pGroups:
				total_vertices += group['nVertices']
				
			total_polygons = 0
			for group in pGroups:
				total_polygons += group['nPrimitives']
			
			print( "subname = %s\ncount = %s\npolys = %s" % (vertices_subname, vertices_count, total_polygons) )
			if "xyznuviiiwwtb" in vertices_subname:
				stride = 37
			if 'iiiww' in format_string:
				flgSkinned = True
			if format_string == 'set3/xyznuviiiwwtbpc':
				stride = 40
				
			"""
			@TODO:
			if has_uv2:
				get_u2(vertices_count, uv2_section)
			"""
			
			big_l = indicies_groups
			k = 0
			i = 0
			
			while k < big_l:
				
				groups.append(pGroups[k])
				groups[k]['vertices'] = []
				
				pos = groups[k]['nVertices']
				v = 0
				while v < pos:
					vert = Vertice()
#					pdb.set_trace()
					vert.x = unpack('f', mainFP.read(4))[0] * scale_x + transform_x
					vert.y = unpack('f', mainFP.read(4))[0] * scale_y + transform_y
					vert.z = unpack('f', mainFP.read(4))[0] * scale_z + transform_z
					vert.n = unpack('I', mainFP.read(4))[0]
					vert.u = unpack('f', mainFP.read(4))[0]
					vert.v = unpack('f', mainFP.read(4))[0]
					if MIRROR_ENABLED and not flgSkinned:
						vert.x = - vert.x
				
					if stride == 32:
						vert.t = unpack('I', mainFP.read(4))[0]
						vert.bn = unpack('I', mainFP.read(4))[0]
					elif stride == 37:	#old format skinned
						vert.index_1 = ord(mainFP.read(1))	#bone id for weight1
						vert.index_2 = ord(mainFP.read(1))	#bone id for weight2
						vert.index_3 = ord(mainFP.read(1))	#bone id for remaing weight??
						vert.weight_1 = ord(mainFP.read(1))
						vert.weight_2 = ord(mainFP.read(1))
						vert.t = unpack('I', mainFP.read(4))[0]
						vert.bn = unpack('I', mainFP.read(4))[0]
					elif stride == 40:	#new format skinned. Data structure is guess work 
						vert.index_1 = ord(mainFP.read(1))	#bone id for weight1
						vert.index_2 = ord(mainFP.read(1))	#bone id for weight2
						vert.index_3 = ord(mainFP.read(1))	#bone id for remaing weight??
						vert.indexB_1 = ord(mainFP.read(1))	#bone id for weight1 in another primitives?
						vert.indexB_2 = ord(mainFP.read(1))	#bone id for weight2 in another primitives?
						vert.indexB_3 = ord(mainFP.read(1))	#bone id for remaing weight in another primitives?
						vert.weight_1 = ord(mainFP.read(1))
						vert.weight_2 = ord(mainFP.read(1))
						vert.t = unpack('I', mainFP.read(4))[0]
						vert.bn = unpack('I', mainFP.read(4))[0]
						
	#					if vert.indexB_1 + vert.indexB_2 + vert.indexB_3:
	#						print 'mark'
	#						pdb.set_trace()
				
					groups[k]['vertices'].append(vert)
				
					v += 1
					i += 1
			
				k += 1
			
			for group in groups:
				mainFP.seek(section_indicies['position'] + group['startIndex'] * ind_scale + 72)
	
				group['indicies'] = []
				
				i = 0
				cnt = group['nPrimitives']
				while i < cnt:			
					p1 = None
					p2 = None
					p3 = None
				
					if ind_scale != 2:
						p2 = unpack('I', mainFP.read(4))[0]
						p1 = unpack('I', mainFP.read(4))[0]
						p3 = unpack('I', mainFP.read(4))[0]
					else:
						p2 = unpack('H', mainFP.read(2))[0]
						p1 = unpack('H', mainFP.read(2))[0]
						p3 = unpack('H', mainFP.read(2))[0]
				
					group['indicies'].append({
						'v1': p1,
						'v2': p2,
						'v3': p3
					});
					
					i += 1
			
			
		mtlc = ""
		objc = ""
			
		objc += "#Exported by Python script\n";
#		objc += "#YOU SUCK\n\n"
		objc += "mtllib " + filename_mtl + "\n"
		
		mtlc += "#Exported by Python script\n";
#		mtlc += "#YOU SUCK\n\n"
	
		total_vertices = 0
	
		sub_index = 0
		for subgroup in subgroups:
			if output_material:
				groups_textures = visual_textures[sub_index]
			else:
				groups_textures = {}
		
			for group in subgroup:
				material_name = "Material_%d_%d" % (sub_index, group['id'])
				object_name = "Group_%d_%d" % (sub_index, group['id'])
			
				if str(group['id']) in groups_textures:
					textures = groups_textures[str(group['id'])]
				else:
					textures = None
					
				object_name_print = object_name
				if textures != None and textures['ident'] != None:
					object_name_print = textures['ident']
				
				if output_material:
					if str(group['id']) in groups_textures:
						textures = groups_textures[str(group['id'])]
					else:
						textures = None
					mtlc += "\r\nnewmtl %s\n" % material_name
					mtlc += "\tNs 20.0\n"
					mtlc += "\tNi 1.0000\n"
					mtlc += "\td 1.0000\n"
					mtlc += "\tTr 1.0000\n"
					mtlc += "\tTf 1.0000 1.0000 1.0000\n"
					mtlc += "\tillum 1\n"
					mtlc += "\tKa 0.2 0.2 0.2\n"
					mtlc += "\tKd 0.3 0.3 0.3\n"
					mtlc += "\tKs 0.4 0.4 0.4\n"
					mtlc += "\tKe 0.0 0.0 0.0\n"
					if textures != None and textures['diffuse'] != None and len(textures['diffuse']) > 0:
						mtlc += "\tmap_Kd " + textures_path + textures['diffuse'] + "\n"
				
	#			objc += "o %s\n" % object_name
				objc += "g %s|%s\n" % (object_name_print,object_name)
				
				for vertice in group['vertices']:
					objc += "v %f %f %f\n" % (vertice.x,vertice.y,vertice.z)
					
				if output_vn:
					x_cnt = 1
					for vertice in group['vertices']:		
	#					pdb.set_trace()
						if flgNewFormat: 
							n = unpackNormal_tag3(vertice.n)
						else:				
							n = unpackNormal(vertice.n)
						if MIRROR_ENABLED and not flgSkinned:
							n['x'] = -n['x']
						objc += "vn %f %f %f\n" % (n['x'],n['y'],n['z'])
						x_cnt += 1
				
				if output_vt:
					for vertice in group['vertices']:
						objc += "vt %f %f 0.0\n" % (vertice.u, 1-vertice.v)
					
				if output_material:
					objc += "usemtl %s\n" % material_name
					objc += "s 1\n"
					
					
				format = 0
				if not output_vn and not output_vt:
					format = 1
				if output_vn and not output_vt:
					format = 2
				if not output_vn and output_vt:
					format = 3
				for indicie in group['indicies']:
					l1 = total_vertices + indicie['v1'] + 1 - group['startVertex']
					l2 = total_vertices + indicie['v2'] + 1 - group['startVertex']
					l3 = total_vertices + indicie['v3'] + 1 - group['startVertex']
					
					if flgSkinned: #skinned primitives need to invert face vertex sequence
						lx = l1
						l1 = l3
						l3 = lx
					else:
						if MIRROR_ENABLED:		#if mirror is introduced, non-skinned model also need to invert vertex sequence.
							lx = l1
							l1 = l3
							l3 = lx
	
					if format == 0:
						objc += "f %d/%d/%d %d/%d/%d %d/%d/%d\n" % (l3,l3,l3,l2,l2,l2,l1,l1,l1)
					elif format == 1:
						objc += "f %d %d %d\n" % (l3,l2,l1)
					elif format == 2:
						objc += "f %d//%d %d//%d %d//%d\n" % (l3,l3,l2,l2,l1,l1)
					elif format == 3:
						objc += "f %d/%d %d/%d %d/%d\n" % (l3,l3,l2,l2,l1,l1)
					
				total_vertices += group['nVertices']
			sub_index += 1
		
		
		if compress:
			objc = zlib.compress(objc)
			mtlc = zlib.compress(mtlc)
		
		with open(filename_obj, 'w') as fobj:
			fobj.write(objc)
			
		if output_material:
			with open(filename_mtl, 'w') as fmtl:
				fmtl.write(mtlc)