-
Notifications
You must be signed in to change notification settings - Fork 1
/
discordrpblender.py
200 lines (164 loc) · 5.75 KB
/
discordrpblender.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
import bpy, subprocess
from bpy.app.handlers import persistent
import time, datetime, os, atexit
from discoIPC import ipc
from sys import platform
# Settings
showFilename = True;
showTimeElapsed = True;
base_activity = {
'assets': {
'large_image': 'blender',
'large_text': 'Blender ' + bpy.app.version_string.split(" ")[0],
'small_image': 'default_file',
'small_text': '(Untitled)'
},
'timestamps': {},
}
pid = os.getpid()
objcount = 0
timeElapsed = int(time.time())
active = 0
rendering = 0
rendertime = 0
client = ipc.DiscordIPC('000000000000000000') # Replace your clientID with the 18 digit template
client.connect()
print('\nStarting Discord ICP ...\n')
@atexit.register
def disconnect():
main()
print('Disconnecting Discord ICP ...\n')
client.disconnect()
@persistent
def main():
client.update_activity(set_activity())
@persistent
def resetTime(scene):
bpy.ops.wm.modal_timer_operator()
@persistent
def render_begin(scene):
global rendering
global rendertime
global active
rendering = 1
active = 2
rendertime = int(time.time())
@persistent
def render_cancel(scene):
global rendering
global rendertime
global active
rendering = 0
rendertime = 0
active = 1
@persistent
def render_complete(scene):
global rendering
global rendertime
global active
rendering = 0
rendertime = 0
#time_taken = int(time.time()) - int(rendertime)
#time_end = int(time.time())
active = 1
@persistent
def set_activity():
global rendering
global rendertime
global objcount
if platform == "linux" or platform == "linux2":
if not subprocess.getstatusoutput('xdotool')[0] == 0:
pidcheck = subprocess.getstatusoutput('xdotool getwindowfocus getwindowpid')[1]
if not str(pidcheck) != str(pid):
if rendering == 1:
active = 2
else:
active = 0
else:
if rendering == 1:
active = 2
else:
active = 0
else:
if rendering == 1:
active = 2
else:
active = 0
#Set activity for the player
activity = base_activity
if showFilename == True:
if bpy.path.basename(bpy.context.blend_data.filepath)[:-6]:
activity['assets']['small_text'] = bpy.path.basename(bpy.context.blend_data.filepath)[:-6]
else:
activity['assets']['small_text'] = "(Untitled)"
if active == 0:
string = bpy.context.mode.split("_")
string1 = string[0].lower().capitalize()
if len(string) > 1:
string1 = string1 + " " + string[1].lower().capitalize()
activity['details'] = string1 + " Mode"
elif active == 1:
activity['details'] = 'Idle'
elif active == 2:
activity['details'] = 'Rendering for ' + '0' + str(datetime.timedelta(seconds=((int(time.time() - rendertime))))) # Hacky
if bpy.context.mode == "OBJECT" and active != 2:
count = len(bpy.data.objects)
activity['state'] = '(' + str(len(bpy.context.selected_objects)) + '/' + str(count) + ' ' + ("object" if count == 1 else "objects") + ' selected)'
else:
if active != 2:
ob = bpy.context.object
ob.update_from_editmode()
if ob.type == "MESH":
count = len(bpy.context.object.data.vertices)
selected = len([v.index for v in bpy.context.object.data.vertices if v.select])
activity['state'] = '(' + str(selected) + '/' + str(count) + ' ' + ("vertex" if count == 1 else "vertices") + ' selected)'
elif ob.type == "ARMATURE":
count = len(bpy.context.object.data.bones)
selected = len([b.name for b in bpy.context.object.data.bones if b.select])
activity['state'] = '(' + str(selected) + '/' + str(count) + ' ' + ("bone" if count == 1 else "bones") + ')'
else:
activity['state'] = "Frame " + str(bpy.context.scene.frame_current) + "/" + str(bpy.context.scene.frame_end - bpy.context.scene.frame_start)
if showTimeElapsed == True:
activity['timestamps']['start'] = timeElapsed
return activity
timerfile = 0
class ModalTimerOperator(bpy.types.Operator):
"""Operator which runs its self from a timer"""
bl_idname = "wm.modal_timer_operator"
bl_label = "Modal Timer Operator"
_timer = None
@persistent
def modal(self, context, event):
if event.type == 'TIMER':
global filename
global timerfile
if time.time() - timerfile > 0.05:
timerfile = time.time()
main()
return {'PASS_THROUGH'}
@persistent
def execute(self, context):
wm = context.window_manager
self._timer = wm.event_timer_add(2, window=context.window)
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
@persistent
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
def register():
bpy.utils.register_class(ModalTimerOperator)
bpy.app.handlers.load_post.append(resetTime)
bpy.app.handlers.render_pre.append(render_begin)
bpy.app.handlers.render_cancel.append(render_cancel)
bpy.app.handlers.render_complete.append(render_complete)
bpy.app.handlers.load_post.append(resetTime)
def unregister():
bpy.app.handlers.load_post.remove(resetTime)
bpy.app.handlers.render_complete.remove(render_complete)
bpy.app.handlers.render_cancel.append(render_cancel)
bpy.app.handlers.render_pre.remove(render_begin)
bpy.utils.unregister_class(ModalTimerOperator)
if __name__ == "__main__":
register()
bpy.ops.wm.modal_timer_operator()