Script to generate random parts in LDCad


RE: Script to generate random parts in LDCad
#3
(2020-01-23, 18:55)Roland Melkert Wrote: The random part is easy, the parts/colors are tricky because they need input from the user.

Below an example/starter script

Code:
genTools=require('genTools')

function onRun()
  local base='41539.dat'
  local baseCol=7
  local yPos=-8
  local colors={4,15,1,14}
  local colCnt=4
  local xCnt=8
  local zCnt=8
  local xStep=20
  local zStep=20
 
  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 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()
  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
      sel:add(sf:addNewRef(parts[math.random(partCnt)], colors[math.random(colCnt)], ldc.matrix(xOfs+x*xStep, yPos, zOfs+z*zStep)))
    end
  end
end

function register()

  local macro=ldc.macro('My macro')
  macro:setEvent('run', 'onRun')
end

register()

Colors are static but you could also get them from the selection if needed.

It will use the parts in the selection to randomly fill a 8x8 plate.

If you want the plate to be selected too you could do that be asking for the dimensions followed by determining the correct plate to use. Or just adjust the script constants at its top.

OK, I'll look in to that. Looks a bit like the landscape generator "we" once made too. Thanks!
Jaco van der Molen
lpub.binarybricks.nl
Reply
« Next Oldest | Next Newest »



Messages In This Thread
RE: Script to generate random parts in LDCad - by Jaco van der Molen - 2020-01-23, 21:09

Forum Jump:


Users browsing this thread: 1 Guest(s)