-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdynamic_operators.py
77 lines (62 loc) · 2.3 KB
/
dynamic_operators.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
'''
This module can create and register operators dynamically based on a description.
'''
import bpy
from bpy.props import *
from .. utils.debug import *
from .. utils.handlers import eventUMOGHandler
operatorsByDescription = {}
missingDescriptions = set()
def getInvokeFunctionOperator(description):
if description in operatorsByDescription:
return operatorsByDescription[description]
missingDescriptions.add(description)
return fallbackOperator.bl_idname
@eventUMOGHandler("SCENE_UPDATE_POST")
def createMissingOperators(scene):
while len(missingDescriptions) > 0:
description = missingDescriptions.pop()
operator = createOperatorWithDescription(description)
operatorsByDescription[description] = operator.bl_idname
DBG(str(description), operator, operator.bl_idname, TRACE = False)
bpy.utils.register_class(operator)
def createOperatorWithDescription(description):
operatorID = str(len(operatorsByDescription))
idName = "umog.invoke_function_" + operatorID
operator = type("InvokeFunction_" + operatorID, (bpy.types.Operator, ), {
"bl_idname" : idName,
"bl_label" : "Are you sure?",
"bl_description" : description,
"invoke" : invoke_InvokeFunction,
"execute" : execute_InvokeFunction,
"__annotations__" : {
"callback" : StringProperty(),
"invokeWithData" : BoolProperty(default = False),
"confirm" : BoolProperty(),
"data" : StringProperty(),
"passEvent" : BoolProperty()
}
})
return operator
def invoke_InvokeFunction(self, context, event):
self._event = event
if self.confirm:
return context.window_manager.invoke_confirm(self, event)
return self.execute(context)
def execute_InvokeFunction(self, context):
DBG()
args = []
if self.invokeWithData: args.append(self.data)
if self.passEvent: args.append(self._event)
self.umog_executeCallback(self.callback, *args)
bpy.context.area.tag_redraw()
return {"FINISHED"}
fallbackOperator = createOperatorWithDescription("")
# Register
##################################
def register():
try: bpy.utils.register_class(fallbackOperator)
except: pass
def unregister():
try: bpy.utils.unregister_class(fallbackOperator)
except: pass