#!/usr/bin/env python
# MIT or general permissive
import os, sys
from sys import platform
import random
import math
def pre_amble():
s = """
"""
return s
def post_amble():
s = """
"""
return s
piece = """
"""
# 0 0 1 0 1 1 0 1
def make_piece(irow,icol,nrow,ncol):
index = irow*ncol + icol
s = piece.replace("T1","T{}".format(index))
s = s.replace("M1","M{}".format(index))
x = random.random()*20 - 10
y = random.random()*20 - 10
s = s.replace("_translation_","{} {} 1".format(x,y))
scale = random.random()*.3 -.15 + 1.0
scale = 1.0 # comment for more exitement
s = s.replace("_scale_","{} {} 1".format(scale,scale))
r = random.random()*2*math.pi - math.pi
s = s.replace("_rotation_","{}".format(r))
w = 1.0/ncol
h = 1.0/nrow
x1 = 0.0 + icol*w
x2 = x1 + w
y1 = 0.0 + irow*h
y2 = y1 + h
s = s.replace("_texture_coords_","{} {} {} {} {} {} {} {}".format(x1,y2,x1,y1,x2,y1,x2,y2))
return s
nrow = 3 #8
ncol = 3 #9
def make_puzzle():
s = ""
for irow in range(0,nrow):
for icol in range(0,ncol):
s = s + make_piece(irow,icol,nrow,ncol)
return s
def generate_scene():
#coords: -0.5 0.5 0 -0.5 -0.5 0 0.5 -0.5 0 0.5 0.5 0
hx = 18.0 / ncol *.5
hy = 16.0 / nrow *.5
coords = "-{} {} 0 -{} -{} 0 {} -{} 0 {} {} 0".format(hx,hy,hx,hy,hx,hy,hx,hy)
scene = pre_amble().replace('_coordinates_',coords)
scene = scene + make_puzzle()
scene = scene + post_amble()
return scene
if __name__ == "__main__":
outstr = generate_scene()
fname = "puzzle_scene.x3d"
f = open(fname,"w+")
f.write(outstr)
f.close()