Saturday 7 June 2014

Metasequoia Script–Lots of cubes

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

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



import random

def p(*args):
  if len(args)==0:
    MQSystem.println("")
    return
  for arg in args:
    MQSystem.println(str(arg))

#drawCube_def_metal.py
doc = MQSystem.getDocument()
out = MQSystem.println

def unitcube(L=50,x=0,y=0,z=0):
 obj = MQSystem.newObject()
 num=doc.numObject
 a=num
 obj.name="cube_%d" % a 
 obj.addVertex(x-L/2.0,y-L/2.0,z+L/2.0) #0
 obj.addVertex(x+L/2.0,y-L/2.0,z+L/2.0) #1
 obj.addVertex(x+L/2.0,y+L/2.0,z+L/2.0) #2
 obj.addVertex(x-L/2.0,y+L/2.0,z+L/2.0) #3
 obj.addVertex(x-L/2.0,y-L/2.0,z-L/2.0) #4
 obj.addVertex(x-L/2.0,y+L/2.0,z-L/2.0) #5
 obj.addVertex(x+L/2.0,y+L/2.0,z-L/2.0) #6
 obj.addVertex(x+L/2.0,y-L/2.0,z-L/2.0) #7
 obj.addFace([0,3,2,1])
 obj.addFace([1,2,6,7])
 obj.addFace([4,7,6,5])
 obj.addFace([0,4,5,3])
 obj.addFace([3,5,6,2])
 obj.addFace([0,1,7,4])
 doc.addObject( obj ) 
 mat1=MQSystem.newMaterial()
 doc.addMaterial(mat1)
 num0 = doc.numMaterial
 numm=num0-1

 doc.material[numm].color.red=num0*0.00346
 doc.material[numm].color.green=0.1
 doc.material[numm].color.blue=1-num0*0.00346
 doc.material[numm].alpha=1.0
 doc.material[numm].diffuse=1.0
 doc.material[numm].ambient=0.0
 doc.material[numm].emissive=0.0
 doc.material[numm].specular=1.0
 for face in obj.face:
  face.material=numm

import traceback
import sys
cnt=0

try:
 for dist in range(0,1400,60):
  for dist2 in range(0,1400,60):
   cnt=cnt+1
   #out(str(dist))
   unitcube(40,dist-700,20+random.gauss(0,8),dist2-700)
 out(str(cnt))
except:
 info=sys.exc_info()
 p(info[0])
 p(info[1])
 p(*traceback.extract_tb(info[2]))

num = doc.numObject
out("number of cubes %d" % num)

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.

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

Lots of cubes script




No comments:

Post a Comment