Instantly share code, notes, and snippets.

@jrivany /fix_nif.py
Last active Sep 25, 2017

Embed
What would you like to do?
#!/usr/bin/env python
import argparse, re
from pyffi.formats.nif import NifFormat
def main(args):
dryrun = args.dryrun
regex = None
if args.regex is not None:
regex = re.compile(args.regex)
file_changed = False
for stream, data in NifFormat.walkData(args.dir):
try:
file_changed = False
filename = stream.name
print('reading %s' % filename)
data.read(stream)
for block in data.blocks:
if is_block_to_be_removed(block, regex):
print('removing %s node' % type(block).__name__)
data.replace_global_node(block, None)
file_changed = True
# Output
if file_changed and not dryrun:
print('writing to %s' % filename)
stream.close()
output = open(filename, 'wb')
data.write(output)
output.close()
except Exception as e:
print('Error reading file %s' % e)
def is_block_to_be_removed(block, regex):
if (regex is not None
and isinstance(block, NifFormat.NiSourceTexture)
and regex.search(str(block.get_attribute('file_name')))):
return True
if isinstance(block, NifFormat.NiTextureEffect):
return True
return False
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser()
parser.add_argument('--dryrun', action='store_true',
help='Print out node deletions and then exit without replacing files')
parser.add_argument('--regex',
help=(
'The regular expression to use to match NiSourceTextures. '
'If none provided then this step is skipped').format())
parser.add_argument('dir', help='The root directory to scan for .nif files.')
args = parser.parse_args()
except Exception:
parser.print_help()
raise SystemExit()
main(args)
@jrivany

This comment has been minimized.

Owner

jrivany commented Sep 25, 2017

Example usage inspired by http://openmw.readthedocs.io/en/openmw-0.41.0/openmw-mods/convert_bump_mapped_mods.html#converting-apel-s-various-things-sacks

./fix_nif.py --regex '.*_nm\.dds' PATH_TO_APELS_VAIOUS_THINGS_FOLDER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment