Advertisement
Gspin

HeighwayDragon-blender

May 2nd, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. import bpy
  2.  
  3. l = .25     # length of each segment in the curve
  4. n = 16      # iterations of the curve-generating algorithm
  5.  
  6. # generate sequence of right-left turns
  7. new = [True]
  8. for i in range(n-1):
  9.     old = list(new)
  10.     new.append(True)
  11.     old.reverse()
  12.     for j in range(len(old)):
  13.         old[j] = not old[j]
  14.     new.extend(old)
  15. del old
  16.    
  17. # convert the sequence of turns into a sequence of vector "moves"
  18. versors = [(l, 0)]
  19. for turn in new:
  20.     vec = versors[-1]
  21.     if turn:
  22.         x = vec[1]
  23.         y = vec[0] * -1
  24.     else:
  25.         x = vec[1] * -1
  26.         y = vec[0]
  27.     versors.append((x, y))
  28. del new
  29.  
  30. # convert the sequence of vectors to a sequence of vertices
  31. vertices = [(0, 0)]
  32. for ver in versors:
  33.     lastvert = vertices[-1]
  34.     vertex = (lastvert[0] + ver[0], lastvert[1] + ver[1])
  35.     vertices.append(vertex)
  36. del versors
  37.  
  38. # create the Curve Datablock
  39. curveData = bpy.data.curves.new('myCurve', type='CURVE')
  40. curveData.dimensions = '3D'
  41. curveData.resolution_u = 2
  42.  
  43. # map vertices to spline
  44. polyline = curveData.splines.new('POLY')
  45. polyline.points.add(len(vertices) - 1)
  46. for i, coord in enumerate(vertices):
  47.     x,y = coord
  48.     polyline.points[i].co = (x, y, 0, 1)
  49. del vertices
  50.  
  51. # create Object
  52. curveOB = bpy.data.objects.new('myCurve', curveData)
  53.  
  54. # attach to scene and validate context
  55. scn = bpy.context.scene
  56. scn.objects.link(curveOB)
  57. scn.objects.active = curveOB
  58. curveOB.select = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement