Blender (software): Difference between revisions
No edit summary |
|||
(15 intermediate revisions by the same user not shown) | |||
Line 18: | Line 18: | ||
==Modelling== | ==Modelling== | ||
===Geometry Nodes=== | |||
[https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/index.html Blender Manual Geometry Nodes] | |||
Geometry nodes allow you to procedurally modify the geometry of the object. | |||
Some things it can do include instancing of objects, moving vertices, and arithmetic. | |||
Note that Geometry nodes are available in Blender 2.92+ with some nodes introduced in 2.93 LTS. | |||
See some references: | |||
* [https://www.youtube.com/watch?v=Lb9tJhn9EHE Making grass by SouthernShotty] | |||
* [https://www.youtube.com/watch?v=TjGL4RjR13Q Making a raspberry by Bad Normals] | |||
* [https://www.youtube.com/watch?v=52UYqe3zdxQ Making a gumdrop by Blender Guru] | |||
==Materials and Shaders== | |||
Blender uses a node system for shader which let you composite different textures and BSDFS. | |||
===Bypass BSDF=== | |||
If you wish to use a raw colors from a texture, bypassing materials altogether, you can directly attach a image texture node output to the material output.<br> | |||
For transparent textures, you will need a transparent BSDF node and a mix node with alpha connected to <code>mix.outputs["Fac"]</code> to achieve transparency. | |||
==Animations== | |||
==Rendering and Compositing== | |||
Press {{key press|F12}} to render. | |||
===Denoising=== | |||
# In the view layer properties, check <code>Denoising Data</code> | |||
# Switch to the compositor layout. | |||
# Add a denoise node, under filter. | |||
# Attach the denoising normal, denoising albedo, and denoising depth. | |||
==Scripting== | ==Scripting== | ||
Line 28: | Line 58: | ||
See one of the following resources to get started: | See one of the following resources to get started: | ||
* [https://www.youtube.com/watch?v=XqX5wh4YeRw Python Crash Course for Blender! by Curtis Holt] | * [https://www.youtube.com/watch?v=XqX5wh4YeRw Python Crash Course for Blender! by Curtis Holt] | ||
===Meshes=== | |||
<syntaxhighlight lang="python"> | |||
vertices = [(-1, -1, 0), (1, -1, 0), | |||
(-1, 1, 0), (1, 1, 0)] | |||
edges = [(0,1), (1,2), (2,3), (3,0)] | |||
faces = [(0,1,2), (1,2,3)] | |||
uvs = [(0, 0), (1, 0), | |||
(0, 1), (1, 1)] | |||
# Create a new mesh | |||
new_mesh = bpy.data.meshes.new(mesh_name) | |||
new_mesh.from_pydata(vertices, edges, faces) | |||
new_mesh.update() | |||
# Add UVs | |||
uv_layer = new_mesh.uv_layers.new() | |||
for face in new_mesh.polygons: | |||
for vert_idx, loop_idx in zip(face.vertices, face.loop_indices): | |||
uv_layer.data[loop_idx].uv = uvs[vert_idx] | |||
</syntaxhighlight> | |||
===Materials=== | |||
<syntaxhighlight lang="python"> | |||
# Create a new material | |||
my_material = bpy.data.materials.new("My New Material") | |||
my_material.use_nodes = True | |||
# Add material to an object | |||
my_object.data.materials.append(my_material) | |||
</syntaxhighlight> | |||
====Images==== | |||
<syntaxhighlight lang="python"> | |||
# Add an image | |||
image_bpy = bpy.data.images.new("my_image", width=width, height=height, alpha=(channels==4), float_buffer=False) | |||
image_bpy.pixels = np.flipud(image_np).ravel() | |||
image_tex_node = my_material.node_tree.nodes.new("ShaderNodeTexImage") | |||
image_tex_node.image = image_bpy | |||
my_material.node_tree.links.new(image_tex_node.outputs['Color'], my_bsdf.inputs['Base Color']) | |||
</syntaxhighlight> | |||
===Saving scripts outside=== | ===Saving scripts outside=== | ||
Line 39: | Line 109: | ||
exec(compile(open(filepath).read(), filename, 'exec')) | exec(compile(open(filepath).read(), filename, 'exec')) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Rendering=== | |||
<syntaxhighlight lang="python"> | |||
def render_to_file(filepath, width, height): | |||
# Set device to GPU | |||
bpy.context.scene.cycles.device = "GPU" | |||
# Set resolution | |||
bpy.context.scene.render.resolution_x = width | |||
bpy.context.scene.render.resolution_y = height | |||
bpy.context.scene.render.resolution_percentage = 100 | |||
# Set output file path | |||
bpy.context.scene.render.filepath = filepath | |||
# Do render | |||
bpy.ops.render.render(write_still=True) | |||
</syntaxhighlight> | |||
===MVP matrices=== | |||
[https://github.com/blender/blender/blob/v2.93.0/doc/python_api/examples/gpu.9.py Example python code] | |||
<syntaxhighlight lang="python"> | |||
# Model matrix | |||
my_object.matrix_world | |||
# View matrix | |||
bpy.context.scene.camera.matrix_world.inverted() | |||
# Projection matrix | |||
projection_matrix = bpy.context.scene.camera.calc_matrix_camera( | |||
bpy.context.evaluated_depsgraph_get(), x=WIDTH, y=HEIGHT) | |||
</syntaxhighlight> | |||
===Python Modules=== | |||
To add modules, install them using pip to the <code>scripts/modules</code> directory of your Blender install: | |||
<pre> | |||
python3.9 -m pip install -t=scripts/modules $MODULE | |||
</pre> | |||
* If you installed blender using snap, you will need to create another scripts directory somewhere else. | |||
==Compiling Blender== | |||
See [https://wiki.blender.org/wiki/Building_Blender/Linux/Ubuntu https://wiki.blender.org/wiki/Building_Blender/Linux/Ubuntu], | |||
Note that Blender has a [https://github.com/blender/blender github mirror] which may be faster to clone. | |||
==Resources== | ==Resources== |
Latest revision as of 16:48, 8 February 2022
Blender is an open-source 3D modelling software.
It can be used for creating 3D models for games, creating 2D/3D animations, and even creating video games.
Getting Started
Installation
- Windows
Direct Download
From chocolatey:
choco install blender
- Ubuntu
Blender is available on the Snap store
sudo snap install blender --classic
Modelling
Geometry Nodes
Geometry nodes allow you to procedurally modify the geometry of the object.
Some things it can do include instancing of objects, moving vertices, and arithmetic.
Note that Geometry nodes are available in Blender 2.92+ with some nodes introduced in 2.93 LTS.
See some references:
Materials and Shaders
Blender uses a node system for shader which let you composite different textures and BSDFS.
Bypass BSDF
If you wish to use a raw colors from a texture, bypassing materials altogether, you can directly attach a image texture node output to the material output.
For transparent textures, you will need a transparent BSDF node and a mix node with alpha connected to mix.outputs["Fac"]
to achieve transparency.
Animations
Rendering and Compositing
Press F12 to render.
Denoising
- In the view layer properties, check
Denoising Data
- Switch to the compositor layout.
- Add a denoise node, under filter.
- Attach the denoising normal, denoising albedo, and denoising depth.
Scripting
Getting Started
Blender supports scripting as an alternative means of interaction.
Most things are available in the bpy
module.
See one of the following resources to get started:
Meshes
vertices = [(-1, -1, 0), (1, -1, 0),
(-1, 1, 0), (1, 1, 0)]
edges = [(0,1), (1,2), (2,3), (3,0)]
faces = [(0,1,2), (1,2,3)]
uvs = [(0, 0), (1, 0),
(0, 1), (1, 1)]
# Create a new mesh
new_mesh = bpy.data.meshes.new(mesh_name)
new_mesh.from_pydata(vertices, edges, faces)
new_mesh.update()
# Add UVs
uv_layer = new_mesh.uv_layers.new()
for face in new_mesh.polygons:
for vert_idx, loop_idx in zip(face.vertices, face.loop_indices):
uv_layer.data[loop_idx].uv = uvs[vert_idx]
Materials
# Create a new material
my_material = bpy.data.materials.new("My New Material")
my_material.use_nodes = True
# Add material to an object
my_object.data.materials.append(my_material)
Images
# Add an image
image_bpy = bpy.data.images.new("my_image", width=width, height=height, alpha=(channels==4), float_buffer=False)
image_bpy.pixels = np.flipud(image_np).ravel()
image_tex_node = my_material.node_tree.nodes.new("ShaderNodeTexImage")
image_tex_node.image = image_bpy
my_material.node_tree.links.new(image_tex_node.outputs['Color'], my_bsdf.inputs['Base Color'])
Saving scripts outside
See using external ide and run external scripts.
By default, everything is stored in the .blend
file including your custom scripts.
If you want to store your scripts outside or use another IDE, you will need to have the script inside Blender call the script outside:
import bpy
filepath = bpy.path.abspath("//myscript.py")
exec(compile(open(filepath).read(), filename, 'exec'))
Rendering
def render_to_file(filepath, width, height):
# Set device to GPU
bpy.context.scene.cycles.device = "GPU"
# Set resolution
bpy.context.scene.render.resolution_x = width
bpy.context.scene.render.resolution_y = height
bpy.context.scene.render.resolution_percentage = 100
# Set output file path
bpy.context.scene.render.filepath = filepath
# Do render
bpy.ops.render.render(write_still=True)
MVP matrices
# Model matrix
my_object.matrix_world
# View matrix
bpy.context.scene.camera.matrix_world.inverted()
# Projection matrix
projection_matrix = bpy.context.scene.camera.calc_matrix_camera(
bpy.context.evaluated_depsgraph_get(), x=WIDTH, y=HEIGHT)
Python Modules
To add modules, install them using pip to the scripts/modules
directory of your Blender install:
python3.9 -m pip install -t=scripts/modules $MODULE
- If you installed blender using snap, you will need to create another scripts directory somewhere else.
Compiling Blender
See https://wiki.blender.org/wiki/Building_Blender/Linux/Ubuntu,
Note that Blender has a github mirror which may be faster to clone.
Resources
- Blender 2.8 Beginner Tutorial by Blender Guru
- This is an updated version of the famous donut tutorial
- See https://www.reddit.com/r/BlenderDoughnuts/
- Complete Blender Creator: Learn 3D Modelling for Beginners (Udemy, $20)