B3.2: writing annotations to texture pixels

sure :slight_smile:
i be back soon with the code :wink:




def Bressenham(destBuffer,iw,x0,y0,x1,y1,color):

    dx = abs(x1 - x0)
    sx = (1 if x0 < x1 else -1)
    dy = -abs(y1 - y0)
    sy = (1 if y0 < y1 else -1)
    error = dx + dy
    
    while True:
        destBuffer[x0+y0*iw] = color

        if(x0 == x1 and y0 == y1):
            break
        
        e2 = 2 * error
        
        if(e2 >= dy):
            if(x0 == x1):
                break
            error = error + dy
            x0 = x0 + sx

        if(e2 <= dx):
            if(y0 == y1):
                break
            error = error + dx
            y0 = y0 + sy
#==========================================================================================    


def ImageWriteAnnotation(dest:bpy.types.IMAGE_MT_image,anno):
    
    iw, ih = dest.size
    
    color = [1.0,0.0,0.0,1.0] # pure RED

    dest_data = np.empty((iw*ih , 4), dtype="f4") # create buffer of the right size
    dest.pixels.foreach_get(dest_data.ravel())    # and send image bytes to it...

    if anno:
        strokes = anno.active_frame.strokes
        for i, stroke in enumerate(strokes):
            first=True
            for pt in stroke.points:
#                print(f"stroke index: {i} point loc: {pt.co}")
                if((not math.isnan(pt.co[0]))):
                    curX = int(pt.co[0]*iw)
    #                print(destX)
                    curY = int(pt.co[1]*ih)
    #                pixelOffset = destX+destY*iw
    #                dest_data[pixelOffset] = color
                    if(first==False):
                        Bressenham(dest_data,iw,prevX,prevY,curX,curY,color)
                        
                    prevX = curX
                    prevY = curY
                    
                    first=False
                            
    dest.pixels.foreach_set(dest_data.ravel())
    dest.update()
#==========================================================================================  




#**************************************************
#
# The class method called at 'send GP to Image'
# button press
#
#**************************************************
class MyWriteGreasePencilToTexture (bpy.types.Operator):
    bl_idname = "carcass.writegptotexture"
    bl_label = "Write GP to pixels"
    
    def execute(self, context):

        imageName = GetImageInUVEditor()
        annotationName = GetAnnotationNameForImage(imageName)

        print("Writing annotation "+annotationName+" on image "+imageName)
        
        img = bpy.data.images[imageName]
        
        annotationLayer = get_anno(annotationName)

        ImageWriteAnnotation(img,annotationLayer)
        

        return {'FINISHED'}

Here’s part of the whole code :slight_smile:

No doubt this could be greatly accelerated. I just simply don’t need it to be faster; i’m okay with the speed of this piece of code :wink:

Hope you like it !

Happy blending !

1 Like