Friday, 6 June 2014

Metasequoia Script–Sierpinski Triangle Pyramid

See this post [link] for the basics of using Metasequoia’s Script Editor.

I found an interesting script at this Japanese blog [link].
 
# fractal generate

doc = MQSystem.getDocument()

num = doc.numObject
obj = doc.object[0]

# field

x = 100
y = 100
z = 100
rn = 4 # recursive number

# Primitive

p01 = MQSystem.newPoint(x/2,y,z/2)
p02 = MQSystem.newPoint(0,0,0)
p03 = MQSystem.newPoint(x,0,0)
p04 = MQSystem.newPoint(x,0,z)
p05 = MQSystem.newPoint(0,0,z)

# function fractal()

def fractal(number,p1,p2,p3,p4):

  if number == 1:
  
    #left
    
    v = []
    v.append( obj.addVertex(p1) )
    v.append( obj.addVertex(p2) )
    v.append( obj.addVertex(p3) )
    
    obj.addFace(v)
    
    #right
    
    v = []
    v.append( obj.addVertex(p1) )
    v.append( obj.addVertex(p3) )
    v.append( obj.addVertex(p4) )
    
    obj.addFace(v)
    
    # bottom
        
    v = []
    v.append( obj.addVertex(p4) )
    v.append( obj.addVertex(p3) )
    v.append( obj.addVertex(p2) )
    
    obj.addFace(v)
    
  else:
    p5 = MQSystem.newPoint((p1.x + p2.x)/2, (p1.y + p2.y)/2, (p1.z + p2.z)/2)
    p6 = MQSystem.newPoint((p2.x + p3.x)/2, (p2.y + p3.y)/2, (p2.z + p3.z)/2)
    p7 = MQSystem.newPoint((p3.x + p1.x)/2, (p3.y + p1.y)/2, (p3.z + p1.z)/2)
    p8 = MQSystem.newPoint((p3.x + p4.x)/2, (p3.y + p4.y)/2, (p3.z + p4.z)/2)
    p9 = MQSystem.newPoint((p4.x + p1.x)/2, (p4.y + p1.y)/2, (p4.z + p1.z)/2)
    p10 = MQSystem.newPoint((p4.x + p2.x)/2, (p4.y + p2.y)/2, (p4.z + p2.z)/2)
        
    fractal(number-1,p1,p5,p7,p9)
    fractal(number-1,p5,p2,p6,p10)
    fractal(number-1,p7,p6,p3,p8)
    fractal(number-1,p9,p10,p8,p4)
    fractal(number-1,p7,p8,p10,p6)
     
# endfunction fractal()

#main()

fractal(rn,p01,p02,p03,p04)
fractal(rn,p01,p04,p05,p02)

Make sure you have an empty Scene in Metasequoia by clicking File>New then open the Script Editor and paste the code into the upper input pane of the editor.

Paste the code into the Script Editor's input window

Press F5 or Script>Run to execute the script and create the object.

Execute the script to create the object




No comments:

Post a Comment