prepare_folder_for export.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import os
  2. import shutil
  3. def copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id):
  4. # Construct the path for the copied folder
  5. copied_folder_path = os.path.join(target_folder, new_folder_name)
  6. # Check if the target folder already exists
  7. if os.path.exists(copied_folder_path):
  8. print(f"Folder '{copied_folder_path}' already exists. Skipping.")
  9. return
  10. # Copy the source folder to the target folder
  11. shutil.copytree(source_folder, copied_folder_path)
  12. # Extract components from the new folder name
  13. components = new_folder_name.split("_")
  14. if len(components) != 4:
  15. raise ValueError(
  16. "new_folder_name must be in the format Brand_AssetName_Year_AssetNumber"
  17. )
  18. brand = components[0]
  19. asset_name = components[1]
  20. year = components[2]
  21. asset_number = components[3]
  22. # Construct the new .blend file name
  23. new_blend_file_name = (
  24. f"{brand}_{asset_name}_{year}_{content_id}_{asset_number}.blend"
  25. )
  26. # Rename the .blend file inside the copied folder
  27. blend_folder_path = os.path.join(copied_folder_path, "BLEND")
  28. for file_name in os.listdir(blend_folder_path):
  29. if file_name.endswith(".blend"):
  30. old_blend_file_path = os.path.join(blend_folder_path, file_name)
  31. new_blend_file_path = os.path.join(blend_folder_path, new_blend_file_name)
  32. os.rename(old_blend_file_path, new_blend_file_path)
  33. break
  34. print(f"Copied and renamed folder to {copied_folder_path}")
  35. print(f"Renamed .blend file to {new_blend_file_name}")
  36. # Example usage
  37. source_folder = (
  38. r"Z:/01_Production_AP/01_Library/03_Elements/_Brand_AssetName_Year_AssetNumber"
  39. )
  40. target_folder = r"Z:/01_Production_AP/01_Library/03_Elements/IFR/Products"
  41. # new_folder_name = "AP_LRDSPE_2024_0001"
  42. content_id = "ALL_DT_MAT"
  43. folder_name_list = [
  44. "IFR_BJL_2023_ALL_DT_MAT_0001",
  45. "IFR_BJT_2023_ALL_DT_MAT_0001",
  46. "IFR_BTPC_2023_ALL_DT_MAT_0001",
  47. "IFR_BTE_2024_ALL_DT_MAT_0001",
  48. "IFR_BTPE_2023_ALL_DT_MAT_0001",
  49. "IFR_BTEC_2023_ALL_DT_MAT_0001",
  50. "IFR_BTT_2024_ALL_DT_MAT_0001",
  51. "IFR_CPAC_2024_ALL_DT_MAT_0001",
  52. "IFR_CPAX_2023_ALL_DT_MAT_0002",
  53. "IFR_CPE_2024_ALL_DT_MAT_0001",
  54. "IFR_CPT_2024_ALL_DT_MAT_0001",
  55. "IFR_GTAF_2023_ALL_DT_MAT_0001",
  56. "IFR_GTC_2023_ALL_DT_MAT_0001",
  57. "IFR_GTL_2023_ALL_DT_MAT_0001",
  58. "IFR_GTHS_2023_ALL_DT_MAT_0001",
  59. "IFR_GTS_2023_ALL_DT_MAT_0001",
  60. "IFR_NSMP_2023_ALL_DT_MAT_0001",
  61. ]
  62. # loop through list, and remove _ALL_DT_MAT from the names
  63. for folder_name in folder_name_list:
  64. new_folder_name = folder_name.replace("_ALL_DT_MAT", "")
  65. copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)
  66. # copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)