generating grease pencil animation

For Loop, Color, and Animation Tutorial

In this tutorial you will see how to use a for loop to create multiple squares on the same frame, or as an animation of squares moving upwards. We also introduce the legacy system of creating and assigning grease pencil materials.
## change this to True to see the stack as an animation of rising squares
AS_ANIMATION = False


import bpy, math

# Create grease pencil data if none exists
if not bpy.context.scene.grease_pencil:
    a = [ a for a in bpy.context.screen.areas if a.type == 'VIEW_3D' ][0]
    override = {
        'scene'         : bpy.context.scene,
        'screen'        : bpy.context.screen,
        'object'        : bpy.context.object,
        'area'          : a,
        'region'        : a.regions[0],
        'window'        : bpy.context.window,
        'active_object' : bpy.context.object
    }
    bpy.ops.gpencil.data_add( override )

Grease Pencil Materials

Blender2.79 has a palette to organize materials, note this has changed in the new Blender2.8 to use extended standard materials.
Palette = bpy.context.scene.grease_pencil.palettes.new(name='my-palette')
gmat = Palette.colors.new()
each brush stroke will reference a material by name, note in Blender2.8 this has changed to a number index.
gmat.name = 'blue'
gmat.alpha = 1
gmat.fill_alpha = 1
The color and fill_color of the stroke is in format: R,G,B. note, in Blender2.8 this has changed to: R,G,B,A
gmat.fill_color = [0,0,1]
gmat.color = [0,0,0]
Create new grease layer and initialize frame to None.
layer = bpy.context.scene.grease_pencil.layers.new('mylayer')
frame = None
Begin loop, this creates the squares, as an animation or a stack of squares.
for i in range(20):
 ## if AS_ANIMATION is True, then make a new frame of animation
 if AS_ANIMATION:
  frame = layer.frames.new(i)
 elif frame is None:
  frame = layer.frames.new(0)  ## frame zero

 a = frame.strokes.new()

 a.draw_mode = '3DSPACE'
 a.colorname = 'blue'
 a.draw_cyclic = True
 a.line_width = 3

 a.points.add( count = 4 )

 radius = 2

 a.points[0].co.x = -radius
 a.points[0].co.y = -radius

 a.points[1].co.x = radius
 a.points[1].co.y = -radius

 a.points[2].co.x = radius
 a.points[2].co.y = radius

 a.points[3].co.x = -radius
 a.points[3].co.y = radius

 ## set the height of the square
 for pnt in a.points:
  pnt.co.z = i

Comments

Popular Posts