Saturday, December 18, 2010

Script for Generating Terrain

To vary slightly from the usual topics I put on here, this post is about writing a script for moving points around in a z space in 3d studio max. Often I am passed a survey as an autocad file which has a load of points which I then have to draw in 3d to make a terrain. It becomes quite a tedious process tracing over the whole file by hand, so I have written various scripts to speed up the process. Unfortunately, it's requires bodging a few things and I keep on forgetting how to do it, so I am writing this partly as a reminder to myself. The process is as follows -

1. Filter off all the text height information in autocad the original dwg file, making sure there is no stray non numerical data.

2. Import the dwg into 3d studio max, I use version 9 to do this, for some reason 2010 can't import text information from a dwg file.

3. Rename all the individual text objects "n*" which can be automated by "tools" > "rename objects"

4. The first script I have written as javascript:


for (i=0; i < 10; i++){
document.write (
'$n0'+i+'.pos.z = '+ '$n0'+i+'.text as float'+'
'
+'$n0'+i+'.text = "."
'
+ 'convertTo $n0'+i+' SplineShape
'

)
}
for (i=11; i < 725; i++){
document.write (
'$n0'+i+'.pos.z = '+ '$n0'+i+'.text as float'+'
'
+'$n'+i+'.text = "."
'
+ 'convertTo $n'+i+' SplineShape
'
)
}



This would be for 725 text objects, obviously this figure can be changed. It generates an output as so

'$n00.pos.z = $n00.text as float
$n00.text = "."
convertTo $n00 SplineShape
$n01.pos.z = $n01.text as float
$n01.text = "."
convertTo $n01 SplineShape
$n02.pos.z = $n02.text as float
$n02.text = "."
convertTo $n02 SplineShape
$n03.pos.z = $n03.text as float
$n03.text = "." .............'

5. The output can be pasted into the maxscript window and evaluated there. Unfortunately, the reason for drawing out the process like this is that a script like

for i = 1 to $.count do
(
n = $*[i].text as float
$*[i].pos.z = n
)

doesn't seem to work, there is no good reason for this.