· 5 years ago · Aug 21, 2020, 05:42 PM
1import os
2import json
3import requests
4import sys
5import getpass
6import shutil
7import os.path
8import zipfile
9
10
11# HARD CODED STUFF USER WILL NEED TO CHANGE
12scratch = "C:\\Users\\mpate\\OneDrive\\Desktop\\tmp"
13exr_writer = "C:\\Users\\mpate\\.nuke\\tarantula\\artist_nuke_submit_EXRs.nk"
14
15
16#Gets the API key from the user
17def getAPIkey():
18 user = os.path.expanduser("~")
19 user = user + "\\AppData\\Local\\Thorax\\"
20
21
22 config = user + 'artist_config.json'
23
24
25 if os.path.exists(user):
26 print "this directory exists. moving on!"
27 else:
28 os.makedirs(user)
29 print "created " + user
30
31
32
33 if os.path.exists(config):
34
35 with open(config) as i:
36 data = json.load(i)
37
38 return data['api_key']
39
40 else:
41
42 p = nuke.Panel('api key')
43 p.addSingleLineInput('enter your api key', '')
44 ret = p.show()
45 api = p.value()
46
47 with open(config, 'w') as jsonFile:
48
49 empty_json = '{}'
50 api_lib = {"api_key":api}
51 api_json_object = json.loads(empty_json)
52 api_json_object.update(api_lib)
53 json.dump(api_json_object, jsonFile)
54 jsonFile.close()
55 print "api key stored!"
56
57print getAPIkey()
58
59#GETS INFO ABOUT THE CURRENT ARTIST FROM THE ONLINE DATABASE
60def getInfo(api):
61 data = {'api_key': api}
62 url = 'https://trn.la/api/artist/shots'
63 session = requests.Session()
64 r = session.post(url, data=data)
65 result = json.loads(json.dumps(r.json()))
66
67
68 if result["class"] == "success":
69 print "YOUR SHOT IS READY TO UPLOAD!...Proceed further."
70 return result
71 elif result["message"] == "Invalid API Key":
72 nuke.message("Please enter the correct API key.")
73 elif result["message"] == "you have no working shots":
74 nuke.message("Oops! You are not currently assigned any shot.")
75 else:
76 nuke.message("Something went wrong. Please try again later.")
77
78 r.raw.close()
79
80print getInfo(getAPIkey())
81
82
83#CREATES PANEL FOR ARTIST
84
85def panel(info):
86
87 p = nuke.Panel('upload: ' + info['shots']['shot_name'])
88 p.addBooleanCheckBox(info['shots']['tarantula_note'], True)
89 p.addMultilineTextInput('note to the admin!', '')
90 ret = p.show()
91 print p.value('note to the admin!')
92
93 if p.value(info["shots"]["tarantula_note"]) == False:
94 nuke.message("Please confirm you've addressed the note: \n\n" + info["shots"]["tarantula_note"])
95 return False
96
97panel(getInfo(getAPIkey()))
98
99
100#GETS INFO ABOUT THE READ NODE SO THAT THE PROGRAM KNOWS WHAT TO UPLOAD
101def getReadInfo(x):
102 dwaa = exr = bitsPerChannel = False
103
104 n = nuke.selectedNode()
105 file = n['file'].value().split('/')[-1]
106 file = file.split(".")[0]
107 first_frame = n["first"].value()
108 last_frame = n["last"].value()
109 node_name = n["name"].value()
110
111 #does not require user to rerender
112 # 0 = file // 1 = first_frame // 2 = last_frame // 3 = node_name
113 return [file, first_frame, last_frame, node_name]
114getReadInfo(getInfo(getAPIkey()))
115
116
117#COPIES THE CORRECT READ NODE AND RENDERS USING THAT READ NODE
118def renderEXRs(y):
119 info = getReadInfo(getInfo(getAPIkey()))
120 scratch_dir = scratch
121
122 if os.path.exists(scratch_dir + '\\' + info[0] ):
123 pass
124 else:
125 os.mkdir(scratch_dir + '\\' + info[0] )
126
127 output_destination = scratch_dir + '\\' + info[0] + '\\' + info[0] + "_%04d.exr"
128
129 #EXR WRITER
130 nuke.nodePaste(exr_writer)
131
132 n = nuke.selectedNode()
133 n["file"].setValue(output_destination)
134 exr_write_node = n['name'].getValue()
135
136 try:
137 nuke.execute(exr_write_node, info[1], info[2])
138 except:
139 print "something went wrong"
140
141 #deletes write nodes
142 nuke.delete(nuke.toNode(exr_write_node))
143
144 #creates zip file
145 print scratch_dir + '\\' + info[0]
146 zip = zipfile.ZipFile(scratch_dir + '\\' + info[0] + '.zip', mode='w', allowZip64 = True)
147 #shutil.make_archive(scratch_dir + '/' + info[0], 'zip', scratch_dir)
148
149 nuke.toNode(info[3]).setSelected(True)
150
151 return scratch_dir + '\\' + info[0] + ".zip"
152
153
154
155
156#UPLOADS
157def upload(api):
158
159 i = getInfo(api)
160 shot_note = panel(i)
161 readInfo = getReadInfo(getInfo(getAPIkey()))
162 scratch_dir = scratch + "/scratch"
163
164 #THIS IS WHERE THE RENDER FUNCTION IS CALLED
165 zip_file = renderEXRs(getReadInfo(getInfo(getAPIkey())))
166
167 url = "https://trn.la/api/artist/submit_work"
168
169 data = {"api_key": api,
170 "shot_work_id": i["shots"]["work_id"],
171 "artist_note": shot_note
172 }
173
174 files = {
175 'shot_file': open(zip_file, 'rb')
176 }
177
178 session = requests.Session()
179 r = session.post(url, data=data, files=files, stream=True)
180
181 print r.json()
182 return r.json()
183
184 r.raw.close()
185
186
187if len(nuke.selectedNodes()) == 1:
188 x = upload(getAPIkey())
189 if x["success"] == False:
190 nuke.message("Submission Failed. Have you already submitted this shot?")
191 elif x["success"] == True:
192 nuke.message('Submitted to Tarantula')
193else:
194 nuke.message('please select one read node to upload')
195