Skip to content

Instantly share code, notes, and snippets.

@kirby561
Last active April 5, 2024 15:22
Show Gist options
  • Save kirby561/5769a7066842c268bfc62f07171b4eb3 to your computer and use it in GitHub Desktop.
Save kirby561/5769a7066842c268bfc62f07171b4eb3 to your computer and use it in GitHub Desktop.
Python Blender Scripts for Splined Mesh Youtube Video
#
# Replace vertex group with all vertices in a range
#
import bpy
import bmesh
def MakeBoneName(index):
if index < 10:
return "Bone.00" + str(index)
elif index < 100:
return "Bone.0" + str(index)
else:
return "Bone." + str(index)
activeObject = bpy.context.active_object
# Get the mesh data
mesh = activeObject.data
# For each bone
for i in range(100):
# The vertex group for the bone is the same as its name
# We named them all "Bone.###" so that's the group name.
boneVertexGroupName = MakeBoneName(i)
# Find the vertex group
oldVertexGroup = activeObject.vertex_groups.get(boneVertexGroupName)
# Delete the existing group
if oldVertexGroup:
activeObject.vertex_groups.remove(oldVertexGroup)
# Add a fresh one with the same name
newGroup = activeObject.vertex_groups.new(name=boneVertexGroupName)
# Select the vertices for this bone
for vert in mesh.vertices:
minX = i * 0.5 - 0.1
maxX = i * 0.5 + 0.1
# Check if the vertex is inside the bounding box
if (minX <= vert.co.x <= maxX):
# Add it to the group
newGroup.add([vert.index], 1.0, 'ADD')
#
# Extrude selected faces N times
#
import bpy
import bmesh
for i in range(99):
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(.5, 0, 0)})
#
# Create list of bones all parented to a single parent bone at algorithmic start/end location:
#
import bpy
import bmesh
def MakeBoneName(index):
if index < 10:
return "Bone.00" + str(index)
elif index < 100:
return "Bone.0" + str(index)
else:
return "Bone." + str(index)
# Create a new armature object
armature = bpy.data.armatures.new("Armature")
armature_obj = bpy.data.objects.new("Armature", armature)
# Add to scene and select it
bpy.context.collection.objects.link(armature_obj)
bpy.context.view_layer.objects.active = armature_obj
# We need to be in edit mode to edit the bones of the armature
bpy.ops.object.mode_set(mode='EDIT')
# make a single parent bone
parentBone = armature_obj.data.edit_bones.new("ParentBone")
parentBone.head = (0, 0, 0)
parentBone.tail = (0.5, 0, 0)
# Create bones
for i in range(100):
bone = armature_obj.data.edit_bones.new(MakeBoneName(i))
bone.head = (i * 0.5, 0, 0)
bone.tail = ((i + 1)*0.5, 0, 0)
bone.parent = parentBone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment