Skip to content

Instantly share code, notes, and snippets.

@Dylancyclone
Last active May 12, 2020 03:27
Show Gist options
  • Save Dylancyclone/f6ddaa6ecaf088faa49848c5fc0489dc to your computer and use it in GitHub Desktop.
Save Dylancyclone/f6ddaa6ecaf088faa49848c5fc0489dc to your computer and use it in GitHub Desktop.
This is a small blender script to convert simulated hair particles into a series of curves that have a bevel applied to them. This script was used for this project: https://youtu.be/rIxeJ6HQrrE
import bpy
depsgraph = bpy.context.evaluated_depsgraph_get()
object = bpy.data.objects['Source'] # Object that has the hair simulation
taper = bpy.data.objects['Taper'] # Curve each generated curve object should use as a taper
particles_coll = bpy.data.collections.new(name="particles")
bpy.context.scene.collection.children.link(particles_coll)
start_frame = bpy.context.scene.frame_start
end_frame = bpy.context.scene.frame_end
for frame in range(start_frame, end_frame + 1):
bpy.context.scene.frame_set(frame)
depsgraph.update()
bpy.context.view_layer.objects.active = object
object.select_set(True)
bpy.ops.object.modifier_convert(modifier="ParticleSettings")
for obj in bpy.context.scene.objects:
if obj.type == 'MESH' and obj.name.lower() == 'mesh':
obj.name = str(frame)
particles_coll.objects.link(obj)
obj.hide_viewport = True
obj.hide_render = True
obj.keyframe_insert("hide_viewport", frame = frame-1)
obj.keyframe_insert("hide_render", frame = frame-1)
obj.keyframe_insert("hide_viewport", frame = frame+1)
obj.keyframe_insert("hide_render", frame = frame+1)
obj.hide_viewport = False
obj.hide_render = False
obj.keyframe_insert("hide_viewport", frame = frame)
obj.keyframe_insert("hide_render", frame = frame)
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.convert(target='CURVE')
for obj in bpy.context.scene.objects:
if obj.name.lower() == str(frame):
obj.data.bevel_depth = .3 # Bevel amount, change to set the base thickness of the curves
obj.data.taper_object = taper
bpy.ops.object.select_all(action='DESELECT')
print("frame {} processed".format(frame))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment