Showing posts with label generative design. Show all posts
Showing posts with label generative design. Show all posts

Thursday, 29 November 2012

Yet another Generative Design Sketch in Processing.py

This is a sketch you definetly need to run to fully appreciate it.
   1 import generativedesign.Node as Node
   2 import generativedesign.Spring as Spring
   3 import generativedesign.Attractor as Attractor
   4 
   5 """
   6 part of the example files of the generativedesign library. 
   7 shows how to use the classes Node, Spring and Attractor in 3d space.
   8 """
   9 
  10 def setup():
  11     """
  12     Setup Sketch smooth should be called here
  13     """
  14     size(512, 512, P3D)
  15     lights()
  16     global nodeA, nodeB, attractor, spring
  17     smooth(8)
  18     fill(0)
  19     nodeA = Node(random(width), random(height), random(-200, 200))
  20     nodeB = Node(random(width), random(height), random(-200, 200))
  21     nodeA.setStrength(-2)
  22     nodeB.setStrength(-2)
  23     nodeA.setDamping(0.1)
  24     nodeB.setDamping(0.1)
  25     nodeA.setBoundary(0, 0, -300, width, height, 300)
  26     nodeB.setBoundary(0, 0, -300, width, height, 300)
  27     
  28     spring = Spring(nodeA, nodeB)
  29     spring.setStiffness(0.7)
  30     spring.setDamping(0.9)
  31     spring.setLength(100)
  32     
  33     attractor = Attractor(width/2, height/2, 0)
  34     attractor.setMode(Attractor.SMOOTH)
  35     attractor.setRadius(200)
  36     attractor.setStrength(5)
  37 
  38 
  39 def draw():
  40     """
  41     The processing draw loop
  42     """
  43     background(50, 50, 200)
  44     lights()
  45     ambientLight(100, 100, 100)
  46     ambient(30)
  47     specular(30)
  48     if (mousePressed == True):
  49         nodeA.x = mouseX
  50         nodeA.y = mouseY
  51         nodeA.z = mouseY - 256
  52     # attraction between nodes
  53     nodeA.attract(nodeB)
  54     nodeB.attract(nodeA)
  55     # update spring
  56     spring.update()
  57     # attract
  58     attractor.attract(nodeA)
  59     attractor.attract(nodeB)
  60     # update node positions
  61     nodeA.update()
  62     nodeB.update()
  63     # draw attractor
  64     stroke(0, 50)
  65     strokeWeight(1)
  66     noFill()
  67     line(attractor.x-10, attractor.y, attractor.x+10, attractor.y)
  68     line(attractor.x, attractor.y-10, attractor.x, attractor.y+10)
  69     ellipse(attractor.x, attractor.y, attractor.radius*2, attractor.radius*2)
  70     # draw spring
  71     stroke(255, 0, 0, 255)
  72     strokeWeight(4)
  73     line(nodeA.x, nodeA.y, nodeA.z, nodeB.x, nodeB.y, nodeB.z)
  74     # draw nodes
  75     noStroke()
  76     fill(212)
  77     pushMatrix()
  78     translate(nodeA.x, nodeA.y, nodeA.z)
  79     sphere(20)
  80     popMatrix()
  81     pushMatrix()
  82     translate(nodeB.x, nodeB.y, nodeB.z)
  83     sphere(20)
  84     popMatrix()

Another Generative Design Sketch in Processing.py

Here is another generative design sketch translated for processing.py.
   1 import generativedesign.Mesh as Mesh
   2 
   3 
   4 """
   5 part of the example files of the generativedesign library.
   6 Modified to run with processing.py
   7 """
   8 
   9 def setup():
  10     """
  11     Setup the drawing style
  12     """
  13     size(1000,1000,P3D) 
  14     # setup drawing style 
  15     background(255)
  16     noStroke()
  17     fill(0) 
  18     # setup lights
  19     lightSpecular(230, 230, 230) 
  20     directionalLight(200, 200, 200, 0.5, 0.5, -1) 
  21     specular(color(220)) 
  22     shininess(5.0) 
  23     # setup view
  24     translate(width*0.5, height*0.5)
  25     rotateX(-0.2) 
  26     rotateY(-0.5) 
  27     scale(100)  
  28     # setup Mesh, set colors and draw  
  29     myMesh = Mesh(this, Mesh.STEINBACHSCREW, 200, 200, -3.0, 3.0, -PI, PI)
  30     myMesh.setColorRange(200, 200, 50, 50, 40, 40, 100)
  31     myMesh.draw()
  32 
  33 
  34 def draw():
  35     """
  36     The processing draw loop
  37     """
  38     pass

Generative Design Sketch (How to subclass)

Here is an excellent example of sub-classing a java class from a vanilla processing library, and running it with processing.py. Currently you need to put the generativedesign library in the processing.py/libraries folder (but that might change to pick up the library from the sketchbook/libraries folder).
   1 import generativedesign.Mesh as Mesh
   2 
   3 """
   4 part of the example files of the generativedesign library.
   5 shows how to use the mesh class, if you want to define your own forms.
   6 Modified to run with processing.py
   7 """
   8 
   9 def setup():
  10     """
  11     Setup the drawing style
  12     """
  13     size(1000, 1000, P3D)    
  14     colorMode(HSB, 360, 100, 100, 100)
  15     noStroke()
  16     global myMesh
  17     myMesh = MyOwnMesh(this)
  18     myMesh.setUCount(100)
  19     myMesh.setVCount(100)
  20     myMesh.setColorRange(193, 193, 30, 30, 85, 85, 100)
  21     myMesh.update()
  22 
  23 def draw():
  24     """
  25     The processing draw loop
  26     """
  27     background(255)    
  28     # setup lights
  29     colorMode(RGB, 255, 255, 255, 100)
  30     lightSpecular(255, 255, 255) 
  31     directionalLight(255, 255, 255, 1, 1, -1) 
  32     shininess(5.0)
  33     # setup view    
  34     translate(width/2, height/2)
  35     scale(180)
  36     myMesh.draw()    
  37 
  38 
  39 class MyOwnMesh(Mesh):
  40     """
  41     A custom python class that extend the original Mesh class
  42     """
  43     
  44     def __init__(self, theParent):
  45         Mesh.__init__(self, theParent)
  46         
  47         
  48     # just override this function and put your own formulas inside
  49     def calculatePoints(self, u,  v):
  50         A = 2/3.0
  51         B = sqrt(2)      
  52         x = A *  (cos(u) * cos(2 * v) + B * sin(u) * cos(v)) * cos(u) / (B - sin(2 * u) * sin(3 * v))
  53         y = A * (cos(u) * sin(2 * v) - B * sin(u) * sin(v)) * cos(u) / (B - sin(2 * u) * sin(3 * v))
  54         z = B * cos(u) * cos(u) / (B - sin(2 * u) * sin(3 * v))
  55         return PVector(x, y, z)