I have pre-defined the material I want my addon to use when drawing with the grease pencil. I am drawing on grayscale images, so I need a bright color to show up while drawing, in addition to after the strokes are created. (In Blender 2.79 this was solved as described here.)
I cannot find this referenced anywhere in the 2.8 manuals, but it must be possible, so I hope I’ve just missed something.
# Create material for grease pencil
gp_mat = bpy.data.materials.new("my_gp_material")
gp_mat.use_nodes = True
BSDF_node = gp_mat.node_tree.nodes["Principled BSDF"]
BSDF_node.inputs["Base Color"].default_value = (0, 1, 0, 1)
# Set up grease pencil
bpy.ops.object.gpencil_add(type='EMPTY')
bpy.ops.object.mode_set(mode='PAINT_GPENCIL')
# Assign the material to the grease pencil for drawing
### WHAT GOES HERE??
# Draw, using the material defined
bpy.ops.gpencil.draw(wait_for_input=False)
import bpy
context = bpy.context
space = context.space_data
# Create material for grease pencil
if "Bright Material" in bpy.data.materials.keys():
gp_mat = bpy.data.materials["Bright Material"]
else:
gp_mat = bpy.data.materials.new("Bright Material")
if not gp_mat.is_grease_pencil:
bpy.data.materials.create_gpencil_data(gp_mat)
gp_mat.grease_pencil.color = (1, 0, 0.818649, 1)
# Add grease pencil object
gp_data = bpy.data.grease_pencils.new("Bright Pencil")
gp_ob = bpy.data.objects.new("Bright Pencil", gp_data)
context.scene.collection.objects.link(gp_ob)
if space.local_view:
gp_ob.local_view_set(space, True)
for ob in context.selected_objects:
ob.select_set(False)
gp_ob.select_set(True)
context.view_layer.objects.active = gp_ob
bpy.ops.object.mode_set(mode='PAINT_GPENCIL')
# Assign the material to the grease pencil for drawing
gp_data.materials.append(gp_mat)
# Draw, using the material defined
bpy.ops.gpencil.draw(wait_for_input=False)