A collection of Python scripts and add-ons to automate common blender tasks.
automated-lod-generation-demo.mp4
- Clone the repo
git clone [email protected]:alexknutson/Automate-Blender-Scripts.git
- Add the
_LODGenerator.py
as an add-on
- Clone the repo
git clone [email protected]:alexknutson/Automate-Blender-Scripts.git
- Switch to the
Scripting
tab in Blender - Click the folder button to open a new script
- Navigate to the script you want to run and open it
- Select any objects you want the script to work on
- Click the "Play" button and let the magic happen
import bpy
# Loop over all materials in the project.
for material in bpy.data.materials:
material.use_backface_culling = True
Blender-Replace-Material-By-Name.mp4
import bpy
# Cache a reference to all selected objects.
objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
def replace_material(object,old_material,new_material):
"""
replace a material in blender.
params:
object - object for which we are replacing the material
old_material - The old material name as a string
new_material - The new material name as a string
"""
ob = object
om = bpy.data.materials[old_material]
nm = bpy.data.materials[new_material]
# Iterate over the material slots and replace the material
for s in ob.material_slots:
if s.material.name == old_material:
s.material = nm
# Loop through all selected objects.
for obj in objs:
for slot in obj.material_slots:
if (slot.name == "OLD-MATERIAL"):
replace_material(obj, "OLD-MATERIAL", "NEW-MATERIAL")
import bpy
# Cache a reference to all selected objects.
objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
for obj in objs:
bpy.context.view_layer.objects.active = obj
bpy.ops.object.material_slot_remove_unused()
Unnamed material slots are removed from all selected objects and only the named materials remain.
import bpy
# Cache a reference to all selected objects.
objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
for obj in objs:
for x in obj.material_slots:
if x.name == "":
bpy.context.view_layer.objects.active = obj
obj.active_material_index = x.slot_index
bpy.ops.object.material_slot_remove()
print("Removed empty material from... " + obj.name)
Any object with "Material.001" assigned would have it replaced with "Material"
import bpy
mats = bpy.data.materials
for obj in bpy.data.objects:
for slt in obj.material_slots:
part = slt.name.rpartition('.')
if part[2].isnumeric() and part[0] in mats:
slt.material = mats.get(part[0])
BEFORE: blue, white, red, green, black. AFTER: black, blue, green, red, white
import bpy
context = bpy.context
ob = context.object
ob_mats = [mat.name for mat in ob.material_slots]
ob_mats.sort()
for i, mat_name in enumerate(ob_mats):
# set active material slot to end slot
ob.active_material_index = len(ob.material_slots)-1
while ob.active_material.name != mat_name:
ob.active_material_index -=1
while ob.active_material_index > i:
bpy.ops.object.material_slot_move(direction='UP')