| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | import bpy | 
					
						
							|  |  |  | import sys, os | 
					
						
							| 
									
										
										
										
											2009-09-04 01:33:22 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | import http, http.client, http.server, urllib | 
					
						
							|  |  |  | import subprocess, shutil, time, hashlib | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | import netrender.model | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | VERSION = b"0.3" | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | QUEUED = 0 | 
					
						
							|  |  |  | DISPATCHED = 1 | 
					
						
							|  |  |  | DONE = 2 | 
					
						
							|  |  |  | ERROR = 3 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def clientConnection(scene): | 
					
						
							|  |  |  | 		netrender = scene.network_render | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		conn = http.client.HTTPConnection(netrender.server_address, netrender.server_port) | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if clientVerifyVersion(conn): | 
					
						
							|  |  |  | 			return conn | 
					
						
							|  |  |  | 		else: | 
					
						
							|  |  |  | 			conn.close() | 
					
						
							|  |  |  | 			return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def clientVerifyVersion(conn): | 
					
						
							|  |  |  | 	conn.request("GET", "version") | 
					
						
							|  |  |  | 	response = conn.getresponse() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if response.status != http.client.OK: | 
					
						
							|  |  |  | 		conn.close() | 
					
						
							|  |  |  | 		return False | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	server_version = response.read() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if server_version != VERSION: | 
					
						
							|  |  |  | 		print("Incorrect server version!") | 
					
						
							|  |  |  | 		print("expected", VERSION, "received", server_version) | 
					
						
							|  |  |  | 		return False | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def clientSendJob(conn, scene, anim = False, chunks = 5): | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 	netsettings = scene.network_render | 
					
						
							|  |  |  | 	job = netrender.model.RenderJob() | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if anim: | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 		for f in range(scene.start_frame, scene.end_frame + 1): | 
					
						
							|  |  |  | 			job.addFrame(f) | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	else: | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 		job.addFrame(scene.current_frame) | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	filename = bpy.data.filename | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 	job.files.append(filename) | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-02 00:07:55 +00:00
										 |  |  | 	job_name = netsettings.job_name | 
					
						
							|  |  |  | 	path, name = os.path.split(filename) | 
					
						
							|  |  |  | 	if job_name == "[default]": | 
					
						
							|  |  |  | 		job_name = name | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-02 00:07:55 +00:00
										 |  |  | 	for lib in bpy.data.libraries: | 
					
						
							|  |  |  | 		lib_path = lib.filename | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if lib_path.startswith("//"): | 
					
						
							|  |  |  | 			lib_path = path + os.sep + lib_path[2:] | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		job.files.append(lib_path) | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-04 01:33:22 +00:00
										 |  |  | 	root, ext = os.path.splitext(name) | 
					
						
							|  |  |  | 	cache_path = path + os.sep + "blendcache_" + root + os.sep # need an API call for that | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	print("cache:", cache_path) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if os.path.exists(cache_path): | 
					
						
							|  |  |  | 		pattern = re.compile("[a-zA-Z0-9]+_([0-9]+)_[0-9]+\.bphys") | 
					
						
							|  |  |  | 		for cache_name in sorted(os.listdir(cache_path)): | 
					
						
							|  |  |  | 			match = pattern.match(cache_name) | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if match: | 
					
						
							|  |  |  | 				print("Frame:", int(match.groups()[0]), cache_name) | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			job.files.append(cache_path + cache_name) | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	#print(job.files) | 
					
						
							| 
									
										
										
										
											2009-09-02 00:07:55 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	job.name = job_name | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	for slave in scene.network_render.slaves_blacklist: | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 		job.blacklist.append(slave.id) | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 	job.chunks = netsettings.chunks | 
					
						
							|  |  |  | 	job.priority = netsettings.priority | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	# try to send path first | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 	conn.request("POST", "job", repr(job.serialize())) | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	response = conn.getresponse() | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 	job_id = response.getheader("job-id") | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-02 00:07:55 +00:00
										 |  |  | 	# if not ACCEPTED (but not processed), send files | 
					
						
							|  |  |  | 	if response.status == http.client.ACCEPTED: | 
					
						
							|  |  |  | 		for filepath in job.files: | 
					
						
							|  |  |  | 			f = open(filepath, "rb") | 
					
						
							|  |  |  | 			conn.request("PUT", "file", f, headers={"job-id": job_id, "job-file": filepath}) | 
					
						
							|  |  |  | 			f.close() | 
					
						
							|  |  |  | 			response = conn.getresponse() | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | 	# server will reply with NOT_FOUD until all files are found | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return job_id | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def clientRequestResult(conn, scene, job_id): | 
					
						
							|  |  |  | 	conn.request("GET", "render", headers={"job-id": job_id, "job-frame":str(scene.current_frame)}) | 
					
						
							| 
									
										
										
										
											2009-09-02 00:07:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def prefixPath(prefix_directory, file_path, prefix_path): | 
					
						
							|  |  |  | 	if os.path.isabs(file_path): | 
					
						
							|  |  |  | 		# if an absolute path, make sure path exists, if it doesn't, use relative local path | 
					
						
							|  |  |  | 		full_path = file_path | 
					
						
							|  |  |  | 		if not os.path.exists(full_path): | 
					
						
							|  |  |  | 			p, n = os.path.split(full_path) | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if main_path and p.startswith(main_path): | 
					
						
							|  |  |  | 				directory = prefix_directory + p[len(main_path):] | 
					
						
							|  |  |  | 				full_path = directory + n | 
					
						
							|  |  |  | 				if not os.path.exists(directory): | 
					
						
							|  |  |  | 					os.mkdir(directory) | 
					
						
							|  |  |  | 			else: | 
					
						
							|  |  |  | 				full_path = prefix_directory + n | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		full_path = prefix_directory + file_path | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return full_path |