-
Notifications
You must be signed in to change notification settings - Fork 2
/
ZyncPlugin.pyp
255 lines (202 loc) · 8.34 KB
/
ZyncPlugin.pyp
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
"""
Implements Zync plugin for C4D.
"""
from __future__ import division
from importlib import import_module
import os
import sys
# Required for importing of other modules, because C4D doesn't automatically add the plugin
# directory to the search path.
PLUGIN_DIR = os.path.dirname(__file__)
sys.path.append(PLUGIN_DIR)
c4d = import_module('c4d')
from zync_c4d_utils import post_plugin_error, show_exceptions
import zync_c4d_constants
class ZyncPlugin(c4d.plugins.CommandData):
""" Implements Zync plugin for C4D. """
def __init__(self):
self._dialog = None
@show_exceptions
def Execute(self, _doc):
""" Opens Zync plugin dialog window. """
if not self._dialog:
self._create_dialog()
if not self._dialog.Open(dlgtype=c4d.DLG_TYPE_ASYNC,
pluginid=zync_c4d_constants.PLUGIN_ID):
raise Exception('Failed to open dialog window')
return True
def _create_dialog(self):
from zync_c4d_utils import import_zync_module
default_thread_pool = import_zync_module(
'zync_threading.default_thread_pool')
def _handle_background_task_error(task_name, err, traceback_str):
print('Exception %s in task %s' % (err, task_name))
print('Traceback:\n%s' % traceback_str)
post_plugin_error(traceback_str)
thread_pool = default_thread_pool.DefaultThreadPool(
error_handler=_handle_background_task_error)
from zync_c4d_utils import import_zync_module
zync_threading = import_zync_module('zync_threading')
main_thread_executor = zync_threading.MainThreadExecutor(thread_pool,
self._push_special_event,
self._push_special_event)
from zync_c4d_facade import C4dFacade
c4d_facade = C4dFacade(main_thread_executor)
from zync_c4d_dialog import ZyncDialog
self._dialog = ZyncDialog(thread_pool, main_thread_executor, c4d_facade)
@staticmethod
def _push_special_event():
c4d.SpecialEventAdd(zync_c4d_constants.PLUGIN_ID)
@show_exceptions
def RestoreLayout(self, sec_ref):
""" Restores the dialog. """
if not self._dialog:
self._create_dialog()
return self._dialog.Restore(pluginid=zync_c4d_constants.PLUGIN_ID,
secret=sec_ref)
def _plugin_cmd(plug_id):
return "PLUGIN_CMD_" + str(plug_id)
class ResourceWithAncestorPath(object):
"""
Describes C4D menu resource.
It remembers path from root and it's able to update ansestors.
History is represented as a list of dicts.
Each element has two keys:
'item' is the resource.
'index' An integer telling the index of the resource in the parent container
Most of methods return the resource object, so it's possible to chain
operations. Find method returns boolean.
e.g.
res = ResourceWithHistory(main_menu)
if res.find(c4d.MENURESOURCE_COMMAND, _plugin_cmd(ZYNC_DOWNLOAD_MENU_ID)):
res.pop().append_zync_command().update_parents()
"""
def __init__(self, root):
self.root = root
self.ancestor_path = []
def append_zync_command(self):
""" Adds Zync command to the resource. """
item = self.ancestor_path[-1]['item']
zync_menu_clone = item.GetClone(c4d.COPYFLAGS_0)
item.FlushAll()
item.InsData(c4d.MENURESOURCE_COMMAND,
_plugin_cmd(zync_c4d_constants.PLUGIN_ID))
item.InsData(c4d.MENURESOURCE_SEPERATOR, True)
for idx, value in list(zync_menu_clone):
item.InsData(idx, value)
self.ancestor_path[-1]['item'] = item
return self
def append_zync_menu(self):
""" Adds Zync Submenu to the resource and changes current resource to the created submenu. """
bc_zync_menu = c4d.BaseContainer()
bc_zync_menu.InsData(c4d.MENURESOURCE_SUBTITLE,
zync_c4d_constants.ZYNC_SUBMENU_PCMD)
if hasattr(c4d, 'MENURESOURCE_SUBTITLE_ICONID'):
bc_zync_menu.InsData(c4d.MENURESOURCE_SUBTITLE_ICONID,
zync_c4d_constants.PLUGIN_ID)
self.ancestor_path[-1]['item'].InsData(c4d.MENURESOURCE_SUBMENU,
bc_zync_menu)
self.ancestor_path.append(
dict(item=bc_zync_menu,
index=len(self.ancestor_path[-1]['item']) - 1))
return self
def pop(self):
""" Changes current resource to it's parent. """
self.ancestor_path.pop()
return self
def _find(self, attribute, value, root):
"""
Recursively looks for a resource which `attribute` equals to `value`.
Internal implementation of `find` method.
Args:
attribute: int, C4D attribute description. e.g. c4d.MENURESOURCE_SUBTITLE
value: object, Desired value of the attribute.
root: c4d.BaseContainer, Current root of the lookup.
Returns: boolean, [dict()] First element tells if search was successful.
The other is a path form `bc` to found element.
"""
if root is not None:
for i, (attr, current) in enumerate(root):
if attr == c4d.MENURESOURCE_SUBMENU and isinstance(current,
c4d.BaseContainer):
result, current_path = self._find(attribute, value, current)
if result:
current_path.append(dict(item=current, index=i))
return True, current_path
elif attr == attribute and current == value:
return True, [dict(item=current, index=i)]
return False, [dict(item=None, index=0)]
def find(self, attribute, value, root=None):
"""
Recursively looks for a resource which `attribute` equals to `value`.
Args:
attribute: int, C4D attribute description. e.g. c4d.MENURESOURCE_SUBTITLE
value: object, Desired value of the attribute.
root: c4d.BaseContainer, Current root of the lookup.
Returns: boolean, True if element was found.
"""
if root is None:
root = self.root
result, result_path = self._find(attribute, value, root)
if result:
self.ancestor_path = list(reversed(result_path))
return result
def update_parents(self):
""" Follows the path an replaces all resources on the path with a copy hold
in the path structure. """
for i in reversed(range(len(self.ancestor_path))[1:]):
self.ancestor_path[i - 1]['item'].SetIndexData(
self.ancestor_path[i]['index'],
self.ancestor_path[i]['item'])
self.root.SetIndexData(self.ancestor_path[0]['index'],
self.ancestor_path[0]['item'])
return self
def _add_zync_items_to_menu():
"""
Adds "Render with Zync" command to Pipeline->Zync submenu.
If parent elements don't exist, tries to create them.
Returns: bool, True if success.
"""
main_menu = c4d.gui.GetMenuResource("M_EDITOR")
if not main_menu:
print("Not an interactive environment.")
return False # Probably not a Cinema 4D with GUI or a TRS/TRC environment
if c4d.gui.SearchMenuResource(main_menu,
_plugin_cmd(zync_c4d_constants.PLUGIN_ID)):
# Zync menu is already present
return True
res = ResourceWithAncestorPath(main_menu)
if res.find(c4d.MENURESOURCE_COMMAND,
_plugin_cmd(zync_c4d_constants.ZYNC_DOWNLOAD_MENU_ID)):
res.pop().append_zync_command().update_parents()
elif res.find(c4d.MENURESOURCE_SUBTITLE,
zync_c4d_constants.PIPELINE_MENU_PCMD):
res.pop().append_zync_menu().append_zync_command().update_parents()
return True
def PluginMessage(msg_id, _data):
""" Install Zync menu items in C4D. """
# catch C4DPL_BUILDMENU to add Zync items to the menu.
result = False
if msg_id == c4d.C4DPL_BUILDMENU:
result = _add_zync_items_to_menu()
if not result:
print "Zync plugin failed to update menu."
return result
def main():
""" Plugin entry point. """
bmp = c4d.bitmaps.BaseBitmap()
bmp.InitWith(os.path.join(PLUGIN_DIR, 'res', 'zync.png'))
res_pipeline = c4d.gui.SearchPluginMenuResource(
zync_c4d_constants.PIPELINE_MENU_PCMD)
print("Zync plugin loading...")
if not c4d.plugins.RegisterCommandPlugin(
id=zync_c4d_constants.PLUGIN_ID,
str='Render with Zync...',
info=c4d.PLUGINFLAG_HIDEPLUGINMENU if res_pipeline else c4d.PLUGINFLAG_COMMAND_ICONGADGET,
icon=bmp,
help='Render scene using Zync cloud service',
dat=ZyncPlugin()):
print("Zync plugin failed to register command.")
print("Zync plugin loaded.")
if __name__ == '__main__':
main()