Last active
June 20, 2019 16:59
-
-
Save vvzen/cc661587b26149db8544b9cbd5b4181d to your computer and use it in GitHub Desktop.
Command line app in python that renames textures to match mudbox UDIM naming convention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import re | |
| import argparse | |
| import shutil | |
| MARI_REGEX = re.compile(r'(\d{2})(\d{2})') | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| '--dir', | |
| help= | |
| r'source path with the files. On windows, use double blackslashes for the paths (es: P:\\unity\\export_textures)', | |
| required=True) | |
| parser.add_argument('--source', | |
| help='name of the source application', | |
| required=True) | |
| parser.add_argument('--dest', | |
| help='name of the destination application', | |
| required=True) | |
| def str2bool(v): | |
| if isinstance(v, bool): | |
| return v | |
| if v.lower() in ('yes', 'true', 't', 'y', '1'): | |
| return True | |
| elif v.lower() in ('no', 'false', 'f', 'n', '0'): | |
| return False | |
| else: | |
| raise argparse.ArgumentTypeError('Boolean value expected.') | |
| parser.add_argument('--preview', | |
| required=True, | |
| help='if you only want to preview the renaming', | |
| type=str2bool) | |
| args = parser.parse_args() | |
| def rename_texture(oldname, inputapp, outputapp): | |
| splitted = oldname.split('_') | |
| old_name = '_'.join(splitted[:-1]) | |
| if inputapp == 'mari': | |
| tokens = MARI_REGEX.findall(splitted[-1]) | |
| #print tokens | |
| if int(tokens[0][1]) % 10 == 0: | |
| u_number = '1' + str(tokens[0][1][1]) | |
| v_number = (int(tokens[0][1][0]) % 10) | |
| else: | |
| u_number = int(tokens[0][1][1]) | |
| v_number = (int(tokens[0][1][0]) % 10) + 1 | |
| else: | |
| return | |
| if outputapp == 'mudbox': | |
| new_name = '{name}_u{u}_v{v}'.format(name=old_name, | |
| u=u_number, | |
| v=v_number) | |
| else: | |
| return | |
| return new_name | |
| def main(): | |
| if not os.path.exists(args.dir): | |
| print 'the given dir path doesn not exists! : {}'.format(args.dir) | |
| return | |
| if args.source not in ['mari']: | |
| print '{} as input app is not supported'.format(args.source) | |
| return | |
| if args.dest not in ['mudbox']: | |
| print '{} as out app is not supported'.format(args.dest) | |
| return | |
| source_folder = os.path.join(os.path.realpath(os.path.normpath(args.dir))) | |
| target_folder = os.path.join(os.path.realpath(os.path.normpath(args.dir)), | |
| 'convert-to-{}'.format(args.dest)) | |
| print args.preview | |
| if not os.path.exists(target_folder): | |
| os.makedirs(target_folder) | |
| for file in sorted(os.listdir(source_folder)): | |
| if file.startswith('.'): | |
| continue | |
| name, ext = os.path.splitext(file) | |
| if not MARI_REGEX.findall(name): | |
| continue | |
| #print 'converting {}'.format(name) | |
| if ext not in ['.jpg', '.png', '.exr', '.tiff', '.tif', '.dpx']: | |
| continue | |
| new_name = rename_texture(name, 'mari', 'mudbox') | |
| if new_name is None: | |
| continue | |
| print '{o} --> {d}'.format(o=name, d=new_name) | |
| if not args.preview: | |
| source_path = os.path.join(source_folder, name) + ext | |
| destination_path = os.path.join(target_folder, new_name) + ext | |
| if os.path.exists(destination_path): | |
| print 'skipping copy since destination path exists, {}'.format( | |
| destination_path) | |
| continue | |
| # print 'copying from {} to {}'.format(source_path, destination_path) | |
| try: | |
| shutil.copyfile(source_path, destination_path) | |
| except OSError, e: | |
| print 'could not copy, error message: {}'.format(e) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment