Script to generate random parts in LDCad


Script to generate random parts in LDCad
#1
Hi all,

For a MOC I have to simulate a kind of landscaping rough soil-like.
I am looking for a way in LDCad to make something like this with a script.

With it, you should be able to specify dimensions (in this case 8x8), what bricks to use (plates, tiles, etc.) and colors (like LBG, tan, white, ...)
Would that be possible?

   
Jaco van der Molen
lpub.binarybricks.nl
Reply
RE: Script to generate random parts in LDCad
#2
(2020-01-22, 11:44)Jaco van der Molen Wrote: Hi all,

For a MOC I have to simulate a kind of landscaping rough soil-like.
I am looking for a way in LDCad to make something like this with a script.

With it, you should be able to specify dimensions (in this case 8x8), what bricks to use (plates, tiles, etc.) and colors (like LBG, tan, white, ...)
Would that be possible?

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.
Reply
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
RE: Script to generate random parts in LDCad
#4
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()
Reply
RE: Script to generate random parts in LDCad
#5
(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
Reply
RE: Script to generate random parts in LDCad
#6
(2020-01-27, 8:48)Jaco van der Molen Wrote: Nice. I've had a busy weekend, but will take a look asap. Thanks Roland an Philo!

Hi Philo, Roland,

I can perfectly work with this so far.

Tnx

Jaco
Jaco van der Molen
lpub.binarybricks.nl
Reply
« Next Oldest | Next Newest »



Forum Jump:


Users browsing this thread: 2 Guest(s)