Skip to content

Instantly share code, notes, and snippets.

View CGArtPython's full-sized avatar
👋

CGArtPython

👋
View GitHub Profile
@CGArtPython
CGArtPython / get_script_folder_path.py
Created April 17, 2023 06:11
Beginner Blender Python tutorial code for retrieving the script file path when running from Blender's Text Editor, an Add-on, or the Command Line (tutorial video: https://youtu.be/jT2EbqMfKU8)
import bpy
import pathlib
# check if we are running from the Text Editor
if bpy.context.space_data != None and bpy.context.space_data.type == "TEXT_EDITOR":
# get the path to the SAVED TO DISK script when running from blender
print("bpy.context.space_data script_path")
script_path = bpy.context.space_data.text.filepath
else:
@CGArtPython
CGArtPython / setting_op_props_on_button.py
Created March 21, 2023 08:04
[Blender Python Add-on] Creating buttons using the same operator but with different parameters (extending script from this YouTube tutorial: https://youtu.be/0_QskeU8CPo)
"""
See YouTube tutorial here: https://youtu.be/0_QskeU8CPo
+
Adding 2 more buttons that set the operator's props
"""
bl_info = {
"name": "My Custom Panel",
@CGArtPython
CGArtPython / get_all_blend_files.py
Created March 21, 2023 07:14
[Blender Python Scripting] Get a list of blend file in the same folder as the Python script file
import pathlib
import pprint
path_to_current_dir = pathlib.Path(__file__).parent
###########
# Option 1
###########
blend_files = list()
@CGArtPython
CGArtPython / linking_objects_materials_scens_between_blend_files.py
Created March 12, 2023 08:57
Beginner Blender Python tutorial code for linking objects, materials, and scens from one .blend file into another (tutorial video: https://youtu.be/ZrN9w8SMFjo)
# extend Python's functionality to work with file paths
import pathlib
# give Python access to Blender's functionality
import bpy
def remove_libraries():
"""remove the linked blend files"""
bpy.data.batch_remove(bpy.data.libraries)
@CGArtPython
CGArtPython / simple_material_with_noise.py
Last active March 5, 2023 12:41
Beginner Blender Python tutorial code for creating a simple material and using a noise mask to control the roughness (tutorial video: https://youtu.be/D6Rm4WvVvoI)
# give Python access to Blender's functionality
import bpy
# extend Python's math functionality
import math
# extend Python functionality to generate random numbers
import random
@CGArtPython
CGArtPython / create_and_apply_a_simple_material.py
Created February 20, 2023 08:04
Beginner Blender Python tutorial code for creating a simple material and tweak its base color, metallic, and roughness (tutorial https://youtu.be/TdBYf8orLA4)
# give Python access to Blender's functionality
import bpy
# extend Python functionality to generate random numbers
import random
def partially_clean_the_scene():
# select all object in the scene
bpy.ops.object.select_all(action="SELECT")
@CGArtPython
CGArtPython / empty_following_a_bezier_circle_curve.py
Created January 31, 2023 06:26
Add a Bezier circle curve and an empty, make the empty animate by following the circle curve
import bpy
last_frame = 90
# set the lenght of the animation
bpy.context.scene.frame_end = last_frame
# add a Bezier circle curve into the scene
bpy.ops.curve.primitive_bezier_circle_add()
bezier_circle_obj = bpy.context.active_object
@CGArtPython
CGArtPython / update_cube_hight.py
Created January 31, 2023 06:16
Add a cube into the scene and update the hight
import bpy
custom_size = 5
# add cube into the scene
bpy.ops.mesh.primitive_cube_add(size=custom_size)
cube_obj = bpy.context.active_object
# move the cube up half way on the Z axis
cube_obj.location.z = custom_size / 2
@CGArtPython
CGArtPython / reading_and_writing_json_files.py
Last active March 9, 2025 06:14
Reading and Writing JSON Files for Blender Python Beginners (tutorial video: https://youtu.be/f6UoW5rtrw0)
# extend Python's functionality to work with JSON files
import json
# extend Python's functionality to work with file paths
import pathlib
# extend Python's functionality to print data in a readable way
import pprint
# give Python access to Blender's functionality
@CGArtPython
CGArtPython / create_a_cube_with_bmesh.py
Created January 30, 2023 08:42
Beginner Python Exercise in Blender: Make a cube from a list of vertices (tutorial https://www.youtube.com/watch?v=mN3n9b98HMk)
import bpy
verts = [
(-1.0, -1.0, -1.0),
(-1.0, 1.0, -1.0),
(1.0, 1.0, -1.0),
(1.0, -1.0, -1.0),
(-1.0, -1.0, 1.0),
(-1.0, 1.0, 1.0),
(1.0, 1.0, 1.0),