1
$\begingroup$

Refactoring this post to simplify the request:

I need to draw a straight line/continuous Stroke in Python with Texture Paint. Unfortunately it is currently only placing a single dot at either end of the stroke.

Desired Outcome:

enter image description here

Current Code does this:

enter image description here

Tried:

  • Using "location" & "mouse_event" instead of mouse
  • Making "name" unique
  • Using different Stroke settings in Texture Paint (like Line)
  • UV unwrapping
  • Every combination of "is_start" for each stroke

I've seen multiple posts about this with no solutions, is bpy.ops.paint.image_paint just broken?

Code:

import bpy

x1 = 500
x2 = 800
# the line should be horizontal, ie 500-800px

strokes = [{"name": "",
    "is_start": True,
    "location": (0,0,0),
    "mouse":(x1, 500),
    "mouse_event":(0,0),
    "pen_flip":False,
    "pressure":1.0,
    "size":10,
    "time":0,
    "x_tilt":0.0,
    "y_tilt":0.0}, 

    {"name": "",
    "is_start": False,
    "location": (0,0,0),
    "mouse":(x2, 500),
    "mouse_event":(0,0),
    "pen_flip":False,
    "pressure":1.0,
    "size":10,
    "time":0,
    "x_tilt":0.0,
    "y_tilt":0.0}]  
    
bpy.ops.object.mode_set(mode='TEXTURE_PAINT')

areas  = [area for area in bpy.context.window.screen.areas if area.type == 'VIEW_3D']

with bpy.context.temp_override(
    window=bpy.context.window,
    area=areas[0],
    region=[region for region in areas[0].regions if region.type == 'WINDOW'][0],
    screen=bpy.context.window.screen
):
    bpy.ops.paint.brush_select(vertex_tool="DRAW", toggle=False)
    bpy.ops.paint.image_paint(stroke=strokes)
$\endgroup$
4
  • $\begingroup$ I'm on Blender 3.6 and nothing happens. Are you sure your code works? the bpy.ops statement will even error out as poll context fail. so i updated it to be executed in the right context. it runs without errors now but nothing happens. Not even a dot gets painted. $\endgroup$
    – Harry McKenzie
    Commented Aug 9, 2023 at 14:17
  • $\begingroup$ thanks for adding the context override, i was in the middle of adding it but you were faster ;) the rest should work, it works on my end (it's an operator in a bigger addon), i just don't understand why it doesn't paint a continuous line, i've tried in 3.3 and 3.6 edit: try adding a plane, and zooming in so the plane covers the full 3d view, then try running the operator, since it's using fixed x/y values if you're zoomed out it might be "missing" the mesh $\endgroup$
    – nytemairqt
    Commented Aug 9, 2023 at 14:17
  • $\begingroup$ oooh ok. cool now i see the 2 dots drawn. now something i can work on. $\endgroup$
    – Harry McKenzie
    Commented Aug 9, 2023 at 14:20
  • $\begingroup$ sweet, i appreciate you taking the time! $\endgroup$
    – nytemairqt
    Commented Aug 9, 2023 at 14:21

1 Answer 1

0
$\begingroup$

I think it's not the way it works where we expected it to fill the gap. You have to manually define the points where a stroke will be applied:

import bpy

points = []

for i in range(0,800):
    points.append((i*6,500))

strokes = []
for i, point in enumerate(points):
    stroke = {
        "name": "",
        "is_start": True,
        "location": (0, 0, 0),
        "mouse": point,
        "mouse_event": (0, 0),
        "pen_flip": False,
        "pressure": 1.0,
        "size": 10,
        "time": 0,
        "x_tilt": 0.0,
        "y_tilt": 0.0
    }
    strokes.append(stroke)

bpy.ops.object.mode_set(mode='TEXTURE_PAINT')

areas  = [area for area in bpy.context.window.screen.areas if area.type == 'VIEW_3D']

with bpy.context.temp_override(
    window=bpy.context.window,
    area=areas[0],
    region=[region for region in areas[0].regions if region.type == 'WINDOW'][0],
    screen=bpy.context.window.screen
):
    bpy.ops.paint.brush_select(image_tool="DRAW", toggle=False)
    bpy.ops.paint.image_paint(stroke=strokes)
$\endgroup$
3
  • $\begingroup$ damn unfortunately to get a rectangle I'd have to call this line operator with a 1px size a few hundred times using a nested loop and it's just extremely slow, I was hoping Blender had a way to draw the strokes continuously i appreciate your help anyway! $\endgroup$
    – nytemairqt
    Commented Aug 9, 2023 at 15:19
  • $\begingroup$ but im not 100% sure if there is another way, there is unfortunately not enough documentation on this. $\endgroup$
    – Harry McKenzie
    Commented Aug 9, 2023 at 15:21
  • 1
    $\begingroup$ ill dig into the source a bit and see if there's anything but I don't have high hopes considering the previous posts asking similar questions $\endgroup$
    – nytemairqt
    Commented Aug 9, 2023 at 15:24

You must log in to answer this question.

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