Terrain Matcher

Have you ever wanted a plateau on your Vue terrain that a building can stand on? Maybe you want to cut a flat road through a scene? This python script, for Vue 6 Infinite and Vue 6 xStream, makes doing both of these tasks easier. How to match your terrain to an object in seven easy steps:

  1. Download this file onto your computer.
  2. Create a terrain. You can scale it but avoid rotating it.
  3. Place objects above the terrain that you want to affect the terrain. Don’t put them too close to the top of the terrain or it may be missed.
  4. Select the terrain to be modified.
  5. Then run script. To run it go to Python -> Run Python Script Then locate the file on your computer.
  6. Answer the questions that appear in the pop ups.
  7. Let it run till it pops up to say it’s finished.

[codesyntax lang=”python”]

#******************************************************
# Mold a terrain to objects placed above it
#
# - terrainmatcher.py
# - By Mark Caldwell
# - Version 0.1
# - 10th April 2007
# - Tested with Vue 6 Infinite
#
# How to use in 6 easy steps
#
# 1. Download this file onto your computer
#
# 2. Create a terrain (at this stage you can scale it
#   but avoid rotating it)
#
# 3. Place objects above the terrain that you want to
#    affect the terrain (don't put them too close to
#    the top of the terrain)
#
# 4. Select the terrain to be modified
#
# 5. Then run script
#    To run it go to Python -> Run Python Script
#    Then locate the file on your computer
#
# 6. Answer the questions that appear in the pop ups
#    then let it run till it pops up to say it's
#    finished
#
#******************************************************

import copy

#----------------------------------------------
# Configuration
#----------------------------------------------

spread=Prompt('How far should the match spread beyond the match to objects?','2',true,'Input Spread')

if int(spread)<0:
    spread=0
else:
    spread=int(spread)

setheight=Prompt('What altitude should the top be set to on a scale of 1 to 100 with 100 at the top and 1 at the bottom of the terrain? (enter 0 to not affect matches altitude)',
              '100',true,'Input Top Altitude')

if float(setheight)==0:
    setheight=-1
elif float(setheight)>100:
    setheight=0.99
else:
    setheight=(float(setheight)-1)/100

setdepth=Prompt('What altitude should the areas not under the object be set to on a scale of 1 to 100 with 100 at the top and 1 at the bottom of the terrain? (enter 0 to not affect unmatched altitude)',
              '0',true,'Input Bottom Altitude')

if float(setdepth)==0:
    setdepth=-1
elif float(setdepth)>100:
    setdepth=0.99
else:
    setdepth=(float(setdepth)-1)/100

#setheight=0.2
#setdepth=-1

#--------------------------------------------------------------------------
# Test the user has a scene loaded
#--------------------------------------------------------------------------

if TestLoaded():

#--------------------------------------------------------------------------
# Test the user has 1 object and that it is a terrain
#--------------------------------------------------------------------------

    numselected=CountSelectedObjects()

    if numselected>1:
        message="Please select only one object."
    elif numselected<1:
        message="Please select an object."
    else:
        bObject=GetSelectedObjectByIndex(0)     # Get first selected object

        if bObject.IsTerrain()==False:
            message="Please select a terrain."
        elif bObject.IsLocked():                # Check object isn't locked
            message="Please select an object that isn't locked."
        else:

#--------------------------------------------------------------------------
# Apply terrain data to terrain
#--------------------------------------------------------------------------

            pos=bObject.Position()
            scal=bObject.GetScale()

            altitudes=[]

            TerrainObj=bObject.ToTerrain()

            height= TerrainObj.Height()         # Get Terrain Objects Height
            width= TerrainObj.Width()           # Get Terrain Objects Width

            factorheight=height
            factorwidth=width

            stepdistancewidth=(factorheight*scal[0])/(width-1)
            stepdistanceheight=(factorwidth*scal[1])/(height-1)

            altitudes=TerrainObj.TerrainAltitudes ()
            altitudes_new=[[0]*width for i in range(height)]
            xplus=pos[0]-((factorheight/2)*scal[0])
            yplus=pos[1]-((factorwidth/2)*scal[1])

            altitudes=TerrainObj.TerrainAltitudes ()

            for i in range (0,height):
                for j in range (0,width):
                    x=(i*stepdistancewidth)+xplus
                    y=(j*stepdistanceheight)+yplus
                    z=pos[2]+(altitudes[j][i]*scal[2]*100)+(20*scal[2])

                    start=x,y,z
                    vector=0,0,1

                    hitobj=GetFirstHitObject(start,vector)
                    if hitobj:
                        altitudes_new[j][i]=1
                    else:
                        altitudes_new[j][i]=0

            if spread>0:
                altitudes_temp=[[0]*width for i in range(height)]
                for i in range (0,height):
                    for j in range (0,width):
                        if altitudes_new[j][i]==1:
                            for k in range ((i-spread),(i+spread)):
                                for l in range ((j-spread),(j+spread)):
                                    if k>=0 and k<height and l>=0 and l<width:
                                        altitudes_temp[l][k]=1

                altitudes_new=copy.deepcopy(altitudes_temp)

            altitudes_temp=[[0]*width for i in range(height)]

            for i in range (0,height):
                for j in range (0,width):
                    if setheight>=0 and altitudes_new[i][j]==1:
                        altitudes_temp[i][j]=setheight
                    elif setdepth>0 and altitudes_new[i][j]==0:
                        altitudes_temp[i][j]=setdepth
                    else:
                        altitudes_temp[i][j]=altitudes[i][j]

            TerrainObj.SetTerrainAltitudes (altitudes_temp)  # Set terrain heights

            message="Successfully Changed Terrain"

else:
    message="No Scene Loaded"

Refresh()

Message(message)    # Display message

#--------------------------------------------------------------------------
# End of Script
#--------------------------------------------------------------------------

[/codesyntax]

impworks © Copyright Mark Caldwell 1996 - 2024