forked from cwright/b3d-particles-to-curves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles2curves.py
68 lines (52 loc) · 2.51 KB
/
particles2curves.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# This script converts particle systems to individual curve objects. It was
# based heavily on the particles to keyframes script by stackexchange user jasperge
# at http://blender.stackexchange.com/a/7142 , but this version gives you curve
# objects instead. Like the original script, the animation start and stop times will
# set the range of points used for the curves. Select the object you wish to convert
# prior to running the script.
#
# Got impatient wating for that new particle-node-editor feature that's been
# rumored, this seemed like the simplest stop-gap approach.
import bpy
def create_objects_for_particles(ps, totalFrames):
# create a curve object for every particle
obj_list = []
for i, _ in enumerate(ps.particles):
# create the Curve Datablock
curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.resolution_u = 2
# map particle coords at each frame to spline
polyline = curveData.splines.new('POLY')
print(totalFrames)
polyline.points.add(totalFrames-1)
#splines init with a point already
#create the container object
dupli = bpy.data.objects.new(
name="particle.{:03d}".format(i),
object_data=curveData)
bpy.context.scene.objects.link(dupli)
obj_list.append(dupli)
return obj_list
def match_and_keyframe_objects(ps, obj_list, start_frame, end_frame):
# Match and keyframe the objects to the particles for every frame in the
# given range.
for frame in range(start_frame, end_frame + 1):
bpy.context.scene.frame_set(frame)
for p, obj in zip(ps.particles, obj_list):
#print obj.data.splines.points.length()
obj.data.splines[0].points[frame].co = (p.location.x, p.location.y, p.location.z, 1)
def main():
# The active object should be the one with the particle system.
ps_obj = bpy.context.object
#this line selects the replicated object. not using for now
#obj = [obj for obj in bpy.context.selected_objects if obj != ps_obj][0]
ps = ps_obj.particle_systems[0] # Assume only 1 particle system is present.
start_frame = bpy.context.scene.frame_start
end_frame = bpy.context.scene.frame_end
total_frames = end_frame-start_frame+1
#frame inclusive
obj_list = create_objects_for_particles(ps, total_frames)
match_and_keyframe_objects(ps, obj_list, start_frame, end_frame)
if __name__ == '__main__':
main()