Advertisement
Guest User

Untitled

a guest
May 25th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import sys, os, configparser, codecs, re
  2. sys.argv=["Main"]
  3.  
  4. import tkinter as tk
  5. from tkinter.filedialog import asksaveasfilename
  6.  
  7. root = tk.Tk()
  8. root.withdraw()
  9.  
  10. proj = RPR_EnumProjects(-1, "", 512)[0]
  11. (proj, project_dir, buf_sz) = RPR_GetProjectPathEx(proj, "", 255)
  12.  
  13. config_path = os.path.join(project_dir, "tempo_mapping.ini")
  14. config = configparser.RawConfigParser()
  15. config.read(config_path)
  16.  
  17. def update_osu_path():
  18. file_path = asksaveasfilename(initialdir=project_dir,defaultextension=".osu", filetypes=[("osu! file",".osu")], title="Save Timing Points")
  19. if not file_path:
  20. RPR_ShowMessageBox("No file location provided!", "Export Error", 0)
  21. return False
  22. if not config.has_section('Export'):
  23. config.add_section('Export')
  24. config.set('Export', 'osu', file_path)
  25. with codecs.open(config_path, 'w', encoding='utf8') as f:
  26. config.write(f)
  27. return True
  28.  
  29. if not config.has_option('Export', 'osu'):
  30. update_osu_path()
  31.  
  32. def check_file_path():
  33. file_path = config.get('Export', 'osu')
  34. confirm = RPR_ShowMessageBox("Export to osu: \n" + file_path, "Export Confirm", 3)
  35. if confirm == 7: # No
  36. if update_osu_path():
  37. export(config.get('Export', 'osu'))
  38. elif confirm == 6: # Yes
  39. export(config.get('Export', 'osu'))
  40.  
  41. def timing_points_lines():
  42. global proj
  43. tempo_c = RPR_CountTempoTimeSigMarkers(proj)
  44. last_timesig_num = 0
  45. last_timesig_denom = 0
  46.  
  47. timing_points = []
  48.  
  49. for ptidx in range(0, tempo_c):
  50. (ok, proj, ptidx, timepos, measurepos, beatpos, bpm, timesig_num, timesig_denom, lineartempo) = RPR_GetTempoTimeSigMarker(proj, ptidx, 0, 0, 0, 0, 0, 0, 0)
  51. if timesig_num == 0:
  52. timesig_num = last_timesig_num
  53. else:
  54. last_timesig_num = timesig_num
  55. if timesig_denom == 0:
  56. timesig_denom = last_timesig_denom
  57. else:
  58. last_timesig_denom = timesig_denom
  59. timing_points.append("%d,%s,%d,2,0,100,1,0\n" % (int(timepos * 1000), (60000 / bpm), timesig_denom))
  60.  
  61. return timing_points
  62.  
  63. def export(file_path):
  64. with codecs.open(file_path, 'r', encoding='utf8') as f:
  65. lines = f.readlines()
  66. idxs = [ i for i, line in enumerate(lines) if line.startswith('[TimingPoints]') ]
  67. if any(idxs):
  68. first_part = lines[:idxs[0]+1]
  69. second_part = lines[idxs[0]+1:]
  70. idxs = [ i for i, line in enumerate(second_part) if re.search("^\[\w*\]", line) ]
  71. if any(idxs):
  72. second_part = second_part[idxs[0]:]
  73. lines = first_part + timing_points_lines() + ['\r\n', '\r\n', '\r\n'] + second_part
  74. with codecs.open(file_path, 'w', encoding='utf8') as f:
  75. f.writelines(lines)
  76.  
  77. check_file_path()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement