network_renders.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # THIS FILE IS A PART OF VCStudio
  2. # PYTHON 3
  3. ##############################################################################
  4. # Now you are probably asking yourself. Network? Render? How are these related?
  5. # Actually it's a complicated answer. So let's walk through the idea of the
  6. # Renderer implementation in VCStudio.
  7. # Why do we need renderer in the first place? Doesn't blender already has one?
  8. # Well yes. And I would not care if I was always sitting on a biffy, nice
  9. # machine. But back in 2016, 2017 when I was making I'm Not Even Human I realized
  10. # one very ennoying fact.
  11. # My lap top at the time could not manage the loads of the files. And while
  12. # rendering Blender would crash very often. Of course I was not dumb as was
  13. # rendering into image files and not straight into video. So I could start
  14. # rendering from the last frame.
  15. # Unfortunatly it was happening way too often. So I started looking at ways to
  16. # unload things from the memory so Blender would crash less. One of those ways
  17. # is rendering using a console. Type blender -b <filename> -a and it will launch
  18. # blender without UI saving some memory. Also I could do a simple script that
  19. # restores Blender when if it does crashes.
  20. # By the time of Blender-Organizer 4.0 I had a system of rendering that will
  21. # look into a folder and see if any file between start and end frame are missing
  22. # and render them. Instead of trying to render every single frame in sequense.
  23. # Also in Blender-Organizer 4.0 I made a very clever thing. I openned the
  24. # rendering not in a terminal, but rather in it's own process that is piped to
  25. # a little UI window. Where I show some quick analytics. Tho there was one
  26. # problem. Closing the window was closing the blender. Or if not there was no
  27. # way to cancel it. And it would continue till the end or till the crash.
  28. # So here since VCStudio is a complite re-write of Blender-Organizer I can try
  29. # to re-implement the renderer in a slightly better way.
  30. # I will run a script that recieves stuff from the pipe and has an ability to
  31. # kill the blender's process. And this script will have no UI. But will instead
  32. # use LOCAL NETWORK to talk to VCStudio. I'm using 127.0.0.1 for this so on
  33. # any normal system even without Internet connection it should work. Since this
  34. # IP adress simply means THIS COMPUTER.
  35. # I will probably implement some LOCAL NETWORK talk ability later for the
  36. # multiuser.
  37. # This file is collection of functions for Rendering network sub-system. The UI
  38. # are contained in studio folder. Render it self is a separate file.
  39. ##############################################################################
  40. import os
  41. import json
  42. import socket
  43. def read_renders(win):
  44. # This function will listen for 127.0.0.1 port 54545 for data about current
  45. # renders. NOTE: I'm not doing any encryption and will use UDP protocol.
  46. # so it's quite simple to mess arround with it. NO SECURITY. But I don't see
  47. # any use in hacking it appart from maybe making VCStudio think it's rendering
  48. # a different frame or something.
  49. # First thing. And you probably think that I'm crazy but bare with me. I
  50. # need to see if a active_renders.data exists. And will read from it on
  51. # each frame. I know. A bit of not cool. But to be honest. The language
  52. # files are also read at every frame. Yeah...
  53. if not os.path.exists(win.project+"/set/active_renders.data"):
  54. m = open(win.project+"/set/active_renders.data", "w")
  55. m.close()
  56. # Now we are going to read it on every frame to see that renders are still
  57. # there.
  58. r = open(win.project+"/set/active_renders.data")
  59. r = r.read()
  60. r = r.split("\n")
  61. filenames = []
  62. for filename in r:
  63. if filename:
  64. # So basically the file contains list of files currently placed for
  65. # rendering. The renderer will go one by one and when finised will
  66. # remove the filename from this file. Also to cancel the render the
  67. # filename should be removed. The rest will be done using a very
  68. # crappy UDP network protocol on localhost.
  69. # Now if you just openned the VCStudio. While maybe render were
  70. # doing their job somewhere on the background. You want to re-new
  71. # all of the data.
  72. # So...
  73. if filename not in win.renders:
  74. # We are going to read the JSON file of the render.
  75. folder = filename[:filename.rfind("/")]+"/extra"
  76. savefile = folder+filename[filename.rfind("/"):]+".json"
  77. # It might not exits so we are going to do this with try.
  78. try:
  79. with open(win.project+savefile) as json_file:
  80. data = json.load(json_file)
  81. win.renders[filename] = data
  82. except:
  83. pass
  84. # Now let's make a list of all lines anyway
  85. if filename not in filenames:
  86. filenames.append(filename)
  87. # Now let's remove any of them renders that are finished or otherwise
  88. # removed from the file.
  89. for stuff in list(win.renders.keys()):
  90. if stuff not in filenames:
  91. del win.renders[stuff]
  92. try:
  93. if stuff == win.current["renders_window"]["filename"]:
  94. win.current["renders_window"]["filename"] = ""
  95. except:
  96. pass
  97. # Now that we know about the data. Let's read the stream from the network
  98. string_read = ""
  99. try:
  100. UDP_IP = "127.0.0.1"
  101. UDP_PORT = 54545
  102. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  103. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  104. sock.bind((UDP_IP, UDP_PORT))
  105. # Now usually it will wait for a message untill one appears. But render
  106. # could be finished. Or some error might accur. Or it could be that no
  107. # render what so ever. So I don't want the UI to freez. So I'm going to
  108. # put a timeout. Very short timeout.
  109. sock.settimeout(0.005)
  110. # This comes with it's own problems. Mainly I tested I need to send
  111. # about 500 messages at ones for it to be recognized at all. But it's
  112. # not a big deal what so ever. Just need to keep in mind. 500 messages.
  113. data, addr = sock.recvfrom(1024)
  114. data = data.decode('utf8')
  115. sock.close()
  116. string_read = data
  117. except:
  118. pass
  119. # Now let's do something with this data we read from the stream.
  120. # We gonna complitelly ignore anything unrelated. Such as not from
  121. # the project.
  122. if "network_render_timeout_frame" not in win.current:
  123. win.current["network_render_timeout_frame"] = win.current["frame"]
  124. if string_read.startswith("VCStudio : "):
  125. if ": RENDERING :" in string_read:
  126. blend, blend_string = string_read.split(" : ")[2], string_read.split(" : ")[3]
  127. # So we've got 2 peaces of data from the renderer. Blender. Which
  128. # is a path to the blend file. Similar to the one in
  129. # active_renders.data file that we read previously.
  130. # And we've got a raw string that blender outputs during render.
  131. # Example:
  132. # Fra:70 Mem:93.09M (Peak 97.78M) | Time:00:01.83 | Rendering 26 / 64 samples
  133. # You can find a string like this in the top banner inside the blender.
  134. # From this string we can find out a bunch of things. But not everything is
  135. # that simple. Cycles and Eevee for exmple output different strings. Any
  136. # other, wild render engine will output it's own string.
  137. # Now I'd like to actually load the data from the JSON file at every step
  138. # like this. Because our rendering script will be recording and saving
  139. # render times of each frame into the file. I need it for analytics.
  140. if blend in win.renders:
  141. folder = blend[:blend.rfind("/")]+"/extra"
  142. savefile = folder+blend[blend.rfind("/"):]+".json"
  143. # It might not exits so we are going to do this with try.
  144. try:
  145. with open(win.project+savefile) as json_file:
  146. data = json.load(json_file)
  147. win.renders[blend] = data
  148. except:
  149. pass
  150. win.renders[blend]["rendering"] = blend_string
  151. # Next is let's try finding out the current frame rendering. It's
  152. # probably not that hard. Every string usually starts with Fra:<number>
  153. # So let's try doing it.
  154. frame = 0
  155. try:
  156. frame = int(blend_string[4:blend_string.find(" ")])
  157. except:
  158. pass
  159. win.renders[blend]["current_frame"] = frame
  160. # The rest of it I will do in the UI.
  161. win.current["network_render_timeout_frame"] = win.current["frame"]
  162. # Now sometimes for no particular reason the whole rendering thing could crash
  163. # or in other words. If there is no data sent. We want to clean the renderings
  164. # and make it so the user sees that renders DO NOT render. For this there should
  165. # be a timeout system. Basically if in a given amount of frames there is no message
  166. # from the renderer script. We going to say that it's not rendering.
  167. if win.current["frame"] - win.current["network_render_timeout_frame"] > 100:
  168. for blend in win.renders:
  169. win.renders[blend]["rendering"] = False
  170. def stop_render(win):
  171. # This function will stop the rendering. It will bombard the renderer with
  172. # stop messages.
  173. for i in range(5000):
  174. cs1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  175. cs1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  176. cs1.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  177. cs1.sendto(bytes("VCStudio : RENDER STOP", 'utf-8'), ('127.0.0.1', 54545))