Skip to content

Instantly share code, notes, and snippets.

@rockjail
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rockjail/7eef80e2b27545bebdcc to your computer and use it in GitHub Desktop.
Save rockjail/7eef80e2b27545bebdcc to your computer and use it in GitHub Desktop.
# !/usr/bin/env python
#sunFlower
# base on https://commons.wikimedia.org/wiki/File:SunflowerModel.svg
# also because of http://community.thefoundry.co.uk/discussion/topic.aspx?f=4&t=114228
# main thread http://community.thefoundry.co.uk/discussion/topic.aspx?f=4&t=114228
# small changes to include the orientation
# eulerToMatrix function from here: https://gist.github.com/Farfarer/4fb11fc8d4290241e92b
import lx
import math
# Given Euler agnles in radians, return a rotation matrix.
def eulerToMatrix (x=0.0, y=0.0, z=0.0, rotOrder='XYZ'):
cx = math.cos (x)
sx = -math.sin (x)
cy = math.cos (y)
sy = -math.sin (y)
cz = math.cos (z)
sz = -math.sin (z)
if rotOrder == 'XYZ':
return [[cy*cz, cy*sz, -sy],
[sx*sy*cz - cx*sz, sx*sy*sz + cx*cz, sx*cy],
[cx*sy*cz + sx*sz, cx*sy*sz - sx*cz, cx*cy]]
elif rotOrder == 'XZY':
return [[cy*cz, sz, -cz*sy],
[sx*sy - cx*cy*sz, cz*cx, cx*sy*sz + cy*sx],
[cy*sx*sz + cx*sy, -cz*sx, cx*cy - sx*sy*sz]]
elif rotOrder == 'YXZ':
return [[cy*cz - sx*sy*sz, cy*sz + cz*sx*sy, -cx*sy],
[-cx*sz, cx*cz, sx],
[cy*sx*sz + cz*sy, sy*sz - cy*cz*sx, cx*cy]]
elif rotOrder == 'YZX':
return [[cy*cz, cx*cy*sz + sx*sy, cy*sx*sz - cx*sy],
[-sz, cx*cz, cz*sx],
[cz*sy, cx*sy*sz - cy*sx, sx*sy*sz + cx*cy]]
elif rotOrder == 'ZXY':
return [[sx*sy*sz + cy*cz, cx*sz, cy*sx*sz - cz*sy],
[cz*sx*sy - cy*sz, cx*cz, sy*sz + cy*cz*sx],
[cx*sy, -sx, cx*cy]]
elif rotOrder == 'ZYX':
return [[cy*cz, cx*sz + cz*sx*sy, sx*sz - cx*cz*sy],
[-cy*sz, cx*cz - sx*sy*sz, cx*sy*sz + cz*sx],
[sy, -cy*sx, cx*cy]]
def SunFlower(num):
lx.eval('layer.new')
lx.eval('vertMap.new Transform xfrm false {0.78 0.78 0.78} 1.0')
lyr = lx.eval('query layerservice layers ? main')
lx.eval('select.type vertex')
a = math.pi * (3 - math.sqrt(5))
for n in range(1, num, 1):
r = math.sqrt(n)
t = a * n
lx.out(math.degrees(t))
rotMat = [item for sublist in eulerToMatrix(y=t, rotOrder='ZXY') for item in sublist]
lx.out(rotMat)
x = r * math.cos(t)
z = r * math.sin(t)
lx.eval('vert.new %s %s %s' % (x, 0, z))
lx.eval('select.element %s vertex set %s'%(lyr,n-1))
for i,k in enumerate(rotMat):
lx.eval('vertMap.setValue transform {%s} {%s}'%(i,k))
SunFlower(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment