Skip to content

Instantly share code, notes, and snippets.

View CGArtPython's full-sized avatar
👋

CGArtPython

👋
View GitHub Profile
@CGArtPython
CGArtPython / blender_driver_functions_persistent_library.py
Created November 21, 2023 21:32
An add-on example for creating a persistent library of Python-powered Blender driver functions (find out more about Blender Python drivers here https://www.youtube.com/watch?v=yiMGxlRv8h4&lc=UgwlEGLUyWsiKhPPlml4AaABAg )
View blender_driver_functions_persistent_library.py
# name this file __init__.py
import bpy
ADDON_NAME = "My Diver Library"
bl_info = {
"name": "My Diver Library",
"author": "Viktor Stepanov",
"version": (0, 0, 1),
@CGArtPython
CGArtPython / get_selected_verts_via_list_comprehension.py
Created September 11, 2023 07:13
Blender Python get selected verts via list comprehension (video tutorial here: https://youtu.be/N3U2noAHgBo)
View get_selected_verts_via_list_comprehension.py
import bpy
import bmesh
# get a reference to the active object
mesh_obj = bpy.context.active_object
# create a new bmesh
bm = bmesh.from_edit_mesh(mesh_obj.data)
selected_verts = [vert for vert in bm.verts if vert.select]
@CGArtPython
CGArtPython / bmesh_icosphere.py
Created September 11, 2023 07:10
Creating an icosphere using the Bmesh Blender Python module. (video tutorial here: https://youtu.be/N3U2noAHgBo)
View bmesh_icosphere.py
import bpy
import bmesh
obj_name = "my_shape"
# create the mesh data
mesh_data = bpy.data.meshes.new(f"{obj_name}_data")
# create the mesh object using the mesh data
mesh_obj = bpy.data.objects.new(obj_name, mesh_data)
@CGArtPython
CGArtPython / create_shape_from_scratch_with_bmesh.py
Created September 11, 2023 07:09
Creating a pyramid from scratch using the Bmesh Blender Python module. (video tutorial here: https://youtu.be/N3U2noAHgBo)
View create_shape_from_scratch_with_bmesh.py
import bpy
import bmesh
obj_name = "my_shape"
# create the mesh data
mesh_data = bpy.data.meshes.new(f"{obj_name}_data")
# create the mesh object using the mesh data
mesh_obj = bpy.data.objects.new(obj_name, mesh_data)
@CGArtPython
CGArtPython / use_custom_displacement_image_texture_in_displacement_modifier.py
Created September 8, 2023 07:15
Use a custom displacement image texture in a displacement modifier
View use_custom_displacement_image_texture_in_displacement_modifier.py
# downlaod and extract example image from there https://polyhaven.com/a/mud_cracked_dry_03
import bpy
# set path to the folder where the texture is
image_folder_path = r'c:\path\to\your\textures'
# set the name of the displacement texture image
image_name = 'name_of_your_texture.png'
# load the image
@CGArtPython
CGArtPython / bmesh_bevel_from_object_mode_mesh.py
Created August 28, 2023 07:16
Blender Python script that bevels the edges of an active object that is in OBJECT mode (see tutorial here: https://youtu.be/TFQMNcTj5Jw)
View bmesh_bevel_from_object_mode_mesh.py
import bpy
import bmesh
# get a reference to the active object
mesh_obj = bpy.context.active_object
# create a new bmesh
bm = bmesh.new()
# initialize the bmesh data using the mesh data
@CGArtPython
CGArtPython / bmesh_bevel_from_edit_mode_mesh.py
Created August 28, 2023 07:15
Blender Python script that bevels the edges of an active object that is in EDIT mode (see tutorial here: https://youtu.be/TFQMNcTj5Jw)
View bmesh_bevel_from_edit_mode_mesh.py
import bpy
import bmesh
# get a reference to the active object
mesh_obj = bpy.context.active_object
# create a new bmesh and initialize it from mesh data in Edit Mode
bm = bmesh.from_edit_mesh(mesh_obj.data)
bmesh.ops.bevel(
@CGArtPython
CGArtPython / update_custom_split_normals.py
Created August 24, 2023 07:53
Blender Python: seting custom split normals
View update_custom_split_normals.py
import bpy
mesh_data = bpy.context.object.data
# Create a list with the same number of elements as the number of loops (so we can do this `new_loop_normals[loop_index]`)
new_loop_normals = [None for _ in mesh_data.loops]
for face in mesh_data.polygons:
for loop_index in face.loop_indices:
new_loop_normals[loop_index] = face.normal
@CGArtPython
CGArtPython / light_obj_data_name.py
Created July 21, 2023 07:41
In Blender there are names for the object and the data that the object represents.
View light_obj_data_name.py
import bpy
bpy.ops.object.light_add(type='SUN')
light_obj = bpy.context.active_object
light.name = "light object name"
light.data.name = "light data name"
@CGArtPython
CGArtPython / random_world_sky.py
Created June 29, 2023 14:59
Blender Python: quickly generate random lighting conditions
View random_world_sky.py
"""
See full tutorial video here: https://youtu.be/ZBvoMcqdRe4
Note: set_up_world_sun_light() is avalible via the bpybb Python package.
https://github.com/CGArtPython/bpy_building_blocks
"""
import bpy
import math