1
$\begingroup$

I am in blender 2.79b. I would want to draw grease pencil lines by feeding in the points_list calculated in a programatic manner.

When used in "Poly" mode, this is the info output:

bpy.ops.gpencil.draw(mode='DRAW_POLY', 
                     stroke=[{"name":"", 
                              "location":(0, 0, 0), 
                              "mouse":(451, 503), 
                              "pressure":1, 
                              "size":0, 
                              "pen_flip":False, 
                              "time":0, 
                              "is_start":False}, 
                             {"name":"", 
                              "location":(0, 0, 0), 
                              "mouse":(429, 415), 
                              "pressure":1, 
                              "size":0, 
                              "pen_flip":False, 
                              "time":0, 
                              "is_start":False}, 
                             {"name":"", 
                              "location":(0, 0, 0), 
                              "mouse":(363, 347), 
                              "pressure":1, 
                              "size":0, 
                              "pen_flip":False, 
                              "time":0, 
                              "is_start":False}, 
                             {"name":"", 
                              "location":(0, 0, 0), 
                              "mouse":(407, 295), 
                              "pressure":1, 
                              "size":0, 
                              "pen_flip":False, 
                              "time":0, 
                              "is_start":False}, 
                             {"name":"", 
                              "location":(0, 0, 0), 
                              "mouse":(527, 219), 
                              "pressure":1, 
                              "size":0, 
                              "pen_flip":False, 
                              "time":0, 
                              "is_start":False}])

but when executed in console, it is giving me an error:

RuntimeError: Operator bpy.ops.gpencil.draw.poll() Failed to find Grease Pencil data to draw into

What steps am I missing to activate the grease pencil , so that it can work from a script?

$\endgroup$
1
$\begingroup$

Don't use the operator

The error is alerting you that there is no current grease pencil assigned to that scene, and with no gp it doesn't poll. Have a hunch tho, that adding one will lead to next poll error of incorrect area, when script is not invoked, or overridden with context of a 3d view. See here

The operator command bpy.ops.gpencil.draw is using 2d coordinates of the 3d viewport they were drawn in. These will change based on the orientation of the view. Would be far better off adding grease pencil using the API methods available. (note add links). Each point can be given a specific 3S location. This is what the UI is doing for you, akin to placing 3d cursor

Similarly to Mr @TLousky's Answer, without using any operators add a grease pencil, layer, frame stroke and finally points to draw a 2 x 2 square on the XY plane.

import bpy

context = bpy.context
scene = context.scene
gp = scene.grease_pencil
if not gp:
    gp = bpy.data.grease_pencil.new("GP")
    scene.grease_pencil = gp

# Reference grease pencil layer or create one of none exists
if gp.layers:
    gpl = gp.layers[0]
else:
    gpl = gp.layers.new('gpl', set_active = True )

# Reference active GP frame or create one of none exists    
if gpl.active_frame:
    fr = gpl.active_frame
else:
    fr = gpl.frames.new(0) 

# Create a new stroke
str = fr.strokes.new()
str.draw_mode = '3DSPACE'
str.line_width = 5 # default 3

points = [(-1, -1, 0),
          (-1, 1, 0),
          (1, 1, 0),
          (1, -1, 0),
          (-1, -1, 0)]

str.points.add(len(points))
for p0, p in zip(points, str.points):
    p.co = p0
| improve this answer | |
$\endgroup$
  • $\begingroup$ I would want to work in DRAW_POLY mode. In the code that u mentioned , where do I have to add that ? $\endgroup$ – Nitish Reddy Oct 1 '18 at 8:56
  • $\begingroup$ got it. Thanks for the clarification @batFinger $\endgroup$ – Nitish Reddy Oct 1 '18 at 10:26
  • $\begingroup$ Cheers. I always try for an API alternative to an operator. To find how API works I mostly jump straight to autocomplete in python console and fiddle, then to the manual, then the net. Consider setting up a class, to handle your gp drawing, with a class method (for example) def draw_poly(self, points, closed=Fase): And cheers in that I have a project where having answered this realize the gp would be ideal. $\endgroup$ – batFINGER Oct 1 '18 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.