1
$\begingroup$

I'm using Grease Pencil to draw a cuboid on top of an existing scene. I'm then rendering it in Cycles.

How can I make the stroke to be of red color? (currently, it turns out light gray) I've tried setting material_index, but I guess I misconfigured my material. How can I do it correctly?

Also, what units are line_width?

Thank you!

def draw_line(gp_frame, p0, p1, line_width = 10000):
        gp_stroke = gp_frame.strokes.new()
        gp_stroke.line_width = line_width
        gp_stroke.display_mode = '3DSPACE'
        gp_stroke.points.add(count=2)
        gp_stroke.points[0].co = p0
        gp_stroke.points[1].co = p1
    
    verts, edges = ..., ...
    bpy.ops.object.gpencil_add(location=(0, 0, 0), rotation = (math.pi / 2, 0, 0), type='EMPTY')
    gp = bpy.context.scene.objects[-1]
    gp_layer = gp.data.layers.new('gplayer', set_active=True)
    gp_frame = gp_layer.frames.new(0)
    for u, v in edges:
        draw_line(gp_frame, verts[u], verts[v])
$\endgroup$
2
  • $\begingroup$ If you have obtained this script snippet from elsewhere, please provide a link. $\endgroup$
    – batFINGER
    Commented Aug 17, 2021 at 13:30
  • $\begingroup$ Originally, I used the code from towardsdatascience.com/…, but then simplified it a lot $\endgroup$ Commented Aug 18, 2021 at 9:16

1 Answer 1

1
$\begingroup$

I came up with the following solution (note create_gpencil_data, grease_pencil.color, use_lights):

    material = bpy.data.materials.new("mymaterial")
    bpy.data.materials.create_gpencil_data(material)
    material.grease_pencil.color = (1, 0, 0, 1) # color red

    bpy.ops.object.gpencil_add(location=(0, 0, 0), rotation = (0, 0, 0), type='EMPTY')
    gp = bpy.context.scene.objects[-1]
    gp.active_material = material
    gp_layer = gp.data.layers.new('gplayer', set_active=True)
    gp_layer.use_lights = False
    gp_frame = gp_layer.frames.new(0)
    for u, v in edges:
        draw_line(gp_frame, verts[u], verts[v])
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .