see script below for explanation
"""
floor maker script *** work in progress ***
goal is to make something that looks like floor generator for 3dsmax
would require a multimaterial and ability to assign material IDs to objects
to work properly and ultimately randomization of the map color per object
which is currently not possible vray as far as I know.
Script will generate floor planks and add random text coordinates to each plank
but will do that within the maps size.
Right now a texture size of 2000 x 300 units is chosen. So the smaller the
planks are, the more it can be randomized
other possibilities to include:
- making the planks not straight, but this will require denser meshes
- rotation (180 degrees) of textures for more quasi uniqueness
- a check to avoid seams on short ends to not come to close together
***********************************
* script written by Gijs de Zwart *
* www.studiogijs.nl *
* April, 2016 *
***********************************
"""
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import random
import math
def createMesh(vertices,face_vertices):
mesh = Rhino.Geometry.Mesh()
for a, b, c in vertices: mesh.Vertices.Add(a, b, c)
for face in face_vertices:
if len(face)<4:
mesh.Faces.AddFace(face[0], face[1], face[2])
else:
mesh.Faces.AddFace(face[0], face[1], face[2], face[3])
return mesh
def addPlank(plane, width, length):
#define texture size in world units here
texWidth = 2000
texHeight = 300
rect=Rhino.Geometry.Rectangle3d(plane,width,length)
pts=[rect.Corner(i) for i in range(0,4)]
pts2=[]
for i in range(0,4):
pts2.append(Rhino.Geometry.Point3d(0,0,20))
for i in range(0,len(pts2)):#modify points
pts2[i].X=pts[i].X
pts2[i].Y=pts[i].Y
for pt in pts2:#add points to original points
pts.append(pt)
faceVertices = []
faceVertices.append((0,3,2,1))
faceVertices.append((0,1,5,4))
faceVertices.append((1,2,6,5))
faceVertices.append((2,3,7,6))
faceVertices.append((3,0,4,7))
faceVertices.append((4,5,6,7))
mesh=createMesh(pts,faceVertices)
mesh.Unweld(.3,True)
dx=Rhino.Geometry.Interval(0,texWidth)
dy=Rhino.Geometry.Interval(0,texHeight)
dz=Rhino.Geometry.Interval(0,20)
#add mesh to document to apply a texture map
newmesh = sc.doc.Objects.AddMesh(mesh)
randX=(texHeight-width)*random.uniform(0,1)
randY=(texWidth-length)*random.uniform(0,1)
texplane=Rhino.Geometry.Plane.WorldXY
texplane.Origin = plane.Origin
texplane.Rotate(math.pi/2,plane.ZAxis)
texplane.OriginY+=length-texWidth+randY
texplane.OriginX+=texHeight-randX
texmap = Rhino.Render.TextureMapping.CreatePlaneMapping(texplane,dx,dy,dz)
table = Rhino.RhinoDoc.ActiveDoc.Objects
Rhino.DocObjects.Tables.ObjectTable.ModifyTextureMapping(table,newmesh,1,texmap)
def randomFloor():
floorwidth = rs.GetReal("enter floor width size",10000)
floorlength = rs.GetReal("enter floor length size",10000)
width=rs.GetReal("enter plank width",140)
maxlength = rs.GetReal("enter plank max length",2000)
minlength = rs.GetReal("enter plank min length",700)
gapwidth = rs.GetReal("enter gap width", 2)
plane = Rhino.Geometry.Plane.WorldXY
i=0
meshes=[]
temp=0
while i<floorwidth:
j=0
plane.OriginY=0
while j<floorlength:
randomlength = random.randint(minlength,maxlength)
if j==0:
startlength=random.randint(2,20)*maxlength/20
while temp==startlength:
startlength=random.randint(3,20)*maxlength/20
temp=startlength
addPlank(plane, width, startlength)
j+=startlength+gapwidth
elif randomlength<floorlength-j:
#if (floorlength-j)/2<randomlength:
#randomlength-=300
addPlank(plane, width, randomlength)
j+=randomlength+gapwidth
else:
addPlank(plane, width, floorlength-j)
j=floorlength
plane.OriginY=j
plane.OriginX+=width+gapwidth
i+=width
floor=[sc.doc.Objects.AddMesh(mesh) for mesh in meshes]
sc.doc.Views.Redraw()
randomFloor()