Connecting to the Shotgun Project Mangement system


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import sys
  2. from pprint import pprint
  3. #sys.path.append ('z:/software/scripts')
  4. # you need to either append the path to where the shotgun api is or
  5. #you need to add it to your PYTHONPATH as i have done at home
  6. #from getShotData import *
  7.  
  8. print ('Loading Shotgun Support')
  9.  
  10. URL = ""
  11. name = ""
  12. API = ""
  13.  
  14. # connect to shotgun
  15. def shotgunConnect():
  16. from shotgun_api3_preview import Shotgun
  17. sg = Shotgun(URL,name,API)
  18. return sg
  19.  
  20. # find a project given the name
  21. def sgProject (sg, input):
  22. return sg.find_one("Project", [["name", "is", input]] )
  23.  
  24. # finding a shot given the name :
  25. def sgShot (sg, shot, project):
  26. return sg.find_one('Shot',[['code','is',shot],['project','is',project]],['sg_cut_in','sg_cut_out','sg_client_version', 'code'])
  27.  
  28.  
  29. # expects a project in the proper naming, and a shot
  30. def createShot (sg, proj, shot):
  31. filters = [['code','is','CGI / Live Action Shot' ]]
  32. template = sg.find_one('TaskTemplate',filters)
  33. project = sgProject(proj)
  34.  
  35. data = { 'project': {"type":"Project","id": project['id']},
  36. 'code': shot,
  37. 'task_template' : template,
  38. 'description': '',
  39. 'sg_status_list': 'wtg' }
  40. result = sg.create('Shot', data)
  41. return result
  42.  
  43.  
  44.  
  45. # find multiple notes given a shot ID (from above)
  46. # order in newest to oldest
  47. # requires you call it like note[0]['content']
  48. def sgNotesFind (sg, shotID):
  49. print shotID
  50. note = sg.find('Note',[['note_links','is', shotID]],['subject','content', 'created_at'],[{'field_name':'created_at','direction':'desc'}])
  51. return note
  52.  
  53. # find a single (most recent) note given a shot ID
  54. # then call it like note['content']
  55. def sgNotesFindLatest (sg, shotID):
  56. note = sg.find_one('Note',[['note_links','is', shotID][0]],['subject','content','created_at'],[{'field_name':'created_at','direction':'desc'}])
  57. return note
  58.  
  59.  
  60. def sgCreateNote(sg, project, shotID, subject, content):
  61. # enter data here for a note to create
  62. data = {'subject':subject,'content':content,'note_links':[shotID],'project':project}
  63. # create the note
  64. noteID = sg.create('Note',data)
  65. return noteID
  66.  
  67. # create a version
  68. def sgCreateVersion(sg, project, shotID, verName, description, framePath, firstFrame, lastFrame, clientName=''):
  69.  
  70. data = {'project': project,
  71. 'code': verName,
  72. 'description': description,
  73. 'sg_path_to_frames': framePath,
  74. 'frame_range': firstFrame + '-' + lastFrame,
  75. #'sg_uploaded_movie': '/Users/throb/Downloads/test.m4v',
  76. #'sg_first_frame': 1,
  77. #'sg_last_frame': 100,
  78. 'sg_status_list': 'rev',
  79. 'entity': shotID}
  80. # in case we're putting a client version in here we need this code.
  81. # we are expecting a field called sg_client_name in the version table.
  82. # please make sure you create this in the shotgun setup
  83. if clientName != '' :
  84. data['sg_client_name'] = clientName
  85.  
  86. #'user': {'type':'HumanUser', 'id':165} }
  87. return sg.create('Version',data)
  88.  
  89. #add a task version to the system
  90. def sgCreateVersionTask(sg, project, shotID, verName, description, framePath, firstFrame, lastFrame, task):
  91. filters = [['content','is',task],['entity','is',shot]]
  92. taskID = sg.find_one('Task',filters)
  93. data = {'project': project,
  94. 'code': verName,
  95. 'description': description,
  96. 'sg_path_to_frames': framePath,
  97. 'frame_range': firstFrame + '-' + lastFrame,
  98. #'sg_uploaded_movie': '/Users/throb/Downloads/test.m4v',
  99. #'sg_first_frame': 1,
  100. #'sg_last_frame': 100,
  101. 'sg_status_list': 'rev',
  102. 'sg_task': taskID,
  103.  
  104. 'entity': shotID}
  105. # in case we're putting a client version in here we need this code.
  106. # we are expecting a field called sg_client_name in the version table.
  107. # please make sure you create this in the shotgun setup
  108. #'user': {'type':'HumanUser', 'id':165} }
  109. return sg.create('Version',data)
  110.  
  111. # look for versions in a shot:
  112. def sgVersionFind(sg, shotID):
  113. return sg.find('Version',[['entity','is',shotID]],['code','task','sg_path_to_frames'],[{'field_name':'created_at','direction':'desc'}])
  114.  
  115. def sgVersionFindLatest(sg, shotID):
  116. return sg.find_one('Version',[['entity','is',shotID]],['code','task','sg_path_to_frames'],[{'field_name':'created_at','direction':'desc'}])
  117.  
  118. # search for the latest task given shotID and task info
  119. def sgVersionFindLatestTask(sg, shotID, task):
  120. # first look for the task and get the ID
  121. filters = [['content','is',task],['entity','is',shotID]]
  122. taskID = sg.find_one('Task',filters)
  123. # then look for the latest
  124. #version using the task ID. note that we need to use the [0] or else we're sending the array versus the hash
  125. versionLatest = sg.find_one('Version',[['entity','is',shotID],['sg_task','is',taskID]],['code','sg_task','sg_path_to_frames'],[{'field_name':'created_at','direction':'desc'}])
  126. return versionLatest
  127.  
  128. ## The following requires a field called "client_version" be added to shotgun
  129. def sgVersionClientUpate (sg, shotID, version):
  130. data = { 'sg_client_version': version}
  131. result = sg.update('Shot', shotID['id'], data)
  132. return result
  133.  
  134. # Upload a file to shotgun in the 'sg_qt' field.
  135. # If you do this for a shot, you will need to add this to a field
  136. # Shotgun provides the sg_qt for versions automatically
  137. def sgQTUpload (sg,entity,itemID,path):
  138. # Make sure first letter is capitalized
  139. entity = entity[:1].upper() + entity[1:]
  140. # upload that mother
  141. result = sg.upload (entity, itemID['id'], path,'sg_qt')
  142. return results
  143.  
  144. # connect to shotgun and get the latest "client version" number
  145. def sgGetClientVersion (shotgunShot):
  146. try :
  147. currentVersion = shotgunShot['sg_client_version']
  148. except :
  149. currentVersion = 0
  150. if currentVersion == None:
  151. return 0
  152. return currentVersion

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.