plot.jl - wookay/BlenderPlot.jl GitHub Wiki

export plot

function plot(f::F, start, stop; color::ColorT=RGB(1,1,0)) where {F <: Function, ColorT <: Union{RGB, RGBA}}
    # code from https://blender.stackexchange.com/a/49013
    scene = bpy.context[:scene]

    if scene[:grease_pencil] isa Void
        areas = bpy.context[:screen][:areas]
        threedviews = [a for a in areas if a[:type] == "VIEW_3D" ]
        if isempty(threedviews)
            area = last(areas)
            area[:type] = "VIEW_3D"
        else
            area = first(threedviews)
        end
        override = Dict(
            "scene"         => scene,
            "screen"        => bpy.context[:screen],
            "object"        => bpy.context[:object],
            "area"          => area,
            "region"        => area[:regions][1],
            "window"        => bpy.context[:window],
            "active_object" => bpy.context[:object]
        )
        bpy.ops[:gpencil][:data_add](override)
    end
    
    gp = scene[:grease_pencil]
    if isempty(gp[:layers])
        gpl = gp[:layers][:new]("gpl", set_active=true)
    else
        gpl = gp[:layers][1]
    end
    
    if isempty(gpl[:frames])
        fr = gpl[:frames][:new](1)
    else
        fr = gpl[:active_frame]
    end

    if isempty(gp[:palettes])
        palette = gp[:palettes][:new]("Palette")
    else
        palette = gp[:palettes][1]
    end

    c = RGBA(color)
    active_color = palette[:colors][:new]()
    active_color[:color] = (c.r, c.g, c.b)
    active_color[:alpha] = c.alpha
    palette[:colors][:active] = active_color

    strokeLength = 500
    str = fr[:strokes][:new](colorname = active_color[:name])
    str[:draw_mode] = "3DSPACE"
    str[:points][:add](count = strokeLength)
    points = str[:points]

    lin = linspace(start, stop, strokeLength)
    for (idx, point) in enumerate(points)
        point[:co] = (lin[idx], f(lin[idx]), 0)
    end
end