Skip to content

Instantly share code, notes, and snippets.

View CGArtPython's full-sized avatar
👋

CGArtPython

👋
View GitHub Profile
@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)
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
# 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)
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)
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
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.
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
"""
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
@CGArtPython
CGArtPython / simple_cube_location_animation.py
Created June 1, 2023 03:19
Beginner Blender Python tutorial code for a cube location animation (link to video: https://www.youtube.com/watch?v=nmJqIaSZlRs&lc=Ugw4q26HfFYOa0zv1Wh4AaABAg)
# give Python access to Blender's functionality
import bpy
# add a cube into the scene
bpy.ops.mesh.primitive_cube_add()
# get a reference to the currently active object
cube = bpy.context.active_object
# insert keyframe at frame one
start_frame = 1
@CGArtPython
CGArtPython / blender_pie_menu_template_tut.py
Created May 30, 2023 08:29
Code for a YouTube tutorial "Exploring the Pie Menu Template in Blender with Python" (video link: https://youtu.be/zAMVFPr8ZIE)
'''
This code is based on a built-in Blender Python Tempalte - "UI Pie Menu"
'''
bl_info = {
"name": "Pie Menu: Template",
"description": "Pie menu example",
"author": "Viktor Stepanov",
"version": (0, 1, 1),
"blender": (2, 80, 0),
"location": "3D View",
@CGArtPython
CGArtPython / beveling_a_single_edge.py
Created April 22, 2023 16:43
Beginner Blender Python tutorial code for beveling a single edge
import bpy
import bmesh
bpy.ops.mesh.primitive_cube_add()
cube_obj = bpy.context.active_object
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action="DESELECT")
bm = bmesh.from_edit_mesh(cube_obj.data)