Skip to content

Instantly share code, notes, and snippets.

@behreajj
Last active May 26, 2021 01:53
Show Gist options
  • Save behreajj/d2678fd9fb6240700791e1153e4b1328 to your computer and use it in GitHub Desktop.
Save behreajj/d2678fd9fb6240700791e1153e4b1328 to your computer and use it in GitHub Desktop.
Bramble Circle in Grease Pencil
import bpy
import math
from mathutils import Vector
grease_data = bpy.data.grease_pencils.new("Stroke")
grease_layers = grease_data.layers
active_layer = grease_layers.new("Layer")
grease_frames = active_layer.frames
active_frame = grease_frames.new(
bpy.context.scene.frame_current,
active=True)
frame_strokes = active_frame.strokes
arc_count = 24
point_count = 64
i_range = range(0, arc_count, 1)
i_to_theta = math.tau / arc_count
j_range = range(0, point_count, 1)
j_to_theta = math.tau / point_count
j_to_fac = 1.0 / (point_count - 1.0)
theta_offset = math.tau / 3.0
orbit_radius = 1.0
minor_radius = 0.5
min_pressure = 0.5
max_pressure = 5.0
for i in i_range:
i_theta = i * i_to_theta
active_stroke = frame_strokes.new()
active_stroke.line_width = 12.0
active_stroke.hardness = 1.0
active_stroke.start_cap_mode = 'FLAT'
active_stroke.end_cap_mode = 'FLAT'
stroke_points = active_stroke.points
stroke_points.add(point_count)
orbit = orbit_radius
if i % 2 != 0:
orbit = orbit_radius * 0.875
origin = Vector(
(orbit * math.cos(i_theta),
orbit * math.sin(i_theta),
0.0))
j_theta_origin = math.pi + (i_theta - theta_offset)
j_theta_dest = math.pi + (i_theta + theta_offset)
for j in j_range:
j_fac = j * j_to_fac
j_theta = (1.0 - j_fac) * j_theta_origin \
+ j_fac * j_theta_dest
point = stroke_points[j]
osc = 0.5 + 0.5 * math.cos(math.tau * j_fac - math.pi)
point.pressure = (1.0 - osc) * min_pressure \
+ osc * max_pressure
point_local = Vector(
(minor_radius * math.cos(j_theta),
minor_radius * math.sin(j_theta),
0.0))
point.co = origin + point_local
grease_obj = bpy.data.objects.new(grease_data.name, grease_data)
bpy.context.collection.objects.link(grease_obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment