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