Posts: 1,291
Threads: 182
Joined: Jul 2011
RE: Script to generate random parts in LDCad
2020-01-27, 8:48
(2020-01-24, 9:56)Philippe Hurbain Wrote: I played a little with that script and made a few modifications to address:- Tiles, plates, bricks don't have the same Y position to sit on a plane
- Assymetric parts (eg. slopes) need orientation randomness
- Some parts are not available in all colors, so get color from each part in selection
Usage: place the seed parts on a plate at Y=0, select all seed parts, run script.
Code:
function onRun()
local base='41539.dat'
local baseCol=71
local xCnt=8
local zCnt=8
local xStep=20
local zStep=20
local yVect=ldc.vector(0, 1, 0)
local sf=ldc.subfile()
local ses=ldc.session()
if not ses:isLinked() then
ldc.dialog.runMessage('No session')
return
end
local sel=ses:getSelection()
local parts={}
local pColor={}
local yPos={}
local partCnt=sel:getRefCount()
if partCnt==0 then
ldc.dialog.runMessage('No parts in selection')
return
end
for i=1,partCnt do
parts[i]=sel:getRef(i):getName()
pColor[i]=sel:getRef(i):getColor()
yPos[i]=sel:getRef(i):getPos():getY()
end
sel:remove()
local xOfs=10-xCnt*xStep/2
local zOfs=10-zCnt*zStep/2
sel:add(sf:addNewRef(base, baseCol))
for z=0,zCnt-1 do
for x=0,(xCnt-1) do
local rnd=math.random(partCnt)
local rndYRot=ldc.matrix()
rndYRot:setRotate(math.random(4)*90,yVect)
sel:add(sf:addNewRef(parts[rnd], pColor[rnd], ldc.matrix(xOfs+x*xStep, yPos[rnd], zOfs+z*zStep)):setOri(rndYRot))
end
end
end
function register()
local macro=ldc.macro('RandomGen')
macro:setEvent('run', 'onRun')
end
register()
Nice. I've had a busy weekend, but will take a look asap. Thanks Roland an Philo!
Jaco van der Molen
lpub.binarybricks.nl