scripting grease pencil

Grease Pencil Drawing a Square Tutorial

grease pencil for dummies

Welcome to getting started with grease pencil in Blender2.79.

import bpy, math
The scene must first be prepared to use grease pencil
By default blender does not start with grease pencil ready for using.
Note, the following is just a hack to automate the grease pencil setup, and has changed in Blender2.8. So you can mostly ignore it.
# 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 )
## End of grease pencil hack for set up,
Each layer contains one or more of frames Below we only use a single layer and a single frame. If we had wanted to generate animation, we call layer.frames.new(n), where n is the frame number, this returns a frame object that contains any number of strokes.

layer = bpy.context.scene.grease_pencil.layers.new('mylayer')
frame = layer.frames.new(1)  ## 1 is the first frame

Each frame has an unlimited number of brush strokes,
calling frame.strokes.new() will create and return a new stroke,
which you can assign to a variable, like below the variable `a`

a = frame.strokes.new()
A brush stroke defaults to screen space, so here we force it to be in 3D space.

a.draw_mode = '3DSPACE'
draw_cyclic is required because it draws a line from the last point back to the first point
a.draw_cyclic = True
line_width set a bigger line width, because it defaults to 1 pixel
a.line_width = 3
a.points.add( count = 4 )
Define new variable `radius` which is the radius of the square.
radius = 2
Below are the points of the square. Within the brackets [], 0 is the first item, 1=second, 2=third, and 3=forth. Radius affects the square x and y only.
a.points[0].co.x = -radius
a.points[0].co.y = -radius
## this is the second point 
a.points[1].co.x = radius
a.points[1].co.y = -radius
## this is the third point
a.points[2].co.x = radius
a.points[2].co.y = radius
##this is the forth point
a.points[3].co.x = -radius
a.points[3].co.y = radius
The video above is all generated by Python and drawing simple shapes using: sin, cos, random, uniform and other operations.

Comments

Popular Posts