drawing a circle

Grease Pencil Circle Tutorial

In this tutorial you will learn how to draw a simple circle, and add noise to a section of it. Just like in previous tutorials you must import bpy and math modules, after that we use the from syntax to import just the uniform function from the random module. We need uniform to generate the random noise.
import bpy, math
from random import uniform
Skip this section if you have already read the previous tutorials, it is just a hack to setup the legacy (blender2.79) grease pencil.
# 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 )

Palette = bpy.context.scene.grease_pencil.palettes.new(name='my-palette')
gmat = Palette.colors.new()
gmat.name = 'blue'
gmat.alpha = 1
gmat.fill_alpha = 1
gmat.fill_color = [0,0,1]
gmat.color = [0,0,0]
Prepare the stroke called a, and give it 360 points
layer = bpy.context.scene.grease_pencil.layers.new('mylayer')
frame = layer.frames.new(1)  ## 1 is the first frame
a = frame.strokes.new()
a.draw_mode = '3DSPACE'
a.colorname = 'blue'
a.draw_cyclic = True
a.line_width = 8
a.points.add( count = 360 )

Drawing the Circle

The circle is drawn by looping 360 times, where i is an incrementing number from 0 to 359. math.radians is required to convert from degrees to radians, because math.sin requires radians.
Noise is applied to part of the circle by the last two lines: the if is checking for when i is greater than 270, and if so then apply uniform noise on the z axis. The function uniform takes a minimum and maximum numbers to generate a random number within that range.
for i in range(360):
    a.points[i].co.x = math.sin( math.radians(i) )
    a.points[i].co.y = math.cos( math.radians(i) )
    if i > 270:
        a.points[i].co.z = uniform(-0.1, 0.1)

Drawing a Spiral

Using the same for loop above, you can easily draw a spiral simply by adding an increasing value to the z axis. Adding this line to the end of the for loop will create the image above.
    a.points[i].co.z += i * 0.01

Comments

Popular Posts