-
Notifications
You must be signed in to change notification settings - Fork 0
/
Video_to_Grease_Pencil.py
115 lines (96 loc) · 4.11 KB
/
Video_to_Grease_Pencil.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
import bpy
import os
"""Process images"""
def imageLoad(imgpath, fp):
files = os.listdir(imgpath)
scene = bpy.context.scene
# get existing output path
#Get the names of the files in the provided folder
for x in range(len(files)):
bpy.ops.object.select_all(action='DESELECT')
imagename = files[x][0:4]
print (imagename)
if files[x].endswith('.jpg') or files[x].endswith('.png'):
print(files[x])
empty = bpy.data.objects.new('image.' + imagename, None)
scene.collection.objects.link(empty)
empty.location = (0,0,0)
empty.empty_display_type = 'IMAGE'
img = bpy.data.images.load(imgpath + files[x])
empty.data = img
# Link light object to the active collection of current view layer so that it'll appear in the current scene.
#trace image to grease pencil
trace_image(imagename, empty)
#rename GPencil
rename_GPencil(imagename)
#for debugging purposes
# if imagename == '0002':
# return
#delete empty object
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['image.' + imagename].select_set(True)
bpy.ops.object.delete()
#render image
render_single_frame(imagename, fp)
#delete grease pencil object
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['gpencil.' + imagename].select_set(True)
bpy.ops.object.delete()
#for debugging purposes
# if imagename == '0002':
# return
"""Delete empty objects"""
def delete_empties():
bpy.ops.object.select_all(action='DESELECT')
# select all grease pencil objects
for ob in bpy.context.scene.objects:
if ob.type == 'EMPTY' and ob.name.startswith("image."):
#Select the object
ob.select_set(state=True)
#Delete all objects selected above
bpy.ops.object.delete()
"""Delete grease pencil objects"""
def delete_gpencils():
bpy.ops.object.select_all(action='DESELECT')
# select all grease pencil objects
for ob in bpy.context.scene.objects:
if ob.type == 'GPENCIL' and ob.name.startswith("gpencil."):
#Select the object
ob.select_set(state=True)
#Delete all objects selected above
bpy.ops.object.delete()
"""render frame"""
def render_single_frame(frame, fp):
scene = bpy.context.scene
# set current frame
scene.frame_set(int(frame))
# set output path so render won't get overwritten
scene.render.filepath = fp + frame
#render the frame
bpy.ops.render.render(write_still=True) # render still
#reset filepath
scene.render.filepath = fp
"""trace selected image to grease pencil"""
def trace_image(frame, empty):
print("trace." + frame)
#make empty active and select
bpy.context.view_layer.objects.active = empty
empty.select_set(state=True)
bpy.ops.gpencil.trace_image(target='NEW', thickness=1, resolution=5, scale=0.97, sample=0.0, threshold=0.502, turnpolicy='MINORITY', mode='SINGLE')
"""rename grease pencel object"""
def rename_GPencil(frame):
bpy.context.view_layer.objects.active = bpy.data.objects['GPencil']
bpy.data.objects["image." + frame].select_set(False)
bpy.data.objects["GPencil"].select_set(True)
bpy.context.view_layer.objects.active
bpy.context.object.name = ('gpencil.' + frame)
# check for empty & grease pencil objects added in previous run of this script and delete them
delete_empties()
delete_gpencils()
# set path where source images reside
imgpathfull = "C:/tmp/"
#Get path where rendered images will be written
scene = bpy.context.scene
fp = scene.render.filepath
#load images, create empties, convert to GP and render each image
imageLoad(imgpathfull, fp)