Can someone can help me to simplify a stroke in grease pencil edit mode?
I'm looking for the equivalent of this command used on curves:
but in Python and for Grease Pencil objects.
UPDATE: The original question has been clarified in a comment on my answer. This answers the question in the comment.
If you already have a Grease Pencil object set up then you can simply call stroke_simplify
with that object selected:
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Stroke'].select_set(True)
bpy.context.view_layer.objects.active = object
bpy.ops.gpencil.editmode_toggle()
bpy.ops.gpencil.stroke_simplify()
This assumes you are starting in object mode.
EDIT: I've left the original answer, in case anyone has a need for it.
Grease pencil is a bit complex, even as bpy subsystems go, so you may want to watch the tutorial series that starts with this video before you try working with Grease pencil in Python.
Basically, before you can draw you need to create a GPencil
object, configure it, and give it one or more materials. Then you can draw strokes on that object. Here's an example, taken from the tutorial:
gpencil_data = bpy.data.grease_pencils.new("GPencil")
gpencil = bpy.data.objects.new(gpencil_data.name, gpencil_data)
bpy.context.collection.objects.link(gpencil)
gp_layer = gpencil_data.layers.new("lines")
gp_frame = gp_layer.frames.new(bpy.context.scene.frame_current)
gp_stroke = gp_frame.strokes.new()
gp_stroke.line_width = 12
gp_stroke.start_cap_mode = 'ROUND'
gp_stroke.end_cap_mode = 'ROUND'
gp_stroke.use_cyclic = True
pts = [(0.0, 0.0, -1.0), (0.0, 0.0, 1.0), (-1.0, 0.0, -0.5), (0.5, 0.0, -0.5)]
gp_stroke.points.add(len(pts))
for item, value in enumerate(pts):
gp_stroke.points[item].co = value
mat = bpy.data.materials.new(name="Black")
bpy.data.materials.create_gpencil_data(mat)
gpencil.data.materials.append(mat)
mat.grease_pencil.show_fill = True
mat.grease_pencil.fill_color = (1.0, 0.0, 1.0, 1.0)
mat.grease_pencil.color = (0.0, 0.0, 1.0, 1.0)
gp_stroke.points[0].pressure = 10
gp_stroke.points[0].vertex_color = (1.0, 0.0, 0.0, 1.0)
gp_stroke.points[-1].pressure = 10
gp_stroke.points[-1].vertex_color = (0.0, 1.0, 0.0, 1.0)
If you run the example, it will create an object, GPencil
, and then draw some strokes in it, resulting in this:
This is a bit confusing, because the code doesn't generate a Stroke material, so Blender defaults to a Blue color, and while the code generates a material that it calls Black
, it doesn't set the material properties, so it ends up with a pink material for the Fill.
stroke_simplify()
on the selected closed curve. You don't need the for loop in your example because you've only got one gpencil object selected.
$\endgroup$
Commented
May 28, 2022 at 20:04