Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. import os
  2. import argparse
  3. import socket
  4. import tempfile
  5.  
  6. from avalon import io, api
  7. from launcher import lib
  8.  
  9.  
  10. def wait_for_maya_boot():
  11. # Create a TCP/IP socket
  12. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.  
  14. # Bind the socket to the port
  15. server_address = ("localhost", 20000)
  16. print("Starting up on {}".format(server_address))
  17. sock.bind(server_address)
  18.  
  19. sock.settimeout(120)
  20.  
  21. # Listen for incoming connections
  22. sock.listen(1)
  23.  
  24. while True:
  25. # Wait for a connection
  26. print("Waiting for a connection to Maya.")
  27. connection, client_address = sock.accept()
  28. break
  29.  
  30.  
  31. def send_to_maya(cmd, connection):
  32. print("Sending: {}".format(repr(cmd)))
  33. connection.send(cmd.encode())
  34. data = connection.recv(4096)
  35. print(data)
  36.  
  37.  
  38. def read_maya_script_editor_output(maya_logging_file, previous_log):
  39. with open(maya_logging_file) as f:
  40. log = f.read().replace(previous_log, "")
  41. print(log)
  42. previous_log += log
  43.  
  44. return previous_log
  45.  
  46.  
  47. def main(silo, shots, task, application_name):
  48. # Maya logging.
  49. maya_logging_directory = tempfile.mkdtemp()
  50. maya_logging_file = os.path.join(
  51. maya_logging_directory, "maya_script_editor_output.txt"
  52. )
  53. os.environ["MAYA_CMD_FILE_OUTPUT"] = maya_logging_file
  54. previous_log = ""
  55.  
  56. io.install()
  57.  
  58. apps = lib.get_apps(io.find_one({"type": "project"}))
  59. application = None
  60. for App in apps:
  61. if App.name == application_name:
  62. application = App()
  63.  
  64. session = api.Session
  65. session["AVALON_SILO"] = silo
  66. session["AVALON_TASK"] = task
  67. session["WORKFILES_STARTUP"] = ""
  68. os.environ["PYTHONPATH"] += os.pathsep + os.path.dirname(__file__)
  69. for shot in shots:
  70. session["AVALON_ASSET"] = shot
  71. session["AVALON_HIERARCHY"] = "/".join(
  72. io.find_one({"type": "asset", "name": shot})["data"]["parents"]
  73. )
  74. template = io.find_one({"type": "project"})["config"]["template"]
  75. session["AVALON_WORKDIR"] = template["work"].format(
  76. root=session["AVALON_PROJECTS"],
  77. project={"name": session["AVALON_PROJECT"]},
  78. hierarchy=session["AVALON_HIERARCHY"],
  79. asset=shot,
  80. task=task
  81. )
  82.  
  83. application.process(session.copy())
  84.  
  85. print("Waiting until maya is ready to go.")
  86. wait_for_maya_boot()
  87. previous_log = read_maya_script_editor_output(
  88. maya_logging_file, previous_log
  89. )
  90.  
  91. # Establish connection.
  92. connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  93. connection.connect(("localhost", 7777))
  94.  
  95. # Ensure no unsaved changes dialog appears.
  96. cmd = "from maya import cmds;cmds.file(modified=False)"
  97. send_to_maya(cmd, connection)
  98. previous_log = read_maya_script_editor_output(
  99. maya_logging_file, previous_log
  100. )
  101.  
  102. # Open workfile.
  103. cmd = (
  104. "from avalon.tools import workfiles;"
  105. "from avalon import api;"
  106. "host = api.registered_host();"
  107. "window = workfiles.app.Window(host.work_root());"
  108. "window.on_open_pressed()"
  109. )
  110. send_to_maya(cmd, connection)
  111. previous_log = read_maya_script_editor_output(
  112. maya_logging_file, previous_log
  113. )
  114.  
  115. # Ensure imageplane refreshes
  116. cmd = (
  117. "import pymel.core as pm;"
  118. "pm.lookThru(pm.PyNode(\"reviewMain\").members()[0])"
  119. )
  120. send_to_maya(cmd, connection)
  121. previous_log = read_maya_script_editor_output(
  122. maya_logging_file, previous_log
  123. )
  124.  
  125. # Publish.
  126. cmd = "import pyblish.util;pyblish.util.publish()"
  127. send_to_maya(cmd, connection)
  128. previous_log = read_maya_script_editor_output(
  129. maya_logging_file, previous_log
  130. )
  131.  
  132. # Close Maya
  133. cmd = "from maya import cmds;cmds.quit(force=True)"
  134. send_to_maya(cmd, connection)
  135.  
  136.  
  137. if __name__ == "__main__":
  138. parser = argparse.ArgumentParser(description="Batch publish.")
  139. parser.add_argument(
  140. "--silo",
  141. dest="silo",
  142. action="store",
  143. required=True,
  144. help="Silo to find shots."
  145. )
  146. parser.add_argument(
  147. "--shots",
  148. dest="shots",
  149. nargs="+",
  150. required=True,
  151. help="Shots to publish."
  152. )
  153. parser.add_argument(
  154. "--task",
  155. dest="task",
  156. action="store",
  157. required=True,
  158. help="Task to publish."
  159. )
  160. parser.add_argument(
  161. "--application",
  162. dest="application",
  163. action="store",
  164. required=True,
  165. help="Task to publish."
  166. )
  167.  
  168. args = vars(parser.parse_args())
  169. main(args["silo"], args["shots"], args["task"], args["application"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement