-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
227 lines (165 loc) · 5.87 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
'''
Copyright (c) 2018-2020 Naxela
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
'''
bl_info = {
'name': 'BMForge',
'description': 'BMForge.',
'author': 'Alexander Kleemann @ Naxela',
'version': (0, 0, 1),
'blender': (2, 90, 0),
'location': 'View3D',
'category': '3D View'
}
import os, sys, bpy, time, threading, socket, json, multiprocessing
from bpy.utils import ( register_class, unregister_class )
from bpy.props import (
StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
FloatVectorProperty,
EnumProperty,
PointerProperty,
)
from bpy.types import (
Panel,
AddonPreferences,
Operator,
PropertyGroup,
)
BM_STATUS = {
"active" : False,
"thread" : None,
"socket" : None
}
class BM_Properties (PropertyGroup):
Server_Running : BoolProperty(
name="Server running",
description="Prop.",
default = False
)
def broadcast(connection, msg, prefix=""): # prefix is for name identification.
connection.send(bytes(prefix, "utf8") + msg)
def evaluateCommand(input, header):
if input == 0: #Shutdown
print("Shutting down")
try:
BM_STATUS["socket"].close()
except:
pass
BM_STATUS["active"] = False
BM_STATUS["thread"].join()
elif input == 1: #Append file
print("Appending scene")
filepath = header
with bpy.data.libraries.load(filepath) as (data_from, data_to):
data_to.objects = [name for name in data_from.objects]
# link them to scene
scene = bpy.context.scene
for obj in data_to.objects:
if obj is not None:
bpy.context.collection.objects.link(obj)
def startServer():
BM_STATUS["socket"] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
BM_STATUS["socket"].bind(('localhost', 9750))
BM_STATUS["socket"].listen(1)
print("Starting server")
BM_STATUS["active"] = True
while BM_STATUS["active"]:
connection, address = BM_STATUS["socket"].accept()
print("Connection from: %s:%s" % address)
while BM_STATUS["active"]:
#print("Listening")
try:
data = connection.recv(1024)
except:
print("Bad error occurred A")
if data != bytes("#quit", "utf8"):
try:
parsed_data = json.loads(data.decode())
except:
print("Bad error occurred B")
print(parsed_data)
if parsed_data['action'] == 0: #Ping
#print("BMForge: Ping")
print("Ping header: " + parsed_data['header'])
elif parsed_data['action'] == 1: #Query
#print("BMForge: Query")
print("Query header: " + parsed_data['header'])
elif parsed_data['action'] == 2: #Command
#print("BMForge: Command")
print("Command header: " + parsed_data['header'])
evaluateCommand(parsed_data['command'], parsed_data['header'])
else:
print("BMForge: Unknown call")
BM_STATUS["active"] = False
connection.close()
#broadcast(connection, data, name + ": ")
else:
print("Quitting")
connection.send(bytes("#quit", "utf8"))
connection.close()
broadcast(connection, bytes("%s has left the chat." % name, "utf8"))
break
BM_STATUS["socket"].close()
print("Socket closed")
class BM_Connect(bpy.types.Operator):
bl_idname = "bm.connect"
bl_label = "Connect to Blacksmith"
bl_description = "Connect to Blacksmith"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
BM_STATUS["thread"] = threading.Thread(target=startServer)
BM_STATUS["thread"].daemon = True
BM_STATUS["thread"].start()
return {'FINISHED'}
class BM_Close(bpy.types.Operator):
bl_idname = "bm.close"
bl_label = "Close connection"
bl_description = "Close connection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
try:
BM_STATUS["socket"].close()
except:
pass
BM_STATUS["active"] = False
BM_STATUS["thread"].join()
return {'FINISHED'}
class SCENE_PT_BM_panel (Panel):
bl_idname = "SCENE_PT_BM_panel"
bl_label = "BMForge"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "BMForge"
@classmethod
def poll(self,context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
row = layout.row(align=True)
if BM_STATUS["active"] == False:
row.operator("bm.connect")
else:
row.operator("bm.close")
classes = [BM_Connect, BM_Close, SCENE_PT_BM_panel]
def register():
for cls in classes:
register_class(cls)
def unregister():
for cls in reversed(classes):
unregister_class(cls)