-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnla_script.py
More file actions
441 lines (361 loc) · 13.2 KB
/
Copy pathnla_script.py
File metadata and controls
441 lines (361 loc) · 13.2 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import bpy
def label_to_slot_num(scene, label):
"""Returns -1 if no label was found"""
nla_script = scene.b4a_nla_script
for num in range(len(nla_script)):
slot = nla_script[num]
if slot.label == label:
return num
return -1
def check_marker(scene, marker):
markers = scene.timeline_markers
if len(markers) and marker in markers:
return True
else:
return False
def markers_to_frame_range(scene, marker_start, marker_end):
if not check_marker(scene, marker_start):
return None
if not (marker_end == "" or check_marker(scene, marker_end)):
return None
markers = scene.timeline_markers
first = markers[marker_start].frame
if first < scene.frame_start:
first = scene.frame_start
if marker_end == "":
last = scene.frame_end
for marker in markers:
if marker.frame > first and marker.frame < last:
last = marker.frame
else:
last = markers[marker_end].frame
if last > scene.frame_end:
last = scene.frame_end
return [first, last]
def object_by_name(objects, name):
"""Allow 'dg_name1*dg_name2...*name' format for the object's name"""
names = name.split("*", maxsplit=1)
for obj in objects:
if obj.name == names[0]:
if len(names) == 1:
return obj
elif obj.dupli_group:
return object_by_name(obj.dupli_group.objects, names[1])
else:
return None
return None
class B4A_ScriptSlot(bpy.types.PropertyGroup):
# labels are called "names" in the interface
label = bpy.props.StringProperty(
name = "Script slot name",
description = "Script slot name",
default = ""
)
type = bpy.props.EnumProperty(
name = "Script slot type",
description = "Script slot type",
default = "PLAY",
items = [
("NOOP", "Noop", "No operation"),
("MATH", "Math operation", "Perform a math operation"),
("REGSTORE", "Register store", "Store a value to a register"),
("CONDJUMP", "Conditional Jump", "Conditional jump"),
("JUMP", "Jump", "Jump to the slot by label"),
("SELECT_PLAY", "Select & Play", "Select an object then play"),
("SELECT", "Select & Jump", "Select an object"),
("PLAY", "Play", "Play NLA animation")
]
)
param_marker_start = bpy.props.StringProperty(
name = "Start Marker",
description = "First marker of the playback",
default = ""
)
param_marker_end = bpy.props.StringProperty(
name = "End Marker",
description = "Final marker of the playback",
default = ""
)
param_slot = bpy.props.StringProperty(
name = "Target Slot",
description = "Name of the target slot",
default = ""
)
param_object = bpy.props.StringProperty(
name = "Object",
description = "Name of the object",
default = ""
)
param_register1 = bpy.props.EnumProperty(
name = "Register 1",
description = "First register operand",
default = "R1",
items = [
("R8", "R8", "Register 8"),
("R7", "R7", "Register 7"),
("R6", "R6", "Register 6"),
("R5", "R5", "Register 5"),
("R4", "R4", "Register 4"),
("R3", "R3", "Register 3"),
("R2", "R2", "Register 2"),
("R1", "R1", "Register 1")
]
)
param_register2 = bpy.props.EnumProperty(
name = "Register 2",
description = "Second register operand",
default = "R1",
items = [
("R8", "R8", "Register 8"),
("R7", "R7", "Register 7"),
("R6", "R6", "Register 6"),
("R5", "R5", "Register 5"),
("R4", "R4", "Register 4"),
("R3", "R3", "Register 3"),
("R2", "R2", "Register 2"),
("R1", "R1", "Register 1")
]
)
param_register_dest = bpy.props.EnumProperty(
name = "Destination Register",
description = "Destination register operand",
default = "R1",
items = [
("R8", "R8", "Register 8"),
("R7", "R7", "Register 7"),
("R6", "R6", "Register 6"),
("R5", "R5", "Register 5"),
("R4", "R4", "Register 4"),
("R3", "R3", "Register 3"),
("R2", "R2", "Register 2"),
("R1", "R1", "Register 1")
]
)
param_number1 = bpy.props.FloatProperty(
name = "Number",
description = "First numeric operand",
default = 0,
step = 100
)
param_number2 = bpy.props.FloatProperty(
name = "Number",
description = "Second numeric operand",
default = 0,
step = 100
)
param_register_flag1 = bpy.props.BoolProperty(
name = "Register",
description = "First register operand",
default = False
)
param_register_flag2 = bpy.props.BoolProperty(
name = "Register",
description = "Second register operand",
default = False
)
param_operation = bpy.props.EnumProperty(
name = "Operation",
description = "Operation to perform on input operands",
default = "ADD",
items = [
("DIV", "Divide", "Divide"),
("SUB", "Subtract", "Subtract"),
("MUL", "Multiply", "Multiply"),
("ADD", "Add", "Add")
]
)
param_condition = bpy.props.EnumProperty(
name = "Condition",
description = "Conditonal operator",
default = "EQUAL",
items = [
("GEQUAL", "Greater Than or Equal (>=)", "Greater than or equal"),
("LEQUAL", "Less Than or Equal (<=)", "Less than or equal"),
("GREATER", "Greater Than (>)", "Greater than"),
("LESS", "Less Than (<)", "Less than"),
("NOTEQUAL", "Not Equal (!=)", "Not equal"),
("EQUAL", "Equal (=)", "Equal")
]
)
class B4A_ScriptAddOperator(bpy.types.Operator):
bl_idname = 'b4a.nla_script_add'
bl_label = "Add"
bl_description = "Add new script slot"
def invoke(self, context, event):
scene = context.scene
script = scene.b4a_nla_script
script.add()
slot_cnt = 0
while (True):
old_slot_cnt = slot_cnt
for slot in script:
if slot.label == "SLOT_" + str(slot_cnt):
slot_cnt+=1
if old_slot_cnt == slot_cnt:
break
index = len(script)-1
script[index].name = "SLOT_" + str(slot_cnt)
script[index].label = "SLOT_" + str(slot_cnt)
return {'FINISHED'}
class B4A_ScriptRemOperator(bpy.types.Operator):
bl_idname = 'b4a.nla_script_remove'
bl_label = "Remove"
bl_description = "Remove selected script slot"
def invoke(self, context, event):
scene = context.scene
script = scene.b4a_nla_script
for index in range(len(script)):
if script[index] == context.nla_script_slot:
script.remove(index)
break;
return {'FINISHED'}
class B4A_ScriptUpOperator(bpy.types.Operator):
bl_idname = 'b4a.nla_script_up'
bl_label = "Up"
bl_description = "Move script slot up"
def invoke(self, context, event):
scene = context.scene
script = scene.b4a_nla_script
for index in range(len(script)):
if script[index] == context.nla_script_slot and index > 0:
script.move(index, index-1)
break;
return {'FINISHED'}
class B4A_ScriptDownOperator(bpy.types.Operator):
bl_idname = 'b4a.nla_script_down'
bl_label = "Down"
bl_description = "Move script slot down"
def invoke(self, context, event):
scene = context.scene
script = scene.b4a_nla_script
for index in range(len(script)):
if script[index] == context.nla_script_slot and index < len(script)-1:
script.move(index, index+1)
break;
return {'FINISHED'}
def draw(layout, context):
scene = context.scene
if not scene:
return
row = layout.row()
row.operator("b4a.nla_script_add", icon='ZOOMIN', text="Append NLA script slot")
script = scene.b4a_nla_script
for slot in script:
col = layout.column(align=True)
col.context_pointer_set("nla_script_slot", slot)
row = col.box().row()
row.prop(slot, "label", text="")
sub_row = row.row(align=True)
sub_row.operator("b4a.nla_script_down", icon='TRIA_DOWN', text="")
sub_row.operator("b4a.nla_script_up", icon='TRIA_UP', text="")
row = row.row(align=False)
row.operator("b4a.nla_script_remove", icon='X', text="")
split = col.box().split()
col = split.column()
col.prop(slot, "type", text="Type")
if slot.type == "PLAY":
if check_marker(scene, slot.param_marker_start):
icon = "MARKER"
else:
icon = "ERROR"
col.prop(slot, "param_marker_start", icon=icon)
if slot.param_marker_end == "":
icon = "NONE"
elif check_marker(scene, slot.param_marker_end):
icon = "MARKER"
else:
icon = "ERROR"
col.prop(slot, "param_marker_end", icon=icon)
elif slot.type == "SELECT":
if object_by_name(scene.objects, slot.param_object):
icon = "OBJECT_DATA"
else:
icon = "ERROR"
col.prop(slot, "param_object", icon=icon)
if slot.param_slot and label_to_slot_num(scene, slot.param_slot) > -1:
icon = "NODE"
else:
icon = "ERROR"
col.prop(slot, "param_slot", icon=icon)
elif slot.type == "SELECT_PLAY":
if object_by_name(scene.objects, slot.param_object):
icon = "OBJECT_DATA"
else:
icon = "ERROR"
col.prop(slot, "param_object", icon=icon)
if check_marker(scene, slot.param_marker_start):
icon = "MARKER"
else:
icon = "ERROR"
col.prop(slot, "param_marker_start", icon=icon)
if slot.param_marker_end == "":
icon = "NONE"
elif check_marker(scene, slot.param_marker_end):
icon = "MARKER"
else:
icon = "ERROR"
col.prop(slot, "param_marker_end", icon=icon)
elif slot.type == "JUMP":
if slot.param_slot and label_to_slot_num(scene, slot.param_slot) > -1:
icon = "NODE"
else:
icon = "ERROR"
col.prop(slot, "param_slot", icon=icon)
elif slot.type == "CONDJUMP":
col.prop(slot, "param_condition")
row = col.row()
row.label("First operand:")
if slot.param_register_flag1:
row.prop(slot, "param_register1", text="")
else:
row.prop(slot, "param_number1")
row.prop(slot, "param_register_flag1")
row = col.row()
row.label("Second operand:")
if slot.param_register_flag2:
row.prop(slot, "param_register2", text="")
else:
row.prop(slot, "param_number2")
row.prop(slot, "param_register_flag2")
if slot.param_slot and label_to_slot_num(scene, slot.param_slot) > -1:
icon = "NODE"
else:
icon = "ERROR"
col.prop(slot, "param_slot", icon=icon)
elif slot.type == "REGSTORE":
row = col.row()
row.label("Register:")
row.prop(slot, "param_register_dest", text="")
row.prop(slot, "param_number1")
elif slot.type == "MATH":
col.prop(slot, "param_operation")
row = col.row()
row.label("First operand:")
if slot.param_register_flag1:
row.prop(slot, "param_register1", text="")
else:
row.prop(slot, "param_number1")
row.prop(slot, "param_register_flag1")
row = col.row()
row.label("Second operand:")
if slot.param_register_flag2:
row.prop(slot, "param_register2", text="")
else:
row.prop(slot, "param_number2")
row.prop(slot, "param_register_flag2")
row = col.row()
row.label("Destination:")
row.prop(slot, "param_register_dest", text="")
row.label("")
def register():
bpy.utils.register_class(B4A_ScriptSlot)
bpy.utils.register_class(B4A_ScriptAddOperator)
bpy.utils.register_class(B4A_ScriptRemOperator)
bpy.utils.register_class(B4A_ScriptUpOperator)
bpy.utils.register_class(B4A_ScriptDownOperator)
def unregister():
bpy.utils.unregister_class(B4A_ScriptSlot)
bpy.utils.unregister_class(B4A_ScriptAddOperator)
bpy.utils.unregister_class(B4A_ScriptRemOperator)
bpy.utils.unregister_class(B4A_ScriptUpOperator)
bpy.utils.unregister_class(B4A_ScriptDownOperator)