From 41dd24a67c633a29da9871f568b19b872c6cadeb Mon Sep 17 00:00:00 2001 From: James Ball Date: Sat, 30 Apr 2022 18:51:56 +0100 Subject: [PATCH] Gracefully handle disconnects from Blender --- CHANGELOG.md | 4 ++ blender/osci_render.zip | Bin 1459 -> 1540 bytes blender/osci_render/__init__.py | 36 ++++++++++++------ pom.xml | 2 +- .../java/sh/ball/engine/ObjectServer.java | 2 +- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dc8e7f..46de787 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +- 1.22.1 + - Gracefully handle disconnects from both Blender and osci-render to prevent restarts! + + - 1.22.0 - Add support for externally rendered lines! - This means there is now Blender support! diff --git a/blender/osci_render.zip b/blender/osci_render.zip index 3bb2e98dd765ee005c34662bd600590c306c7c03..12270848c89ecaa78479d3ab5edd8cab7603e408 100644 GIT binary patch delta 1476 zcmV;#1v~n)3xo_9P)h>@6aWAK2mnZxo>Ulu7^QRt008e0kr*C-SZ!0|HW2>qU!nR- zoYok)>-9R!Fmqf8ce&Dp0j7M&Wjv0pBmzfrC7G8p{P*rkw&P$Yq&M{!VrlnT?Xxdh zGfhLrXM!A)XNSP!@@PrjBjO4bvpq?9LZ$25tti(EA#w8?2r2S8N>kS-cJrP}#RP|4 z@6aa)K6%$I)TXI_MS-37J~^!Iq#};ARdWAg^x^$!X)2*AmMkkbPm)A%q6JX^Mdlgh zF-yrznqvqsje9L8euyMze2x&8^zWQW5RXUPg9XCDM##o7sp}COh`?p*;DlsvC0=E z;ybF#w_7E6`5QnVnRaGUEKxg%vMgAD2Px1_3YiQ;pfnhBoUfy+YY3{LI9&*(UFTwS ziAx_2fBX2+anKaJ4F#u;M|Bhk?AtoRM%8&L zAU#Ytt2H(=?}RBs`brp|nu$O_93w5HR~$R2Qwg?zxXDz4CXd~oeSe+*-3pzi>W6Km zLeZ5ae5)HF#aXaMyK5H8d}=v(Er~{j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3Y)ukh&Y!LRS$ z1p$J??m|f(1VNf5#*pEM&x4a2c6r0l66P&ohFc46GZE^wtDGBpnMj`X51okQoc#xNK-x|Q=Qzi1yA5KGCZ5bq~tD*!j&Z}RiHRBP`}$m*eW{D6a<8O!1_$g z+@1*IoW5qp)lfkdyklaOj>>D(rYk|0bg@ZL33p8; zq`V7eWq(6M6;B-(Zv@e>)V+?QIxMzW)sD9v9;%od#bXzA{jmIjr-kJ|?rp7JZ64q% z)6b_k^*>R$JO0SdE(SMoMZF%L{K2kot_H_{_B(-S<=hCvy{SHW*rKQv{Yzp&-l;@TVuLoKyO%ekqdACi}1x?g_&#K-$rPT+T+d@n7J^Xlfad!P_ za2h@aox$XkyeyP*$i-*WJ zR5x41*j-C--+FzWhbyYZ5z#%kk*XbWw~|G2+mO*nYFpkq;(s=T0u?OP%y*s^c;@t!oAh2 zeY*da{Irf~pRxx0N8!W4tYL5*W?&r%;a@r}mbr-<;LtvKp18JPj-p=Ni%IW)5UMEo zne@KOUJX=NUYOvc3bkE(gTT*9&^9o(N4qE8xNG)_+dXVtmP?#up^WIL>R?Yse4jKd zU0_XOx#rb_Y^^x`Zk#F4&0Ht>f)zP97_i{m;DunW=2T9=;97fRoja(pup%?GvWdC= z9o5YJ(&j?`cdIpD!Ux`S7k!~d)h`2US*^<1poj50HlDU AdH?_b diff --git a/blender/osci_render/__init__.py b/blender/osci_render/__init__.py index 80b611d..54e4e82 100644 --- a/blender/osci_render/__init__.py +++ b/blender/osci_render/__init__.py @@ -14,6 +14,7 @@ import bpy import bmesh import socket import json +import atexit from bpy.app.handlers import persistent HOST = "localhost" @@ -50,11 +51,13 @@ class osci_render_connect(bpy.types.Operator): if sock is None: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(1) sock.connect((HOST, PORT)) send_scene_to_osci_render(bpy.context.scene) - except OSError as exp: - sock.close() + except socket.error as exp: sock = None + self.report({"WARNING"}, "Failed to connect to osci-render - make sure it is running first!") + return {"CANCELLED"} return {"FINISHED"} @@ -64,17 +67,20 @@ class osci_render_close(bpy.types.Operator): bl_idname = "render.osci_render_close" def execute(self, context): - global sock - if sock is not None: - sock.send("CLOSE\n".encode('utf-8')) - sock.close() - sock = None + close_osci_render() return {"FINISHED"} -def is_cyclic(spline): - return spline.use_cyclic_u or spline.use_cyclic_v +@persistent +def close_osci_render(): + global sock + if sock is not None: + try: + sock.send("CLOSE\n".encode('utf-8')) + sock.close() + except socket.error as exp: + sock = None def append_matrix(object_info, obj): @@ -85,8 +91,9 @@ def append_matrix(object_info, obj): @persistent def send_scene_to_osci_render(scene): + global sock engine_info = {"objects": []} - + if sock is not None: for obj in bpy.data.objects: if obj.visible_get() and obj.type == 'GPENCIL': @@ -102,12 +109,15 @@ def send_scene_to_osci_render(scene): } for vert in stroke.points]) engine_info["objects"].append(append_matrix(object_info, obj)) - + engine_info["focalLength"] = -0.05 * bpy.data.cameras[0].lens json_str = json.dumps(engine_info, separators=(',', ':')) + '\n' - sock.sendall(json_str.encode('utf-8')) + try: + sock.sendall(json_str.encode('utf-8')) + except socket.error as exp: + sock = None operations = [OBJECT_PT_osci_render_settings, osci_render_connect, osci_render_close] @@ -116,6 +126,7 @@ operations = [OBJECT_PT_osci_render_settings, osci_render_connect, osci_render_c def register(): bpy.app.handlers.frame_change_pre.append(send_scene_to_osci_render) bpy.app.handlers.depsgraph_update_post.append(send_scene_to_osci_render) + atexit.register(close_osci_render) for operation in operations: bpy.utils.register_class(operation) @@ -123,6 +134,7 @@ def register(): def unregister(): bpy.app.handlers.frame_change_pre.remove(send_scene_to_osci_render) bpy.app.handlers.depsgraph_update_post.remove(send_scene_to_osci_render) + atexit.unregister(close_osci_render) for operation in reversed(operations): bpy.utils.unregister_class(operation) diff --git a/pom.xml b/pom.xml index 84c95ce..9f63348 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ sh.ball osci-render - 1.22.0 + 1.22.1 osci-render diff --git a/src/main/java/sh/ball/engine/ObjectServer.java b/src/main/java/sh/ball/engine/ObjectServer.java index 5e5b898..fd74517 100644 --- a/src/main/java/sh/ball/engine/ObjectServer.java +++ b/src/main/java/sh/ball/engine/ObjectServer.java @@ -33,7 +33,7 @@ public class ObjectServer implements Runnable { BufferedReader clientReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (socket.isConnected()) { String json = clientReader.readLine(); - if (json.equals("CLOSE")) { + if (json == null || json.equals("CLOSE")) { socket.close(); break; }