Skip to content

Instantly share code, notes, and snippets.

View CGArtPython's full-sized avatar
👋

CGArtPython

👋
View GitHub Profile
@CGArtPython
CGArtPython / colorize_faces_op.py
Created October 21, 2024 05:04
A draft of an simple add-on that colorize faces of a mesh based on this tutorial https://www.youtube.com/watch?v=WnZX--idSQI&lc=UgzNlDw63xzAORn-3Q14AaABAg
# give Python access to Blender's functionality
import bpy
# give Python access to Blender's mesh editing functionality
import bmesh
# extend Python functionality to generate random numbers
import random
# import the Operator class from the bpy.types module
@CGArtPython
CGArtPython / save_material.py
Created September 25, 2024 06:52
Save only a material into a blend file
import bpy
# Specify the material name
material_name = "ExportThisMaterial"
# Get the material from the current blend file
material = bpy.data.materials.get(material_name)
if material:
# add only the material to the blend new file
@CGArtPython
CGArtPython / adding_drivers_to_empties.py
Created August 23, 2024 07:42
This Blender Python script adds 5 empty objects to the current scene and then assigns drivers to each empty. The drivers are set to control the X location of each empty based on the X location of the next empty in the list, multiplied by 0.5.
import bpy # Import the Blender Python API
def add_empties():
empties = [] # Initialize an empty list to store the empty objects
for i in range(5): # Loop to create 5 empty objects
empty = bpy.data.objects.new(f"Empty_{i+1}", None) # Create a new empty object with a unique name
bpy.context.collection.objects.link(empty) # Link the empty object to the current collection
empties.append(empty) # Add the empty object to the list
return empties # Return the list of empty objects
@CGArtPython
CGArtPython / initial_code_rig_ui_panel.py
Created July 12, 2024 08:26
Initial code for a Blender Python tutorial about creating a simple Rig UI panel. (video link https://www.youtube.com/watch?v=quXn5VoVyAI&lc=UgxGgnYLDtaIVAFxA754AaABAg )
# give Python access to Blender's functionality
import bpy
class VIEW3D_PT_my_rig_ui(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Item'
bl_label = "My Rig UI"
bl_idname = "VIEW3D_PT_rig_ui"
@CGArtPython
CGArtPython / allign_shader_nodes.py
Created June 19, 2024 08:29
This script demonstrates how to align nodes in the Shader Editor using the built-in 'node_arrange' add-on in Blender.
"""
This script demonstrates how to align nodes in the Shader Editor using the built-in 'node_arrange' add-on in Blender.
Note: This is not the most optimal method as it relies on a bpy.ops call, but it is functional.
The script performs the following steps:
1. Checks if the 'node_arrange' add-on is enabled and enables it if it is not.
2. Defines a context manager to switch the area type to the Shader Editor.
3. Aligns the nodes of a specified material using the 'node_arrange' add-on.
@CGArtPython
CGArtPython / File Logging Addon example.py
Created May 9, 2024 07:55
A example of an addon that logs into a file
bl_info = {
"name": "File Logging Addon",
"description": "Creates a panel with three buttons that add a cube, cone, and sphere to the scene. Logs messages to a file.",
"author": "Viktor Stepanov",
"version": (0, 0, 1),
"blender": (4, 0, 0),
"location": "View3D",
"warning": "This addon is still in development.",
"wiki_url": "",
"category": "Object"
@CGArtPython
CGArtPython / final_context_advanced.py
Created May 3, 2024 07:15
Example of how context managers work for mesh editing in Blender
import contextlib
import bpy
import bmesh
class Mode:
Edit = "EDIT_MESH"
Object = "OBJECT"
@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 )
# 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)
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)
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)