Skip to content

Files

Latest commit

 

History

History

examples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Blender.jl usage examples

Simple example

using Blender

Blender.render("simple")

simple.png

Using MeshIO.jl

using Blender
using FileIO
using MeshIO
using CoordinateTransformations
using LinearAlgebra
using Rotations

clear_objects!()

b = load("bunny.obj")

trf = LinearMap(15.f0I)  LinearMap(RotX(Float32(π)/2))
ob = add_object!("bunny", b, trf)
println(ob)

camera = Blender.context.scene.camera
camera.location.z = 6.5

Blender.render("bunny", transparent=true)
PyObject bpy.data.objects['bunny_obj']
Fra:1 Mem:16.94M (0.00M, Peak 17.19M) | Time:00:00.00 | Preparing Scene data
Fra:1 Mem:16.96M (0.00M, Peak 17.19M) | Time:00:00.00 | Preparing Scene data
Fra:1 Mem:16.96M (0.00M, Peak 17.19M) | Time:00:00.00 | Creating Shadowbuffers
Fra:1 Mem:16.96M (0.00M, Peak 17.19M) | Time:00:00.00 | Raytree.. preparing
Fra:1 Mem:17.64M (0.00M, Peak 17.64M) | Time:00:00.00 | Raytree.. building
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Raytree finished
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Creating Environment maps
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Caching Point Densities
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Sce: Scene Ve:2503 Fa:4968 La:1
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Loading voxel datasets
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Sce: Scene Ve:2503 Fa:4968 La:1
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Sce: Scene Ve:2503 Fa:4968 La:1
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Volume preprocessing
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Sce: Scene Ve:2503 Fa:4968 La:1
Fra:1 Mem:17.62M (0.00M, Peak 18.69M) | Time:00:00.00 | Sce: Scene Ve:2503 Fa:4968 La:1
Fra:1 Mem:20.53M (0.00M, Peak 21.35M) | Time:00:00.01 | Scene, Part 6-135
Fra:1 Mem:21.47M (0.00M, Peak 21.49M) | Time:00:00.01 | Scene, Part 2-135
Fra:1 Mem:21.33M (0.00M, Peak 21.49M) | Time:00:00.01 | Scene, Part 4-135
.
.
.
Fra:1 Mem:18.16M (0.00M, Peak 23.70M) | Time:00:00.04 | Scene, Part 118-135
Fra:1 Mem:17.86M (0.00M, Peak 23.70M) | Time:00:00.04 | Scene, Part 133-135
Fra:1 Mem:16.30M (0.00M, Peak 23.70M) | Time:00:00.04 | Sce: Scene Ve:2503 Fa:4968 La:1
Saved: 'bunny.png'
 Time: 00:00.09 (Saving: 00:00.05)

PyObject {'FINISHED'}

bunny.png

Colors & Materials

Here we show how to render a function, with colors provided by ColorSchemes.jl:

using Blender
using GeometryBasics
using ColorSchemes

function generate_grid(nx, ny, w=5, d=5; magf=(i,j) -> 1)
    x = range(-w, stop=w, length=nx)
    y = range(-d, stop=d, length=ny)

    points = Vector{Point3{Float64}}()
    faces = Vector{NTuple{3,Int}}()
    magnitudes = Vector{Float64}()

    for (j,x) in enumerate(x)
        for (i,y) in enumerate(y)
            m = magf(x,y)
            push!(magnitudes, m)
            push!(points, Point3(x, y, m))
        end
    end

    # Generate triangulation
    for j = 1:ny-1
        for i = 1:nx-1
            push!(faces, (i + (j-1)*nx,
                          i + 1 + (j-1)*nx,
                          i + 1 + j*nx))
            push!(faces, (i + 1 + j*nx,
                          i + j*nx,
                          i + (j-1)*nx))
        end
    end

    (mesh=(points, nothing, faces), magnitudes=magnitudes)
end

clear_objects!()

N = 0.4
magf = (x,y) -> N*sinpi(x*cospi(y/2))

g = generate_grid(300, 300, 3, 3, magf=magf)
ob = add_object!("grid", g.mesh..., smooth=true)

cmap = ColorSchemes.plasma

# Paint the vertices according to their elevation
vertex_colors!(i -> cmap[clamp((g.magnitudes[i]/N .+ 1)/2, 0, 1)], ob)

camera = Blender.context.scene.camera
camera.location.z = 4.5

Now we need to map the vertex colors to the material used for rendering, and we will use the node system of Blender, together with some convenience functions of Blender.jl to access the node connectors.

mat = new_material("Paint", use_nodes=true)
append_material!(ob, mat)

bsdf = mat.node_tree.nodes.get("Principled BSDF")
sna = mat.node_tree.nodes.new("ShaderNodeAttribute")
# To access the vertex colors, we choose this attribute
sna.attribute_name="Col"

# We can now list the inputs/outputs of e.g. the BSDF:
connections(bsdf)

# This is how we link the outputs of the attributde node to the inputs
# of the BSDF:
mat.node_tree.links.new(find_input(bsdf, "Base Color"),
                        find_output(sna, "Color"))
mat.node_tree.links.new(find_input(bsdf, "Alpha"),
                        find_output(sna, "Alpha"))
set_input!(bsdf, "Subsurface", 0.1)
set_input!(bsdf, "Metallic", 0.7)

Blender.render("grid-function")

# We can also save the current scene for further use
Blender.save("grid-function.blend")
Node "Principled BSDF"
PyObject bpy.data.materials['Paint'].node_tree.nodes["Principled BSDF"]
  # │ Input                   Output
────┼────────────────────────────────
  1 │ Base Color              BSDF
  2 │ Subsurface
  3 │ Subsurface Radius
  4 │ Subsurface Color
  5 │ Metallic
  6 │ Specular
  7 │ Specular Tint
  8 │ Roughness
  9 │ Anisotropic
 10 │ Anisotropic Rotation
 11 │ Sheen
 12 │ Sheen Tint
 13 │ Clearcoat
 14 │ Clearcoat Roughness
 15 │ IOR
 16 │ Transmission
 17 │ Transmission Roughness
 18 │ Emission
 19 │ Emission Strength
 20 │ Alpha
 21 │ Normal
 22 │ Clearcoat Normal
 23 │ Tangent

Fra:1 Mem:131.64M (Peak 132.31M) | Time:00:00.13 | Syncing Light
Fra:1 Mem:131.64M (Peak 132.31M) | Time:00:00.13 | Syncing Camera
Fra:1 Mem:131.64M (Peak 132.31M) | Time:00:00.13 | Syncing grid_obj
Fra:1 Mem:148.37M (Peak 153.84M) | Time:00:00.23 | Rendering 1 / 64 samples
Fra:1 Mem:132.01M (Peak 153.84M) | Time:00:01.71 | Rendering 26 / 64 samples
Fra:1 Mem:132.01M (Peak 153.84M) | Time:00:03.06 | Rendering 51 / 64 samples
Fra:1 Mem:132.01M (Peak 153.84M) | Time:00:03.77 | Rendering 64 / 64 samples
Saved: 'grid-function.png'
 Time: 00:04.48 (Saving: 00:00.53)

PyObject {'FINISHED'}

Info: Saved "grid-function.blend"
Info: Saved "grid-function.blend"

grid-function.png