soc-2008-mxcurioni: merged changes to revision 23516
[blender.git] / release / scripts / io / netrender / operators.py
1 import bpy
2 import sys, os
3 import http, http.client, http.server, urllib, socket
4 import webbrowser
5
6 from netrender.utils import *
7 import netrender.client as client
8 import netrender.model
9
10 @rnaOperator
11 class RENDER_OT_netclientanim(bpy.types.Operator):
12         '''
13         Operator documentation text, will be used for the operator tooltip and python docs.
14         '''
15         __idname__ = "render.netclientanim"
16         __label__ = "Net Render Client Anim"
17         
18         # List of operator properties, the attributes will be assigned
19         # to the class instance from the operator settings before calling.
20         
21         __props__ = []
22         
23         def poll(self, context):
24                 return True
25         
26         def execute(self, context):
27                 scene = context.scene
28                 
29                 conn = clientConnection(scene)
30                 
31                 if conn:
32                         # Sending file
33                         scene.network_render.job_id = client.clientSendJob(conn, scene, True)
34                         conn.close()
35                 
36                 bpy.ops.screen.render('INVOKE_AREA', animation=True)
37                 
38                 return ('FINISHED',)
39         
40         def invoke(self, context, event):
41                 return self.execute(context)
42
43 @rnaOperator
44 class RENDER_OT_netclientsend(bpy.types.Operator):
45         '''
46         Operator documentation text, will be used for the operator tooltip and python docs.
47         '''
48         __idname__ = "render.netclientsend"
49         __label__ = "Net Render Client Send"
50         
51         # List of operator properties, the attributes will be assigned
52         # to the class instance from the operator settings before calling.
53         
54         __props__ = []
55         
56         def poll(self, context):
57                 return True
58         
59         def execute(self, context):
60                 scene = context.scene
61                 
62                 conn = clientConnection(scene)
63                 
64                 if conn:
65                         # Sending file
66                         scene.network_render.job_id = client.clientSendJob(conn, scene, True)
67                         conn.close()
68                 
69                 return ('FINISHED',)
70         
71         def invoke(self, context, event):
72                 return self.execute(context)
73
74 @rnaOperator
75 class RENDER_OT_netclientstatus(bpy.types.Operator):
76         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
77         __idname__ = "render.netclientstatus"
78         __label__ = "Net Render Client Status"
79         
80         # List of operator properties, the attributes will be assigned
81         # to the class instance from the operator settings before calling.
82         
83         __props__ = []
84         
85         def poll(self, context):
86                 return True
87         
88         def execute(self, context):
89                 netsettings = context.scene.network_render
90                 conn = clientConnection(context.scene)
91
92                 if conn:
93                         conn.request("GET", "/status")
94                         
95                         response = conn.getresponse()
96                         print( response.status, response.reason )
97                         
98                         jobs = (netrender.model.RenderJob.materialize(j) for j in eval(str(response.read(), encoding='utf8')))
99                         
100                         while(len(netsettings.jobs) > 0):
101                                 netsettings.jobs.remove(0)
102                         
103                         bpy.data.netrender_jobs = []
104                         
105                         for j in jobs:
106                                 bpy.data.netrender_jobs.append(j)
107                                 netsettings.jobs.add()
108                                 job = netsettings.jobs[-1]
109                                 
110                                 j.results = j.framesStatus() # cache frame status
111                                 
112                                 job.name = j.name
113                 
114                 return ('FINISHED',)
115         
116         def invoke(self, context, event):
117                 return self.execute(context)
118
119 @rnaOperator
120 class RENDER_OT_netclientblacklistslave(bpy.types.Operator):
121         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
122         __idname__ = "render.netclientblacklistslave"
123         __label__ = "Net Render Client Blacklist Slave"
124         
125         # List of operator properties, the attributes will be assigned
126         # to the class instance from the operator settings before calling.
127         
128         __props__ = []
129         
130         def poll(self, context):
131                 return True
132         
133         def execute(self, context):
134                 netsettings = context.scene.network_render
135                 
136                 if netsettings.active_slave_index >= 0:
137                         
138                         # deal with data
139                         slave = bpy.data.netrender_slaves.pop(netsettings.active_slave_index)
140                         bpy.data.netrender_blacklist.append(slave)
141                         
142                         # deal with rna
143                         netsettings.slaves_blacklist.add()
144                         netsettings.slaves_blacklist[-1].name = slave.name
145                         
146                         netsettings.slaves.remove(netsettings.active_slave_index)
147                         netsettings.active_slave_index = -1
148                 
149                 return ('FINISHED',)
150         
151         def invoke(self, context, event):
152                 return self.execute(context)
153
154 @rnaOperator
155 class RENDER_OT_netclientwhitelistslave(bpy.types.Operator):
156         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
157         __idname__ = "render.netclientwhitelistslave"
158         __label__ = "Net Render Client Whitelist Slave"
159         
160         # List of operator properties, the attributes will be assigned
161         # to the class instance from the operator settings before calling.
162         
163         __props__ = []
164         
165         def poll(self, context):
166                 return True
167         
168         def execute(self, context):
169                 netsettings = context.scene.network_render
170                 
171                 if netsettings.active_blacklisted_slave_index >= 0:
172                         
173                         # deal with data
174                         slave = bpy.data.netrender_blacklist.pop(netsettings.active_blacklisted_slave_index)
175                         bpy.data.netrender_slaves.append(slave)
176                         
177                         # deal with rna
178                         netsettings.slaves.add()
179                         netsettings.slaves[-1].name = slave.name
180                         
181                         netsettings.slaves_blacklist.remove(netsettings.active_blacklisted_slave_index)
182                         netsettings.active_blacklisted_slave_index = -1
183                 
184                 return ('FINISHED',)
185         
186         def invoke(self, context, event):
187                 return self.execute(context)
188
189
190 @rnaOperator
191 class RENDER_OT_netclientslaves(bpy.types.Operator):
192         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
193         __idname__ = "render.netclientslaves"
194         __label__ = "Net Render Client Slaves"
195         
196         # List of operator properties, the attributes will be assigned
197         # to the class instance from the operator settings before calling.
198         
199         __props__ = []
200         
201         def poll(self, context):
202                 return True
203         
204         def execute(self, context):
205                 netsettings = context.scene.network_render
206                 conn = clientConnection(context.scene)
207                 
208                 if conn:
209                         conn.request("GET", "/slaves")
210                         
211                         response = conn.getresponse()
212                         print( response.status, response.reason )
213                         
214                         slaves = (netrender.model.RenderSlave.materialize(s) for s in eval(str(response.read(), encoding='utf8')))
215                         
216                         while(len(netsettings.slaves) > 0):
217                                 netsettings.slaves.remove(0)
218                         
219                         bpy.data.netrender_slaves = []
220                         
221                         for s in slaves:
222                                 for i in range(len(bpy.data.netrender_blacklist)):
223                                         slave = bpy.data.netrender_blacklist[i]
224                                         if slave.id == s.id:
225                                                 bpy.data.netrender_blacklist[i] = s
226                                                 netsettings.slaves_blacklist[i].name = s.name
227                                                 break
228                                 else:
229                                         bpy.data.netrender_slaves.append(s)
230                                         
231                                         netsettings.slaves.add()
232                                         slave = netsettings.slaves[-1]
233                                         slave.name = s.name
234                 
235                 return ('FINISHED',)
236         
237         def invoke(self, context, event):
238                 return self.execute(context)
239
240 @rnaOperator
241 class RENDER_OT_netclientcancel(bpy.types.Operator):
242         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
243         __idname__ = "render.netclientcancel"
244         __label__ = "Net Render Client Cancel"
245         
246         # List of operator properties, the attributes will be assigned
247         # to the class instance from the operator settings before calling.
248         
249         __props__ = []
250         
251         def poll(self, context):
252                 netsettings = context.scene.network_render
253                 return netsettings.active_job_index >= 0 and len(netsettings.jobs) > 0
254                 
255         def execute(self, context):
256                 netsettings = context.scene.network_render
257                 conn = clientConnection(context.scene)
258                 
259                 if conn:
260                         job = bpy.data.netrender_jobs[netsettings.active_job_index]
261                         
262                         conn.request("POST", "/cancel", headers={"job-id":job.id})
263                         
264                         response = conn.getresponse()
265                         print( response.status, response.reason )
266
267                         netsettings.jobs.remove(netsettings.active_job_index)
268                 
269                 return ('FINISHED',)
270         
271         def invoke(self, context, event):
272                 return self.execute(context)
273         
274 @rnaOperator
275 class RENDER_OT_netclientcancelall(bpy.types.Operator):
276         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
277         __idname__ = "render.netclientcancelall"
278         __label__ = "Net Render Client Cancel All"
279         
280         # List of operator properties, the attributes will be assigned
281         # to the class instance from the operator settings before calling.
282         
283         __props__ = []
284         
285         def poll(self, context):
286                 return True
287                 
288         def execute(self, context):
289                 netsettings = context.scene.network_render
290                 conn = clientConnection(context.scene)
291                 
292                 if conn:
293                         conn.request("POST", "/clear")
294                         
295                         response = conn.getresponse()
296                         print( response.status, response.reason )
297                 
298                         while(len(netsettings.jobs) > 0):
299                                 netsettings.jobs.remove(0)
300
301                 return ('FINISHED',)
302         
303         def invoke(self, context, event):
304                 return self.execute(context)
305
306 @rnaOperator
307 class netclientdownload(bpy.types.Operator):
308         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
309         __idname__ = "render.netclientdownload"
310         __label__ = "Net Render Client Download"
311         
312         # List of operator properties, the attributes will be assigned
313         # to the class instance from the operator settings before calling.
314         
315         __props__ = []
316         
317         def poll(self, context):
318                 netsettings = context.scene.network_render
319                 return netsettings.active_job_index >= 0 and len(netsettings.jobs) > 0
320                 
321         def execute(self, context):
322                 netsettings = context.scene.network_render
323                 rd = context.scene.render_data
324                 
325                 conn = clientConnection(context.scene)
326                 
327                 if conn:
328                         job = bpy.data.netrender_jobs[netsettings.active_job_index]
329                         
330                         for frame in job.frames:
331                                 client.requestResult(conn, job.id, frame.number)
332                                 response = conn.getresponse()
333                 
334                                 if response.status != http.client.OK:
335                                         print("missing", frame.number)
336                                         continue
337                                 
338                                 print("got back", frame.number)
339                                 
340                                 f = open(netsettings.path + "%06d" % frame.number + ".exr", "wb")
341                                 buf = response.read(1024)
342                                 
343                                 while buf:
344                                         f.write(buf)
345                                         buf = response.read(1024)
346                                 
347                                 f.close()
348                         
349                         conn.close()
350                 
351                 return ('FINISHED',)
352         
353         def invoke(self, context, event):
354                 return self.execute(context)
355
356 @rnaOperator
357 class netclientscan(bpy.types.Operator):
358         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
359         __idname__ = "render.netclientscan"
360         __label__ = "Net Render Client Scan"
361         
362         # List of operator properties, the attributes will be assigned
363         # to the class instance from the operator settings before calling.
364         
365         __props__ = []
366         
367         def poll(self, context):
368                 return True
369                 
370         def execute(self, context):
371                 netsettings = context.scene.network_render
372                 
373                 try:
374                         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
375                         s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
376                         s.settimeout(30)
377         
378                         s.bind(('', 8000))
379                         
380                         buf, address = s.recvfrom(64)
381                         
382                         print("received:", buf)
383                         
384                         netsettings.server_address = address[0]
385                         netsettings.server_port = int(str(buf, encoding='utf8'))
386                 except socket.timeout:
387                         print("no server info")
388                 
389                 return ('FINISHED',)
390         
391         def invoke(self, context, event):
392                 return self.execute(context)
393
394 @rnaOperator
395 class netclientweb(bpy.types.Operator):
396         '''Operator documentation text, will be used for the operator tooltip and python docs.'''
397         __idname__ = "render.netclientweb"
398         __label__ = "Net Render Client Web"
399         
400         # List of operator properties, the attributes will be assigned
401         # to the class instance from the operator settings before calling.
402         
403         __props__ = []
404         
405         def poll(self, context):
406                 return True
407                 
408         def execute(self, context):
409                 netsettings = context.scene.network_render
410                 
411                 
412                 # open connection to make sure server exists
413                 conn = clientConnection(context.scene)
414                 
415                 if conn:
416                         conn.close()
417                         
418                         webbrowser.open("http://%s:%i" % (netsettings.server_address, netsettings.server_port))
419                 
420                 return ('FINISHED',)
421         
422         def invoke(self, context, event):
423                 return self.execute(context)