-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path__init__.py
2654 lines (2316 loc) · 112 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
#Known bugs:
# aspect ratio isnt detected properly for videos with non-square pixels... not sure how to detect this.
import bpy
import random
import glob
import os
import sys
import math
from bpy_extras.image_utils import load_image
from bpy.app.handlers import persistent
bl_info = {
"name": "Snu Slideshow Generator",
"description": "Assists in creating image slideshows with a variety of options.",
"author": "Hudson Barkley (Snu)",
"version": (0, 86, 0),
"blender": (4, 3, 2),
"location": "3D View Sideobar, 'Slideshow' tab.",
"wiki_url": "https://github.com/snuq/SnuSlideshowGenerator",
"category": "Import-Export"
}
#This is a list of transform dictionary objects
transforms = [
{
'name': 'None',
'influence': [(0, 1)],
'zLoc': [(0, 1)],
'zRot': [(0, 1)],
'xLoc': [(0, 1)]
},
{
'name': 'Move Left Complete With Pause',
'xLoc': [(-1, 0), (0, 1), (1, 0)]
},
{
'name': 'Move Right Complete With Pause',
'xLoc': [(1, 0), (0, 1), (-1, 0)]
},
{
'name': 'Move Left Little',
'xLoc': [(-0.15, 1), (0.15, 1)]
},
{
'name': 'Move Right Little',
'xLoc': [(0.15, 1), (-0.15, 1)]
},
{
'name': 'Rotate Left Around Top',
'zLoc': [(-0.1, 1)],
'zRot': [(-10, 1), (10, 1)],
'xLoc': [(-0.1, 1), (0.1, 1)]
},
{
'name': 'Rotate Left Around Bottom',
'zLoc': [(-0.1, 1)],
'zRot': [(10, 1), (-10, 1)],
'xLoc': [(-0.1, 1), (0.1, 1)]
},
{
'name': 'Rotate Right Around Top',
'zLoc': [(-0.1, 1)],
'zRot': [(10, 1), (-10, 1)],
'xLoc': [(0.1, 1), (-0.1, 1)]
},
{
'name': 'Rotate Right Around Bottom',
'zLoc': [(-0.1, 1)],
'zRot': [(-10, 1), (10, 1)],
'xLoc': [(0.1, 1), (-0.1, 1)]
},
{
'name': 'Pan To Target',
'influence': [(0, 1), (1, 1)]
},
{
'name': 'Rotate Left',
'zLoc': [(-0.1, 1)],
'zRot': [(10, 1), (-10, 1)]
},
{
'name': 'Rotate Right',
'zLoc': [(-0.1, 1)],
'zRot': [(-10, 1), (10, 1)]
},
{
'name': 'Zoom In',
'influence': [(0, 1), (1, 1)],
'zLoc': [(0, 1), (-0.3, 1)]
},
{
'name': 'Zoom Out',
'influence': [(1, 1), (0, 1)],
'zLoc': [(-0.3, 1), (0, 1)]
},
{
'name': 'Zoom In And Rotate Left',
'influence': [(0, 1), (1, 1)],
'zLoc': [(0, 1), (-0.3, 1)],
'zRot': [(0, 1), (-10, 1)]
},
{
'name': 'Zoom In And Rotate Right',
'influence': [(0, 1), (1, 1)],
'zLoc': [(0, 1), (-0.3, 1)],
'zRot': [(0, 1), (10, 1)]
},
{
'name': 'Zoom Out And Rotate Left',
'influence': [(1, 1), (0, 1)],
'zLoc': [(-0.3, 1), (0, 1)],
'zRot': [(10, 1), (0, 1)]
},
{
'name': 'Zoom Out And Rotate Right',
'influence': [(1, 1), (0, 1)],
'zLoc': [(-0.3, 1), (0, 1)],
'zRot': [(-10, 1), (0, 1)]
}
]
def get_extensions_image():
return list(bpy.path.extensions_image) + [x.upper() for x in list(bpy.path.extensions_image)]
def get_extensions_video():
return list(bpy.path.extensions_movie) + [x.upper() for x in list(bpy.path.extensions_movie)]
def get_image(filepath):
for image in bpy.data.images:
if image.filepath == filepath:
return image
def update_scene(scene):
for view_layer in scene.view_layers:
view_layer.update()
view_layer.objects.update()
def select_plane(image_plane, scene):
if image_plane:
if image_plane.name in bpy.context.view_layer.objects: #Bug in Blender 2.8... seems this doesnt get updated when called from a panel
image_plane.select_set(True)
bpy.context.view_layer.objects.active = image_plane
def is_generator_scene(scene):
if scene.snu_slideshow_generator.is_generator_scene or scene.name == 'Slideshow Generator':
return True
else:
return False
@persistent
def slideshow_autoupdate(_):
#Auto-running modal function to update the slideshow scene so the slides stay ordered properly
if is_generator_scene(bpy.context.scene):
update_order()
lock_view()
def lock_view():
space = get_first_3d_view()
if space:
space.region_3d.view_rotation = (1.0, 0, 0, 0)
space.region_3d.view_perspective = 'ORTHO'
def format_seconds(seconds):
if seconds > 60:
minutes, extra_seconds = divmod(seconds, 60)
length_formatted = str(int(minutes))+" Minutes, "+str(int(extra_seconds))+" Seconds"
else:
length_formatted = str(round(seconds, 2)) + " Seconds"
return length_formatted
def get_fps(scene):
return scene.render.fps / scene.render.fps_base
def create_scene(oldscene, scenename):
#Creates a new scene and copies over render settings from current scene
newscene = bpy.data.scenes.new(scenename)
#These loops are needed because there apparently is no way to easily copy the render settings from one scene to another
for prop in oldscene.render.bl_rna.properties:
#Copy the general render settings
if not prop.is_readonly:
value = eval('oldscene.render.'+prop.identifier)
try:
setattr(newscene.render, prop.identifier, value)
except Exception as e:
pass
for prop in oldscene.render.image_settings.bl_rna.properties:
#Copy the image/video encoding settings
if not prop.is_readonly:
value = eval('oldscene.render.image_settings.'+prop.identifier)
try:
setattr(newscene.render.image_settings, prop.identifier, value)
except Exception as e:
pass
for prop in oldscene.render.ffmpeg.bl_rna.properties:
#Copy the video encoder settings
if not prop.is_readonly:
value = eval('oldscene.render.ffmpeg.'+prop.identifier)
try:
setattr(newscene.render.ffmpeg, prop.identifier, value)
except Exception as e:
pass
newscene.view_settings.view_transform = oldscene.view_settings.view_transform #really not sure why this is needed, but it is...?
#Set variables that are specific to the slideshow scene
newscene.render.film_transparent = False
newscene.render.engine = 'BLENDER_EEVEE_NEXT'
newscene.eevee.shadow_pool_size = '1024'
newscene.render.resolution_percentage = 100
newscene.render.image_settings.color_mode = 'RGB'
return newscene
def aspect_ratio(scene):
render = scene.render
return (render.pixel_aspect_x * render.resolution_x) / (render.pixel_aspect_y * render.resolution_y)
def get_material_elements(material, image_name):
#Gets the material nodes for the given material. If the material is corrupted, will recreate and return the fixed one, if image cannot be found, returns None.
tree = material.node_tree
nodes = tree.nodes
texture = None
shadeless = None
shaded = None
mix = None
output = None
for node in nodes:
if node.type == 'TEX_IMAGE':
texture = node
if node.type == 'EMISSION':
shadeless = node
if node.type == 'BSDF_PRINCIPLED':
shaded = node
if node.type == 'MIX_SHADER':
mix = node
if node.type == 'OUTPUT_MATERIAL':
output = node
if texture is None or shadeless is None or shaded is None or mix is None or output is None:
if texture is not None:
image = texture.image
elif image_name in bpy.data.images:
image = bpy.data.images[image_name]
else:
return None
return setup_material(material, image)
else:
material_nodes = {
'texture': texture,
'shadeless': shadeless,
'shaded': shaded,
'mix': mix,
'output': output
}
return material_nodes
def setup_material(material, image):
#Setup an image/video material
material.use_nodes = True
tree = material.node_tree
nodes = tree.nodes
nodes.clear()
#Create nodes
texture = nodes.new('ShaderNodeTexImage')
texture.image = image
texture.image_user.frame_duration = image.frame_duration
texture.image_user.frame_offset = 0
texture.image_user.use_auto_refresh = True
texture.location = (-400, 0)
shadeless = nodes.new('ShaderNodeEmission')
shadeless.location = (0, 0)
shaded = nodes.new('ShaderNodeBsdfPrincipled')
shaded.location = (-100, -150)
mix = nodes.new('ShaderNodeMixShader')
mix.location = (200, 0)
mix.inputs[0].default_value = 0
output = nodes.new('ShaderNodeOutputMaterial')
output.location = (400, 0)
#Connect nodes
tree.links.new(texture.outputs[0], shadeless.inputs[0])
tree.links.new(texture.outputs[0], shaded.inputs[0])
tree.links.new(shadeless.outputs[0], mix.inputs[1])
tree.links.new(shaded.outputs[0], mix.inputs[2])
tree.links.new(mix.outputs[0], output.inputs[0])
material_nodes = {
'texture': texture,
'shadeless': shadeless,
'shaded': shaded,
'mix': mix,
'output': output
}
return material_nodes
def import_slideshow_image(image, image_number, slide_length, generator_scene, video=False, last_image=None):
#This function will import an image into the generator scene and create the objects needed for modifying slides in the generator
if len(image.name) > 20:
image.name = image.name[0:19]
if video:
print('Importing video '+str(image_number)+', filename: '+image.name)
else:
print('Importing image '+str(image_number)+', filename: '+image.name)
#create and set up image plane
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
ix = ((image.size[0] / image.size[1])/2)
iy = 0.5
verts = [(-ix, iy, 0.0), (ix, iy, 0.0), (ix, -iy, 0.0), (-ix, -iy, 0.0)]
faces = [(3, 2, 1, 0)]
image_plane = add_object(generator_scene, image.name, 'MESH', mesh_verts=verts, mesh_faces=faces)
image_plane.slideshow.name = image_plane.name
add_constraints(image_plane, 'Plane')
if video:
image_plane.slideshow.locklength = True
image_plane.slideshow.lockextra = True
image_plane.slideshow.locktransform = True
image_plane.slideshow.videomaxlength = image.frame_duration
image_plane.slideshow.videofile = image.filepath
slide_length = image.frame_duration / get_fps(generator_scene)
#set up and apply material to plane
image_material = bpy.data.materials.new(image_plane.name)
image_plane.data.uv_layers.new()
image_plane.data.materials.append(image_material) #set material to plane
setup_material(image_material, image)
#set up transform and extras
if not video:
randomized = []
hidden = generator_scene.snu_slideshow_generator.hidden_transforms.split(";")
for transform in transforms:
if transform['name'] not in hidden:
if last_image:
if transform['name'] != last_image.slideshow.transform:
randomized.append(transform)
else:
randomized.append(transform)
if len(randomized) == 0:
transform = transforms[0]
else:
transform = randomized[random.randint(0, (len(randomized) - 1))]
randomized = []
hidden = generator_scene.snu_slideshow_generator.hidden_extras.split(";")
extras = list_extras()
for extra in extras:
if extra not in hidden:
if last_image:
if extra != last_image.slideshow.extra:
randomized.append(extra)
else:
randomized.append(extra)
if len(randomized) == 0:
extra = "None"
else:
extra = randomized[random.randint(0, (len(randomized) - 1))]
randomized = []
for extra_texture_preset in generator_scene.snu_slideshow_generator.extra_texture_presets:
if last_image:
if extra_texture_preset.path != last_image.slideshow.extratexture:
randomized.append(extra_texture_preset.path)
else:
randomized.append(extra_texture_preset.path)
if len(randomized) == 0:
extra_texture = 'None'
else:
if len(generator_scene.snu_slideshow_generator.extra_texture_presets) > 1:
extra_texture = randomized[random.randint(0, (len(randomized) - 1))]
else:
extra_texture = generator_scene.snu_slideshow_generator.extra_texture_presets[0].path
#add target empty
target_empty = add_object(generator_scene, image_plane.name+' Target', 'EMPTY')
target_empty.parent = image_plane
target_empty.empty_display_size = 1
add_constraints(target_empty, 'Target')
image_plane.slideshow.target = target_empty.name
#add view empty
view_empty = add_object(generator_scene, image_plane.name+' View', 'EMPTY')
view_empty.parent = image_plane
view_empty.empty_display_size = 1
view_empty.scale = aspect_ratio(generator_scene) / 2, .5, .001
view_empty.empty_display_type = 'CUBE'
add_constraints(view_empty, 'View')
image_plane.slideshow.view = view_empty.name
#add text next to plane saying which index number it is
index_text = add_object(generator_scene, image_plane.name+' Index', 'FONT')
index_text.parent = image_plane
index_text.location = (-1, -.33, 0)
index_text.data.align_x = 'RIGHT'
add_constraints(index_text, 'Text')
if not video:
#add text next to plane saying which transform was picked
transform_text = add_object(generator_scene, image_plane.name+' Transform', 'FONT')
transform_text.parent = image_plane
transform_text.location = (1, 0, 0)
transform_text.scale = .15, .15, 1
add_constraints(transform_text, 'Text')
#add text next to plane saying which extra was picked
extra_text = add_object(generator_scene, image_plane.name+' Extra', 'FONT')
extra_text.parent = image_plane
extra_text.location = (1, -.25, 0)
extra_text.scale = .15, .15, 1
add_constraints(extra_text, 'Text')
#add text next to plane saying how long the slide is
length_text = add_object(generator_scene, image_plane.name+' Length', 'FONT')
length_text.parent = image_plane
length_text.location = (1, .25, 0)
length_text.scale = .15, .15, 1
add_constraints(length_text, 'Text')
#create group and add everything to it
image_group = bpy.data.collections.new(image_plane.name)
image_group.objects.link(image_plane)
image_group.objects.link(index_text)
image_group.objects.link(length_text)
if not video:
image_group.objects.link(target_empty)
image_group.objects.link(transform_text)
image_group.objects.link(extra_text)
image_group.objects.link(view_empty)
#additional settings
image_plane.slideshow.index = image_number + 1
if not video:
image_plane.slideshow.length = slide_length
image_plane.slideshow.transform = transform['name']
image_plane.slideshow.extra = extra
image_plane.slideshow.extratexture = extra_texture
else:
image_plane.slideshow.videolength = image.frame_duration
return image_plane
def add_constraints(constraint_object, constraint_type):
#This function handles adding constraints to the various objects of the slideshow generator scene
#bpy.context.view_layer.objects.active = constraint_object
rotation_constraint = constraint_object.constraints.new(type='LIMIT_ROTATION')
location_constraint = constraint_object.constraints.new(type='LIMIT_LOCATION')
scale_constraint = constraint_object.constraints.new(type='LIMIT_SCALE')
#set up rotation constraint
rotation_constraint.use_limit_x = True
rotation_constraint.use_limit_y = True
if constraint_type != 'View':
rotation_constraint.use_limit_z = True
#set up local location constraint
constraint_object.constraints[1].owner_space = 'LOCAL'
if constraint_type == 'Target' or constraint_type == 'View':
location_constraint.min_x = -1 * (aspect_ratio(bpy.context.scene)/2)
location_constraint.max_x = aspect_ratio(bpy.context.scene) / 2
location_constraint.min_y = -.5
location_constraint.max_y = .5
else:
location_constraint.min_x = constraint_object.location[0]
location_constraint.max_x = constraint_object.location[0]
location_constraint.min_y = constraint_object.location[1]
location_constraint.max_y = constraint_object.location[1]
location_constraint.use_max_x = True
location_constraint.use_min_x = True
if constraint_type != 'Plane':
location_constraint.use_max_y = True
location_constraint.use_min_y = True
location_constraint.use_max_z = True
location_constraint.use_min_z = True
#set up limit scale constraint
scale_constraint.min_x = constraint_object.scale[0]
scale_constraint.max_x = constraint_object.scale[0]
scale_constraint.min_y = constraint_object.scale[1]
scale_constraint.max_y = constraint_object.scale[1]
scale_constraint.min_z = constraint_object.scale[2]
scale_constraint.max_z = constraint_object.scale[2]
if constraint_type != 'View':
scale_constraint.use_min_x = True
scale_constraint.use_max_x = True
scale_constraint.use_min_y = True
scale_constraint.use_max_y = True
scale_constraint.use_min_z = True
scale_constraint.use_max_z = True
#set up text world location limit
if constraint_type == 'Text':
limit_location_constraint = constraint_object.constraints.new(type='LIMIT_LOCATION')
limit_location_constraint.owner_space = 'WORLD'
limit_location_constraint.use_max_x = True
limit_location_constraint.use_min_x = True
limit_location_constraint.min_x = constraint_object.location[0]
limit_location_constraint.max_x = constraint_object.location[0]
def is_video_file(file):
file = bpy.path.abspath(file)
if os.path.isfile(file):
extensions = bpy.path.extensions_movie
extension = os.path.splitext(file)[1]
if extension.lower() in extensions:
return True
return False
def clear_sequencer(scene):
scene.sequence_editor_clear()
def add_object(scene, name, object_type, mesh_verts=[], mesh_faces=[]):
created = None
if object_type == 'EMPTY':
created = bpy.data.objects.new(name=name, object_data=None)
elif object_type == 'FONT':
object_data = bpy.data.curves.new(name=name, type='FONT')
created = bpy.data.objects.new(name=name, object_data=object_data)
elif object_type == 'MESH':
mesh = bpy.data.meshes.new(name=name)
mesh.from_pydata(mesh_verts, [], mesh_faces)
created = bpy.data.objects.new(name=name, object_data=mesh)
elif object_type == 'CAMERA':
camera = bpy.data.cameras.new(name=name)
created = bpy.data.objects.new(name=name, object_data=camera)
if created is not None:
scene.collection.objects.link(created)
return created
def create_slideshow_slide(image_plane, i, generator_scene, image_scene_start, images, previous_image_clip, previous_image_plane):
#This function makes the slideshow image scene and adds it to the sequencer of the scene
base_name = generator_scene.snu_slideshow_generator.base_name
image_scene_name = base_name + '-' + image_plane.name
if not generator_scene.sequence_editor:
generator_scene.sequence_editor_create()
if not image_plane.slideshow.videofile:
print('Generating scene for: '+image_scene_name)
if bpy.data.scenes.find(image_scene_name) != -1:
bpy.data.scenes.remove(bpy.data.scenes[image_scene_name])
image_scene = create_scene(generator_scene, image_scene_name)
image_scene.snu_slideshow_generator.generator_name = generator_scene.name
image_scene.snu_slideshow_generator.base_name = base_name
image_scene.eevee.taa_render_samples = generator_scene.snu_slideshow_generator.render_samples
#Set up image_scene render settings
image_scene_frames = int(get_fps(image_scene) * image_plane.slideshow.length)
image_scene.frame_end = image_scene_frames
target_empty = generator_scene.objects[image_plane.slideshow.target]
view_empty = generator_scene.objects[image_plane.slideshow.view]
image_scene.collection.objects.link(image_plane)
image_scene.collection.objects.link(target_empty)
image_scene.collection.objects.link(view_empty)
#set up scene world
world = bpy.data.worlds.new(image_scene.name)
image_scene.world = world
world.use_nodes = False
world.color = (0, 0, 0)
#create transforms
transform_index = get_transform(image_plane.slideshow.transform)
if transform_index >= 0:
transform = transforms[transform_index]
else:
transform = transforms[0]
image_scene.cursor.location = (0.0, 0.0, 0.0)
image_scene.frame_current = 1
transform_empty = add_object(image_scene, transform['name'], 'EMPTY')
transform_empty.animation_data_create()
transform_action = bpy.data.actions.new(transform['name'])
transform_empty.animation_data.action = transform_action
aspect = aspect_ratio(image_scene)
multiplier = 1.09
multiplier = 1.375
if aspect > 1:
zoffset = aspect * multiplier
else:
zoffset = multiplier
transform_interpolation = image_plane.slideshow.transform_interpolation
transform_empty.location = (0, 0, zoffset)
if 'zLoc' in transform.keys():
locations = len(transform['zLoc'])
fcurve = transform_action.fcurves.new('location', index=2)
for index, location in enumerate(transform['zLoc']):
if locations == 1:
pointx = 1
else:
pointx = ((index * image_scene_frames)/(locations - 1)) + 1
pointy = transform['zLoc'][index][0] + zoffset
pointsize = (transform['zLoc'][index][1]) * (image_scene_frames / locations / 2)
point = fcurve.keyframe_points.insert(frame=pointx, value=pointy)
point.interpolation = transform_interpolation
point.handle_left_type = 'FREE'
point.handle_right_type = 'FREE'
point.handle_left = (pointx - pointsize, pointy)
point.handle_right = (pointx + pointsize, pointy)
if 'zRot' in transform.keys():
locations = len(transform['zRot'])
fcurve = transform_action.fcurves.new('rotation_euler', index=2)
for index, location in enumerate(transform['zRot']):
if locations == 1:
pointx = 1
else:
pointx = ((index * image_scene_frames)/(locations - 1)) + 1
pointy = 3.14159265358979 * (transform['zRot'][index][0]) / 180
pointsize = (transform['zRot'][index][1]) * (image_scene_frames / locations / 2)
point = fcurve.keyframe_points.insert(frame=pointx, value=pointy)
point.interpolation = transform_interpolation
point.handle_left_type = 'FREE'
point.handle_right_type = 'FREE'
point.handle_left = (pointx - pointsize, pointy)
point.handle_right = (pointx + pointsize, pointy)
if 'xLoc' in transform.keys():
locations = len(transform['xLoc'])
fcurve = transform_action.fcurves.new('location', index=0)
for index, location in enumerate(transform['xLoc']):
if locations == 1:
pointx = 1
else:
pointx = ((index * image_scene_frames)/(locations - 1)) + 1
pointy = (transform['xLoc'][index][0]) * aspect_ratio(bpy.context.scene)
pointsize = (transform['xLoc'][index][1]) * (image_scene_frames / locations / 2)
point = fcurve.keyframe_points.insert(frame=pointx, value=pointy)
point.interpolation = transform_interpolation
point.handle_left_type = 'FREE'
point.handle_right_type = 'FREE'
point.handle_left = (pointx - pointsize, pointy)
point.handle_right = (pointx + pointsize, pointy)
if 'influence' in transform.keys():
constraint = transform_empty.constraints.new(type='COPY_LOCATION')
constraint.use_z = False
constraint.target = target_empty
constraint.influence = 0
locations = len(transform['influence'])
fcurve = transform_action.fcurves.new('constraints[0].influence')
for index, location in enumerate(transform['influence']):
if locations == 1:
pointx = 1
else:
pointx = ((index * image_scene_frames)/(locations - 1)) + 1
pointy = transform['influence'][index][0]
pointsize = (transform['influence'][index][1]) * (image_scene_frames / locations / 2)
point = fcurve.keyframe_points.insert(frame=pointx, value=pointy)
point.interpolation = transform_interpolation
point.handle_left_type = 'FREE'
point.handle_right_type = 'FREE'
point.handle_left = (pointx - pointsize, pointy)
point.handle_right = (pointx + pointsize, pointy)
#set up camera location scale and animation
camera = add_object(image_scene, generator_scene.name+' Camera', 'CAMERA')
camera.parent = transform_empty
image_scene.camera = camera
camera.data.dof.focus_object = image_plane
camera.data.dof.use_dof = False
#add camera_scale empty that will scale the transform and camera
camera_scale = add_object(image_scene, generator_scene.name+' Camera Scale', 'EMPTY')
camera_scale.parent = image_plane
transform_empty.parent = camera_scale
camera_scale.location = view_empty.location
camera_scale.rotation_euler = view_empty.rotation_euler
camera_scale_value = (view_empty.scale[1] * 2)
camera_scale.scale = (camera_scale_value, camera_scale_value, camera_scale_value)
#run extras script
if image_plane.slideshow.extra != 'None':
extra = get_extra(image_plane.slideshow.extra)
if extra:
current_scene = bpy.context.window.scene
try:
folder = os.path.split(extra)[0]
file = os.path.splitext(os.path.split(extra)[1])[0]
sys.path.insert(0, folder)
script = __import__(file)
sys.path.remove(folder)
image = load_image(bpy.path.abspath(image_plane.slideshow.extratexture))
material = image_plane.material_slots[0].material
material_nodes = get_material_elements(material, image_plane.slideshow.name)
if material_nodes is not None:
data = {
'image_scene': image_scene,
'image_plane': image_plane,
'material': material,
'material_texture': material_nodes['texture'],
'material_shadeless': material_nodes['shadeless'],
'material_shaded': material_nodes['shaded'],
'material_mix': material_nodes['mix'],
'material_output': material_nodes['output'],
'target_empty': target_empty,
'camera': camera,
'extra_amount': image_plane.slideshow.extraamount,
'extra_text': image_plane.slideshow.extratext,
'extra_texture': image}
script.extra(data)
del script
bpy.context.window.scene = current_scene
except ImportError:
pass
#update_scene(image_scene)
#Add the image scene to the slideshow scene
clip = generator_scene.sequence_editor.sequences.new_scene(scene=image_scene, name=image_scene.name, channel=((i % 2) + 1), frame_start=image_scene_start)
else:
print('Importing Clip: '+image_scene_name)
image_scene_frames = image_plane.slideshow.videolength
rotate = image_plane.slideshow.rotate
#Decide if a blurred background clip is needed
aspect = aspect_ratio(generator_scene)
clipx = image_plane.dimensions[0]
clipy = image_plane.dimensions[1]
clip_aspect = clipx / clipy
aspect_difference = abs(aspect - clip_aspect)
if aspect_difference > 0.1:
blur_background = image_plane.slideshow.videobackground
else:
blur_background = False
#Determine channel to put clips in
base_channel = ((i % 2) + 1)
blur_base_channel = base_channel
if blur_background:
base_channel = base_channel + 3
#Generate video clip(s)
bpy.ops.sequencer.select_all(action='DESELECT')
clip = generator_scene.sequence_editor.sequences.new_movie(filepath=image_plane.slideshow.videofile, name=image_plane.name, channel=base_channel, frame_start=image_scene_start)
flipped = False
if rotate == '90':
flipped = True
clip.transform.rotation = 0 - math.pi/2
elif rotate == '180':
clip.transform.rotation = math.pi
elif rotate == '-90':
flipped = True
clip.transform.rotation = math.pi / 2
if blur_background:
blur_clip = generator_scene.sequence_editor.sequences.new_movie(filepath=image_plane.slideshow.videofile, name=image_plane.name, channel=blur_base_channel, frame_start=image_scene_start)
blur_clip.transform.rotation = clip.transform.rotation
else:
blur_clip = None
#Set scaling of clip and blur clip
image = get_image(image_plane.slideshow.videofile)
clip_x = image.size[0]
clip_y = image.size[1]
render = generator_scene.render
scene_x = render.resolution_x
scene_y = render.resolution_y
video_aspect = clip_x / clip_y
if video_aspect <= aspect:
#video is less wide aspect than the scene, scale to height
if not flipped:
video_scale = scene_y / clip_y
blur_scale = scene_x / clip_x
else:
video_scale = scene_y / clip_x
blur_scale = scene_x / clip_y
else: #if video_aspect > scene_aspect:
#video is more wide aspect than the scene, scale to width
if not flipped:
video_scale = scene_x / clip_x
blur_scale = scene_y / clip_y
else:
video_scale = scene_x / clip_y
blur_scale = scene_y / clip_x
clip.transform.scale_x = video_scale
clip.transform.scale_y = video_scale
if blur_background:
blur_clip.transform.scale_x = blur_scale
blur_clip.transform.scale_y = blur_scale
#Add other clips
speed_clip = None
audioclip = None
blur_speed_clip = None
blur_blur_clip = None
if image_plane.slideshow.videoaudio:
audioclip = generator_scene.sequence_editor.sequences.new_sound(filepath=image_plane.slideshow.videofile, name=image_plane.name, channel=base_channel + 2, frame_start=image_scene_start)
if audioclip.frame_duration == 0:
generator_scene.sequence_editor.sequences.remove(audioclip)
print('No Audio Found For This Clip')
audioclip = None
else:
#Add fadein/out to audio clip
generator_scene.frame_current = image_scene_start
audioclip.volume = 0
audioclip.keyframe_insert(data_path='volume')
generator_scene.frame_current = image_scene_start + generator_scene.snu_slideshow_generator.crossfade_length
audioclip.volume = 1
audioclip.keyframe_insert(data_path='volume')
generator_scene.frame_current = audioclip.frame_final_end - generator_scene.snu_slideshow_generator.crossfade_length
audioclip.keyframe_insert(data_path='volume')
generator_scene.frame_current = audioclip.frame_final_end
audioclip.volume = 0
audioclip.keyframe_insert(data_path='volume')
length_percent = image_scene_frames / clip.frame_final_duration
if audioclip.frame_final_duration != clip.frame_final_duration:
speed_clip = generator_scene.sequence_editor.sequences.new_effect(name='Speed', type='SPEED', channel=clip.channel+1, seq1=clip, frame_start=clip.frame_final_start)
if audioclip.frame_final_duration > 1:
clip.frame_final_duration = audioclip.frame_final_duration
if blur_background:
blur_speed_clip = generator_scene.sequence_editor.sequences.new_effect(name='Speed', type='SPEED', channel=blur_clip.channel+1, seq1=blur_clip, frame_start=blur_clip.frame_final_start)
blur_clip.frame_final_duration = clip.frame_final_duration
image_scene_frames = audioclip.frame_final_duration * length_percent
if speed_clip:
apply_transform = speed_clip
if blur_background:
blur_apply_transform = blur_speed_clip
else:
blur_apply_transform = None
else:
apply_transform = clip
if blur_background:
blur_apply_transform = blur_clip
else:
blur_apply_transform = None
#Properly scale the clip
apply_transform.select = True
if blur_background:
blur_apply_transform.select = True
if blur_background:
blur_blur_clip = generator_scene.sequence_editor.sequences.new_effect(name='Blur', type='GAUSSIAN_BLUR', channel=blur_apply_transform.channel+1, seq1=blur_apply_transform, frame_start=blur_apply_transform.frame_final_start)
blur_blur_clip.size_x = 40
blur_blur_clip.size_y = 40
#Create meta strip
generator_scene.sequence_editor.active_strip = clip
if blur_clip:
blur_clip.select = True
if blur_speed_clip:
blur_speed_clip.select = True
if blur_blur_clip:
blur_blur_clip.select = True
if speed_clip:
speed_clip.select = True
if audioclip:
audioclip.select = True
clip.select = True
bpy.ops.sequencer.meta_make()
clip = generator_scene.sequence_editor.active_strip
#Trim video clip
offset = image_plane.slideshow.videooffset
clip.frame_offset_start = offset
clip.frame_start = image_scene_start - offset
#image_scene_frames = int(image_plane.slideshow.length * get_fps(generator_scene))
if blur_background:
clip.channel = blur_base_channel
else:
clip.channel = base_channel
#Add fade in
if i == 0:
clip.blend_type = 'ALPHA_OVER'
generator_scene.frame_current = 1
clip.blend_alpha = 0
clip.keyframe_insert(data_path='blend_alpha')
generator_scene.frame_current = generator_scene.snu_slideshow_generator.crossfade_length
clip.blend_alpha = 1
clip.keyframe_insert(data_path='blend_alpha')
#Add transition
if generator_scene.snu_slideshow_generator.crossfade_length > 0:
if previous_image_clip and previous_image_plane:
first_sequence = previous_image_clip
second_sequence = clip
if previous_image_plane.slideshow.transition == "GAMMA_CROSS":
effect = generator_scene.sequence_editor.sequences.new_effect(name=previous_image_clip.name+' to '+clip.name, channel=3, frame_start=second_sequence.frame_final_start, frame_end=first_sequence.frame_final_end, type='GAMMA_CROSS', seq1=first_sequence, seq2=second_sequence)
effect.frame_still_end = first_sequence.frame_final_end #apparently needed since the frame_end doesnt get set in the previous function...
elif previous_image_plane.slideshow.transition == "WIPE":
effect = generator_scene.sequence_editor.sequences.new_effect(name=previous_image_clip.name+' to '+clip.name, channel=3, frame_start=second_sequence.frame_final_start, frame_end=first_sequence.frame_final_end, type='WIPE', seq1=first_sequence, seq2=second_sequence)
effect.frame_still_end = first_sequence.frame_final_end #apparently needed since the frame_end doesnt get set in the previous function...
effect.transition_type = previous_image_plane.slideshow.wipe_type
effect.direction = previous_image_plane.slideshow.wipe_direction
if previous_image_plane.slideshow.wipe_soft:
effect.blur_width = 0.2
if previous_image_plane.slideshow.wipe_angle == 'RIGHT':
effect.angle = 0 - (math.pi / 2)
elif previous_image_plane.slideshow.wipe_angle == 'LEFT':
effect.angle = math.pi / 2
elif previous_image_plane.slideshow.wipe_angle == 'UP':
effect.angle = 0
if effect.direction == 'IN':
effect.direction = 'OUT'
else:
effect.direction = 'IN'
else:
effect.angle = 0
elif previous_image_plane.slideshow.transition == "CUSTOM" and is_video_file(previous_image_plane.slideshow.custom_transition_file):
effect_channel = first_sequence.channel
if second_sequence.channel > effect_channel:
effect_channel = second_sequence.channel
if second_sequence.channel > first_sequence.channel:
#second sequence is above first, transitions are normal
inverted = False
start_color = (0, 0, 0)
end_color = (1, 1, 1)
apply_mask_to = second_sequence
else:
#first sequence is above second, transitions are inverted
inverted = True
start_color = (1, 1, 1)
end_color = (0, 0, 0)
apply_mask_to = first_sequence
bpy.ops.sequencer.select_all(action='DESELECT')
file_path = bpy.path.abspath(previous_image_plane.slideshow.custom_transition_file)
effect = generator_scene.sequence_editor.sequences.new_movie(filepath=file_path, name=previous_image_plane.name+' Transition', channel=effect_channel + 1, frame_start=second_sequence.frame_final_start + 1)
effect.frame_final_end = first_sequence.frame_final_end - 1
if inverted:
invert_modifier = effect.modifiers.new(name='Invert Colors', type='CURVES')
invert_modifier.curve_mapping.curves[3].points[0].location = (0, 1)
invert_modifier.curve_mapping.curves[3].points[1].location = (1, 0)
effect_speed = generator_scene.sequence_editor.sequences.new_effect(name='Speed', type='SPEED', channel=effect.channel + 1, seq1=effect, frame_start=effect.frame_final_start)
effect_final = generator_scene.sequence_editor.sequences.new_effect(name='Color', type='COLOR', channel=effect.channel, frame_start=effect.frame_final_end, frame_end=effect.frame_final_end + 1)
effect_final.frame_final_end = effect_final.frame_final_start + 1
effect_final.color = end_color
effect_start = generator_scene.sequence_editor.sequences.new_effect(name='Color', type='COLOR', channel=effect.channel, frame_start=effect.frame_final_start - 1, frame_end=effect.frame_final_start)
effect_start.frame_final_end = effect_start.frame_final_start + 1
effect_start.color = start_color
effect.select = True
effect_speed.select = True
effect_final.select = True
effect_start.select = True
bpy.ops.sequencer.meta_make()
effect = generator_scene.sequence_editor.active_strip
effect.name = previous_image_clip.name+' to '+clip.name
effect.channel = effect_channel + 1
effect.mute = True
apply_mask_to.blend_type = 'ALPHA_OVER'
modifier = apply_mask_to.modifiers.new(name='Transition from '+first_sequence.name, type='MASK')
modifier.input_mask_type = 'STRIP'
modifier.input_mask_strip = effect
elif previous_image_plane.slideshow.transition == "NONE":
pass
else:
effect = generator_scene.sequence_editor.sequences.new_effect(name=previous_image_clip.name+' to '+clip.name, channel=3, frame_start=second_sequence.frame_final_start, frame_end=first_sequence.frame_final_end, type='CROSS', seq1=first_sequence, seq2=second_sequence)
#Add fade out
if i == (len(images) - 1):
clip.blend_type = 'ALPHA_OVER'
generator_scene.frame_current = int(image_scene_start + image_scene_frames)
clip.blend_alpha = 0
clip.keyframe_insert(data_path='blend_alpha')
generator_scene.frame_current = int(image_scene_start + image_scene_frames - generator_scene.snu_slideshow_generator.crossfade_length)
clip.blend_alpha = 1
clip.keyframe_insert(data_path='blend_alpha')
generator_scene.frame_current = 1