def Main(): def process_nif(this_file): #return print('Processing', test_file) target_nif = load_nif(this_file, template = template_mesh, settings = current_settings, init_skin = True) #target_nif = load_nif(path.join(path, test_file)) target_bone_dict = dict([(bone_name, val['bone'])for this_mesh in target_nif.meshes for bone_name, val in this_mesh.bone_dict.items()]) targets_found = 0 for this_mesh in template_mesh.m_list: target_nif.copy_skin_instance(this_mesh.block) #list(target_nif.skeleton_root)[0].add_child(this_mesh.block) new_mesh = kg.mesh_util.mesh(target_nif, this_mesh.block) for bone_name, b in this_mesh.bone_dict.items(): this_bone = target_bone_dict.get(bone_name, b['bone']) new_mesh.addBone( this_bone, dict([(v.index, v.weight) for v in b['weight' ]])) if current_settings['subfolder']: """Preserve the relative file structure in the output folder""" rel_path = path.relpath(this_path, start=current_settings['target']) output_folder = path.join(current_settings['destination'], rel_path) if not path.exists(output_folder): makedirs(output_folder) print ('Folder not found. Adding ' + output_folder) else: output_folder = current_settings['destination'] if current_settings.get('combine_material'): print('Merging Duplicate Blocks') toaster = NifToaster() SpellMergeDuplicates(data=target_nif.data, toaster=toaster).recurse() if current_settings.get('combine_name'): template_path, template_filename = path.split(current_settings['template']) test_file = sub(r_nif, '_', test_file) + template_filename target_nif.save(output_folder,test_file) r_nif = compile('.ni[f$\}]', flags=IGNORECASE) if not path.exists(current_settings['destination']): print ('destination', current_settings['destination']) makedirs(current_settings['destination']) template_mesh = None if path.exists(current_settings['template']): template_mesh = loadTemplate(current_settings['template'], settings = current_settings) if not template_mesh: print('No Valid Blocks found on Template Mesh, Exiting') return """Build a dictionary of all manifold vertices on the mesh""" #current_settings['bone_list'] = template_mesh.getVerts(side = side) #print('Template Found') if not template_mesh: print('Template Not Found, Exiting') return nif_list = kg.file_util.get_files(current_settings['target'], current_settings, tri = False) print(nif_list) for this_path, test_file in nif_list: print('Processing Files') print (this_path, test_file) process_nif(this_path, test_file)
def Main(): Female = False Male = False NoGender = True def process_tri(this_file): if template_verts: print('******TRI******') mesh_ = template_mesh verts_ = template_verts if gender_list or current_settings['gender']: """ Determine Gender of Tri File """ root_nif_path = re.sub(r_tri_end, '.nif', this_file) tri_gender = getGenderFlag(root_nif_path) if tri_gender is 'NONE' and path.exists(root_nif_path): target_nif = load_nif(root_nif_path, template = False, settings = {}) if not target_nif.valid: tri_gender = 'NONE' else: tri_gender = target_nif.getGender() if gender_list: mesh_ = template_mesh.get(tri_gender) verts_ = template_verts.get(tri_gender) if not mesh_ or not verts_: print('Incorrect Gender:', this_file ) print('Closing', this_file ) return elif tri_gender != current_settings['gender']: print('Incorrect Gender:', this_file ) print('Closing', this_file ) return print('Processing', this_file) target_tri = load_tri(this_file, template = template_mesh, settings = current_settings) tri_mesh = target_tri.mesh # for a in zip(sorted([(vert.idx, vert.getLoc(world_loc = True).as_list()) for vert in tri_mesh.getVerts(nmv = True).values()], key = itemgetter(0)), # sorted([(vert.idx, vert.getLoc(world_loc = False).as_list()) for vert in template_mesh.getVerts(nmv = True).values()], key = itemgetter(0))): # print (a) targets_found = mainSearch(current_settings, mesh_, tri_mesh, search_resolution = 1, vertFunction = setTriSeams, nmv = target_nmv, act_verts = verts_, tri = True, world_loc = True) save_file(target_tri, this_file, targets_found, current_settings) def process_nif(this_file, repair_doubles = False): # if findall(r_nif, test_file) and test_file != current_settings['template']: print('******NIF******') print('Processing', this_file) target_nif = load_nif(this_file, template = template_mesh, settings = current_settings) if not target_nif.valid: print(target_nif,'Could Not Be Processed') return nif_gender = target_nif.getGender() if gender_list: if nif_gender not in gender_list: print('Incorrect Gender:', this_file ) print('Closing', this_file ) return mesh_ = template_mesh.get(nif_gender) verts_ = template_verts.get(nif_gender) elif nif_gender != current_settings['gender']: print('Incorrect Gender:', this_file ) print('Closing', this_file ) return targets_found = 0 for this_mesh in target_nif.meshes: if gender_list: targets_found += mainSearch(current_settings, mesh_, this_mesh, search_resolution = 1, vertFunction = setSeams, nmv = target_nmv, act_verts = verts_) else: targets_found += mainSearch(current_settings, template_mesh, this_mesh, search_resolution = 1, vertFunction = setSeams, nmv = target_nmv, act_verts = template_verts) if repair_doubles: for this_mesh in target_nif.meshes: if current_settings.get('doubles'): for tup in this_mesh.nmv_doubles: average_normal = kg.math_util.vector3([0,0,0]) bad_normal = 0 for v in tup: if v.normal: average_normal += v.normal else: bad_normal += 1 if bad_normal == len(tup): continue for v in tup: v.setNormal(kg.math_util.normalizeVector(average_normal)) targets_found += 1 save_file(target_nif, this_file, targets_found, current_settings) #target_nif.save(this_file, current_settings) #save_file(target_nif, this_file, targets_found, current_settings) if not path.exists(current_settings['destination']): print ('destination', current_settings['destination']) makedirs(current_settings['destination']) template_mesh = False template_verts = {} gender_list = [] if path.exists(current_settings['template']): template_mesh = loadTemplate(current_settings['template'], settings = current_settings) if not template_mesh: print('Invalid Template, Exiting') return print('TEMPLATE MESH', template_mesh) gender = False if not isinstance(dict, kg.mesh_util.multiMesh): print(template_mesh) if 'FEMALE' in template_mesh: Female = True gender_list.append('FEMALE') if 'MALE' in template_mesh: Male = True gender_list.append('MALE') if 'NONE' in template_mesh: Neutral = True gender_list.append('NONE') """Build a dictionary of all manifold vertices on the mesh""" template_nmv = current_settings.get('template_mesh') if template_nmv == 'Edges Only': template_nmv = True elif template_nmv == 'All': template_nmv = False else: template_nmv = 'EXCLUDE' print(template_nmv) if gender_list: template_verts = dict([(gender, template_mesh[gender].getVerts(nmv = template_nmv)) for gender in gender_list]) else: template_verts = template_mesh.getVerts(nmv = template_nmv) print('Template Found') if not template_mesh and not current_settings.get('doubles'): print('Template Not Found, Exiting') return target_nmv = current_settings.get('target_mesh') if target_nmv == 'Exclude Edges': target_nmv = 'EXCLUDE' elif target_nmv == 'All': target_nmv = False else: target_nmv = True if current_settings.get('gender') == 'Female Only': current_settings['gender'] = 'FEMALE' elif current_settings.get('gender') == 'Male Only': current_settings['gender'] = 'MALE' else: current_settings['gender'] = None if current_settings['tri']: nif_list, tri_list = get_files(current_settings['target'], current_settings, tri = True, morph = False) for this_file in nif_list: process_nif(this_file, repair_doubles = current_settings.get('doubles')) if template_mesh: for this_file in tri_list: process_tri(this_file) else: nif_list = get_files(current_settings['target'], current_settings, morph = False) for this_file in nif_list: process_nif(this_file, repair_doubles = current_settings.get('doubles'))
def Main(): print (current_settings.get('Game')) def process_morph(test_file, bone_list, morph_dict): print('Processing', test_file) target_nif = load_nif(test_file, template = template_mesh, settings = current_settings) if not target_nif.valid: print(test_file,'Could Not Be Processed') return targets_found = 0 for this_mesh in target_nif.meshes: targets_found += mainSearch(current_settings, template_mesh, this_mesh, search_resolution = 1, vertFunction = setWeightDictionary, side = side) save_file(target_nif, test_file, targets_found, current_settings, morph_dict = morph_dict) def process_nif(test_file, bone_list): print('Processing', test_file) target_nif = load_nif(test_file, template = template_mesh, settings = current_settings) if not target_nif.valid: print(test_file,'Could Not Be Processed') targets_found = 0 for this_mesh in target_nif.meshes: targets_found += mainSearch(current_settings, template_mesh, this_mesh, search_resolution = 1, vertFunction = setWeightDictionary, side = side) save_file(target_nif, test_file, targets_found, current_settings) if not path.exists(current_settings['destination']): makedirs(current_settings['destination']) template_mesh = None if path.exists(current_settings['template']): template_mesh = loadTemplate(current_settings['template'], settings = current_settings) if not template_mesh: print('No Valid Blocks found on Template Mesh, Exiting') return skeleton = current_settings.get('template_s') template_mesh.loadSkeleton(skeleton) bone_list = current_settings['bone_list'] = sorted(list(template_mesh.bone_dict.keys())) #print(bone_list) if current_settings['select_bones']: bone_menu = ui_tools.boneMenu(bone_list, side = side) current_settings['bone_list'] = bone_menu.openMenu() #print (bone_list) try: bone_menu.destroy() except: pass if not template_mesh: print('Template Not Found, Exiting') return if current_settings.get('template_s'): print('Generating Bone Dictionary from Skeleton') skeleton = kg.file_util.load_nif(current_settings['template_s'], settings = current_settings, skeleton_only = True) for b_data in skeleton.bone_dict.values(): if b_data.get('is_skel_root'): root = b_data break for bone_name, bone_data in template_mesh.lno.bone_dict.items(): if bone_name not in skeleton.bone_dict and not bone_data.get('is_skel_root'): skeleton.bone_dict[bone_name] = bone_data root['children'].append(bone_name) template_mesh.lno.bone_dict = skeleton.bone_dict print('Skeleton Dictionary Generation Complete') nif_list, morph_set = kg.file_util.get_files(current_settings['target'], current_settings, tri = False) for test_file in nif_list: #print('test_file, test_file') process_nif(test_file, bone_list) if morph_set: morph_key = kg.file_util.getMorphType(current_settings.get('template')) if morph_key == 0: base = '_0.nif' morph = '_1.nif' else: base = '_1.nif' morph = '_0.nif' for this_morph in morph_set: base_file = this_morph + base morph_file = this_morph + morph #print(morph_file) morph_dict = {} print(morph_file) if path.exists(morph_file): morph_nif = load_nif(morph_file, template = False, settings = current_settings, init_skin = False, init_mesh = False) morph_dict = {} for mesh_ in morph_nif.meshes: #print(mesh_.block.name) morph_dict[mesh_.block.name] = dict([(v_idx, (vert_.getLoc(world_loc = False), vert_.getNormal(world_loc = False))) for v_idx, vert_ in mesh_.verts.items()]) morph_nif.nif_file.close() #print(morph_dict) process_morph(base_file, bone_list, morph_dict)
def Main(): def process_nif(this_path, test_file): #return if findall(r_nif, test_file) and test_file != current_settings['template']: print('Processing', test_file) target_nif = load_nif(path.join(this_path, test_file), template = template_mesh, settings = current_settings) #target_nif = load_nif(path.join(path, test_file)) targets_found = 0 for this_mesh in target_nif.meshes: mainSearch(current_settings, template_mesh, this_mesh, search_resolution = current_settings['res'], vertFunction = compareMatches) print('Displaying Results for', this_mesh.block.name) if current_settings.get('report'): for v in this_mesh.verts.values(): print(v.idx, 'Exact Match: ', v.exact_match) print('Detail View', v.match_results) else: for v in this_mesh.verts.values(): if not v.exact_match: print(v.idx, 'Exact Match: ', v.exact_match) print('Detail View', v.match_results) r_nif = compile('.ni[f$\}]', flags=IGNORECASE) if not path.exists(current_settings['destination']): print ('destination', current_settings['destination']) makedirs(current_settings['destination']) template_mesh = None if path.exists(current_settings['template']): template_mesh = loadTemplate(current_settings['template'], settings = current_settings) if not template_mesh: print('No Valid Blocks found on Template Mesh, Exiting') return """Build a dictionary of all manifold vertices on the mesh""" #current_settings['bone_list'] = template_mesh.getVerts(side = side) #print('Template Found') if not template_mesh: print('Template Not Found, Exiting') return dirpath = current_settings['target'] r_parse_file_bracket = compile('\}\s\{', flags=IGNORECASE) r_parse_file_spacer = compile('\.nif\s', flags=IGNORECASE) r_parse_file_spacer2 = compile('\.nif\',\s\'', flags=IGNORECASE) r_parse_bracket = compile('[\{\}]', flags=IGNORECASE) """Check for .nif in dirpath""" print ('dirpath', dirpath) if not findall(r_nif, dirpath): if current_settings['subfolder']: for this_path, dirnames, filenames in walk(dirpath): for this_file in filenames: process_nif(this_path, this_file) else: for test_file in listdir(dirpath): process_nif(dirpath, test_file) else: if findall(r_parse_file_spacer2, dirpath): for this_file in eval(dirpath): this_path, test_file = path.split(sub(r_parse_bracket, '', this_file)) process_nif(this_path, this_file) else: dir_files = sub(r_parse_file_spacer, '.nif} {', dirpath) dir_files = split(r_parse_file_bracket, dir_files) for this_file in dir_files: this_path, test_file = path.split(sub(r_parse_bracket, '', this_file)) process_nif(this_path, test_file)