-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
280 lines (223 loc) · 8.85 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#-*- coding: utf-8 -*-
#------------------------------------------
# pyEphestos
#
# Morpheas is the GUI side of Ephestos based on morphic.py
#
#
# written by Kilon Alios
#
# version April-2012
#
# Copyright (C) 2012 by Kilon Alios
#
# Under GPL license for more info see the Blender license
#
#---------------------------------------
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
""" Pharo code:
stream := SocketStream openConnectionToHostNamed: ' 127.0.0.1' port: 4000 .
stream sendCommand: 'Hello Pharo AGAIN!!!!'.
stream close."""
bl_info = {
"name": "Ephestos : communication tool for Pharo",
"description": "Ephestos is a communication tool to allow Pharo to control Blender. This allows Pharo to control blender via Blender Python API. Pharo is a simple programming language , very powerful IDE and a fun live coding enviroment.",
"author": "Kilon",
"version": (0, 0, 1),
"blender": (2, 7, 1),
"location": "View3D > Left panel ",
"warning": 'Warning !!! It may crash Blender ! Always save your work !', # used for warning icon and text in addons panel
"wiki_url": "",
"tracker_url": "https://github.com/kilon/pyEphestos",
"category": "Development"}
import sys
import time
import bpy
import threading
import socket
from bpy.props import *
from time import sleep
import atexit
import re
ephestos_running = False
thread_created = False
threadSocket = 0
socketServer = 0
receivedSocket = "none"
listening = False
socketMessages = []
shutDown = False
receivedData = ''
pherror = [""]
def cleanup():
global ephestos_running, threadSocket, listening, socketServer
ephestos_running = False
listening = False
socketServer.settimeout(0.1)
threadSocket.join()
#socketServer.shutdown(socket.SHUT_RDWR)
socketServer.close()
del socketServer
thread_created = False
shutDown = False
atexit.register(cleanup)
def create_thread():
global threadSocket,listening
threadSocket = threading.Thread(name='threadSocket', target= socket_listen)
listening = True
create_socket_connection()
threadSocket.start()
#threadSocket.join()
def socket_listen():
global receivedSocket,listening, receivedData,socketServer, socketMessages, pherror
socketServer.listen(5)
while listening:
(receivedSocket , adreess) = socketServer.accept()
receivedData = (receivedSocket.recv(1024)).decode("utf-8")[:-2]
#exec(receivedData)
socketMessages.append(receivedData)
sleep(0.03)
print("pherror : " ,pherror)
while not (pherror[-1]==""):
receivedSocket.sendall((pherror[-1]+'\n').encode())
print("I have sent err : --->"+ pherror[-1] + ' <---- and removed it from the list of errors')
if len(pherror) > 1:pherror.remove(pherror[-1])
receivedSocket.sendall("end of error\n".encode())
receivedSocket.close()
def create_socket_connection():
global socketServer
socketServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
socketServer.bind(('127.0.0.1',4000))
class open_ephestos(bpy.types.Operator):
bl_idname = "ephestos_button.modal"
bl_label = "Enable Ephestos"
_timer = None
def modal(self, context, event):
global ephestos_running, thread_created, listening, socketServer, socketMessages, pherror, shutDown
result = {'PASS_THROUGH'}
#context.area.tag_redraw()
#context.area.header_text_set("Welcome to Ephestos")
#if context.area:
# context.area.tag_redraw()
if context.area.type == 'VIEW_3D' and ephestos_running and event.type in {'ESC',}:
ephestos_running = False
listening = False
#time.sleep(1)
socketServer.close()
context.window_manager.event_timer_remove(self._timer)
#time.sleep(1)
thread_created = False
result = {'CANCELLED'}
self.report({'WARNING'}, "Ephestos has been closed")
if context.area.type == 'VIEW_3D' and ephestos_running and event.type == 'TIMER' :
if shutDown:
ephestos_running = False
listening = False
socketServer.settimeout(0.01)
#socketServer.shutdown(socket.SHUT_RDWR)
socketServer.close()
threadSocket.join()
del socketServer
context.window_manager.event_timer_remove(self._timer)
thread_created = False
shutDown = False
result = {'CANCELLED'}
self.report({'WARNING'}, "Ephestos has been closed")
for msg in socketMessages:
try:
if "RetValue:" in msg:
regexobj = re.compile("[^(RetValue:)]\S.*")
regexsearch = regexobj.search(msg)
pythoncomm = regexsearch.group(0)
pherror.append("RetValue:"+str(eval(pythoncomm)))
print("eval with pherror: ",pherror)
#pherror.append("no error\n")
else:
exec(msg,globals())
#socketMessages.remove(msg)
pherror.append("no error\n")
except Exception as e:
newerror = "Error:" +str(e)+" with :" + msg
pherror.append( newerror )
print( "inserted an error now pherror : ",pherror)
#self.report({'WARNING'}, pherror)
#socketMessages.remove(msg)
socketMessages.remove(msg)
# create_thread()
# thread_created = True
return result
def invoke(self, context, event):
global ephestos_running,thread_created
if context.area.type == 'VIEW_3D' and ephestos_running == False :
self.cursor_on_handle = 'None'
# Add the region OpenGL drawing callback
# draw in view space with 'POST_VIEW' and 'PRE_VIEW'
#self._handle =bpy.types.SpaceView3D.draw_handler_add(draw_ephestos,(self,context), 'WINDOW', 'POST_PIXEL')
self._timer = context.window_manager.event_timer_add(0.01,context.window)
ephestos_running = True
context.window_manager.modal_handler_add(self)
create_thread()
thread_created = True
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "Ephestos is already opened and running")
return {'CANCELLED'}
class AddBox(bpy.types.Operator):
"""Add a simple box mesh"""
bl_idname = "ephestos.close"
bl_label = "Close Ephestos"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
global shutDown
shutDown = True
return {'FINISHED'}
class ephestos_panel(bpy.types.Panel):
bl_label = "Ephestos"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
global receivedSocket,listening,pherror
sce = context.scene
layout = self.layout
box = layout.box()
box.label(text="Ephestos WIP not finished yet")
box.label(text="-----------------------------")
if listening:
box.label(text="Listening")
else:
box.label(text="Not Listening")
box.label(text="ReceivedData:")
box.label(text=receivedData)
if len(pherror) == 0:
box.label(text="Error: no error ")
else:
box.label(text=pherror[-1])
box.operator("ephestos_button.modal")
box.operator("ephestos.close")
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()