Skip to content

Instantly share code, notes, and snippets.

View CGArtPython's full-sized avatar
👋

CGArtPython

👋
View GitHub Profile
@CGArtPython
CGArtPython / simple_flip_geonodes_color_ramp.py
Created November 27, 2024 08:22
[Blender Python] Here is a way to flip the two edge stops on a color ramp (video explination here https://www.skool.com/cgpython/how-to-flip-a-color-ramp?p=38e4ac8e)
import bpy
class OBJECT_OT_FlipColorRamp(bpy.types.Operator):
"""Flip Color Ramp Elements"""
bl_idname = "object.flip_color_ramp"
bl_label = "Flip Color Ramp"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
try:
@CGArtPython
CGArtPython / apply_driver_to_blur_node_size_y.py
Created November 26, 2024 07:09
[Blender Python] How to apply a driver to a compositor node (video explinaiton here https://www.skool.com/cgpython/how-to-add-drivers-to-node-group-sockets-using-python?p=71dfec91 )
"""prerequisite: you have a group node in the compositor, and inside the group node you have a Blur node""
import bpy
tree = bpy.context.scene.node_tree
driver = tree.nodes['Group'].node_tree.nodes['Blur'].driver_add("size_y").driver
driver.type = 'AVERAGE'
@CGArtPython
CGArtPython / add_asset_from_user_asset_lib.py
Created November 24, 2024 21:13
[Blender Python] Add an asset from the User Asset Library into the scene (vidoe explination https://www.skool.com/cgpython/make-a-button-to-add-a-model?p=abc92a32)
import pathlib
import bpy
def find_user_asset_dir():
user_lib_index = bpy.context.preferences.filepaths.asset_libraries.find("User Library")
if user_lib_index == -1:
return None
user_asset_lib = bpy.context.preferences.filepaths.asset_libraries[user_lib_index]
@CGArtPython
CGArtPython / simple_import_fbx_op.py
Last active November 24, 2024 21:15
[Blender Python] Import a FBX model into the scene (vidoe explination https://www.skool.com/cgpython/make-a-button-to-add-a-model?p=abc92a32)
import bpy
class SimpleImportFBXOperator(bpy.types.Operator):
bl_idname = "import_scene.custom_fbx"
bl_label = "Import FBX Model"
def execute(self, context):
# WARNING: This is just an example path, please don't use hardcoded paths in production add-ons!
# Consider this more advanced solution https://gist.github.com/CGArtPython/f2b3759e27ba1b0eca112c528ebdf1c9
@CGArtPython
CGArtPython / copy_paste_drivers.py
Created November 18, 2024 08:56
[Blender Python] How to copy Drivers with Python? (explanation https://www.skool.com/cgpython/how-to-copy-drivers-with-python?p=fc7b213c)
import bpy
def copy_drivers():
# Get the active object (the one with the drivers to copy)
source_obj = bpy.context.active_object
print(f"Active object: {source_obj.name}")
if not source_obj.animation_data or not source_obj.animation_data.drivers:
print("No drivers found on the active object.")
return
@CGArtPython
CGArtPython / basic_ui_list_panel.py
Created November 18, 2024 06:41
[Blender Python] basic UI list pane (explanation https://www.skool.com/cgpython/ui-guide?p=47cf4527)
import random
import bpy
# Define a custom property group for our list items
class MyItem(bpy.types.PropertyGroup):
name: bpy.props.StringProperty(name="Item Name", default="New Item")
number: bpy.props.IntProperty(name="Item Number", default=0)
@CGArtPython
CGArtPython / example_frame_change_post.py
Created November 18, 2024 05:52
[Blender Python] Shows how the frame_change_post works (video explanation https://www.skool.com/cgpython/making-deformers?p=5f59f1ff)
import bpy
import bmesh
import mathutils
# Dictionary to store previous object positions and velocities
prev_positions = {}
velocities = {}
velocity_decay = 0.90 # Reduce velocity by 10% each iteration
@CGArtPython
CGArtPython / bmesh_edge_selection_timer.py
Created November 13, 2024 07:15
Example of selecting an edge of a mesh using a timer
import bpy
import bmesh
def select_an_edge_every_2_seconds():
obj = bpy.context.object.data
bm = bmesh.from_edit_mesh(obj)
bm.edges.ensure_lookup_table()
not_selected_edges = []
@CGArtPython
CGArtPython / create_blender_driver_variables_extended.py
Created November 11, 2024 06:02
Code for the Extended tutorial video "Easily Create Driver Variables with Blender Python" ( https://www.skool.com/cgpython/classroom/e707bee7?md=b48bf615d1a5465fb6835c4a98d2aa8b )
import bpy
def add_icosphere():
bpy.ops.mesh.primitive_ico_sphere_add()
return bpy.context.active_object
def add_empty():
bpy.ops.object.empty_add()
return bpy.context.active_object
@CGArtPython
CGArtPython / create_blender_driver_variables.py
Created November 11, 2024 06:00
Code for the tutorial video "Easily Create Driver Variables with Blender Python" (https://youtu.be/m-OFyHHY4KI)
import bpy
bpy.ops.mesh.primitive_ico_sphere_add()
ico_sphere_obj = bpy.context.active_object
bpy.ops.object.empty_add()
empty_obj = bpy.context.active_object
driver_fcurve_obj = ico_sphere_obj.driver_add("location", 0)