Skip to content

Instantly share code, notes, and snippets.

@jmpinit
Created May 16, 2023 13:14
Show Gist options
  • Save jmpinit/30d9422af95f8fdc8a2345ffbebcdefd to your computer and use it in GitHub Desktop.
Save jmpinit/30d9422af95f8fdc8a2345ffbebcdefd to your computer and use it in GitHub Desktop.
Draw a squiggle in Blender's Grease Pencil mode via the Python API
import bpy
# Create a new Grease Pencil object to hold the drawing
bpy.ops.object.gpencil_add(location=(0, 0, 0))
grease_pencil = bpy.context.object
grease_pencil.name = "ScriptedGreasePencil"
layer = grease_pencil.data.layers.new("Layer")
grease_pencil.data.layers.active = layer
frame = layer.frames.new(1)
stroke = frame.strokes.new()
# Set the thickness of the stroke (optional)
stroke.line_width = 100
# Define the points of the squiggle
points = [(0, 0), (1, 1), (2, -1), (3, 0)]
# Add points to the stroke
for x, y in points:
stroke.points.add(1)
stroke.points[-1].co = (x, y, 0)
# Update the view to see the changes
bpy.context.view_layer.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment