[TUTORIAL] Mapping Faster with Scripts

Discussion in 'Tutorials & Resources' started by Brandished, May 23, 2008.

  1. Brandished

    Brandished L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    293
    A while back I finally got around to installing HotKeys and started reading about scriptable keys. I applied this to Hammer as well as dozens of other programs and found I'm saving a huge amount of time so I'd figure I'd pass on the info.

    Some of you might know about this already and / or may know of an even more efficient method then the one I describe here, please don't keep this to yourself, I'm no scripting guru by any means. :D

    For this first tutorial you'll need two similar sounding programs, HotKeys and AutoHotKey

    Why bother making an AutoKotKey script?
    Here I have a structure made out of brushes that lets say I want to rotate 90 degrees....

    [​IMG]

    To do this normally I would:
    -select all the brushes
    -press "Ctrl" & "M" to bring up the transformation window
    -change the Z value to 90
    -then hit "Enter"

    This may not sound too time consuming, but I tend to do something like this 20 or more times on the fly when I'm mapping. The time it takes to repeat this same procedure over and over again adds up. Now what if I could bind the whole procedure to a 2 key combination....

    This is where AutoHotKey and Hotkeys come in, first I'm going to use AutoHotKey to record the procedure to a file and next I'll use Hotkeys to bind the file to a key combination.

    Here's the steps:

    1st: After installing the two programs mentioned above, open up Hammer and load a vmf file to experiment with (or create a new one).

    2nd: while Hammer is still running, open up Hotkeys' AutoScriptWriter and hit the "Record" button in the top left of the window.

    [​IMG]
    [​IMG]

    There should now be a "Stop" button in the top left corner of your screen.

    [​IMG]

    3rd: Pull Hammer up again and perform the same routine:
    - select the brushes you want to rotate
    - press "Ctrl" & "M" to bring up the transformation window
    - press the "Tab" button 3 times two get down to the Z value
    - type in "9" and "0" to change the degrees from 0 to 90
    - press "Enter" to perform the rotation

    4th: Click the "Stop" button in the top left corner of the screen. You should now be presented with a window that looks something like this:

    [​IMG]

    We don't want to keep all of this, look near the bottom for the line "Send, {CTRLDOWN}m{CTRLUP}", remove all the text above this line leaving you with this:

    [​IMG]

    5th: Click save and put in "rotate_90" for the filename and hit "Enter" to save it, taking note of where you save the file to. Close the AutoScriptWriter.

    6th: Bring up the HotKeys keyboard editor by pressing "Z" & the "Windows Key" [​IMG].

    [​IMG]

    Here we can see what the default key bindings are for each key. Now drag the "rotate_90.ahk" file that was just created onto the number 1 key.

    [​IMG]

    And we've completed binding the procedure to rotate an object 90 degrees onto the number 1 key. Now to test it out.

    Remove the HotKeys keyboard editor from the screen by again pressing "Windows Key" & "Z" and bring Hammer back up. Now select the brush(es) you want to rotate, but instead of hitting "Ctrl" & "M" to bring up the transformation window, hit the "Windows Key" & "Z". HotKeys should load up the script we just made and send the required commands into Hammer to rotate the brush 90 degrees.

    No selecting Z, no typing in 90, no hitting enter; Time = Saved.

    I use the script above in Hammer myself, as well as several other I've created over time. I've been using the 2 programs above to do everything from change the resolution of an image in MS-Paint, to changing the extension on a file.

    There are a few snags I've run into with using AHK scripts, though:
    1: the procedure you're binding has to performable using only the keyboard, I haven't had any success with procedures that requires mouse input
    2: while the script is running, you can't hit any other keys until it's finished or you could jack-up the process (ie: instead of rotating a brush 90 degrees, it'll be scaled larger by a factor of 90)

    (The place holder posts below are for another tutorial I'm planning to add to this thread)
     
    • Thanks Thanks x 5
    Last edited: May 27, 2008
  2. Brandished

    Brandished L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    293
    Opening several folders at once with the push of a button

    When I'm mapping, I always find myself opening the same folders every time, my mapsrc folder, my tf2 maps folder, and my tools folder (where packrat resides). To save myself time, I made a simple batch script that opens these three folders at once and bound it to a key on my keyboard through hotkeys so I can open all three folders with he push of a button.

    The script is fairly easy to make compared to the last one, just open notepad and add the following (quotation marks included):

    [​IMG]

    The first line opens the folder "C:\mapping" where all my mapping tools reside. This might not be where you keep your stuff at, if not, just replace "C:\mapping" with the location of your mapping stuff. The second two lines open my TF2 maps folder (where the game engine looks for maps when your at the "Create Server" menu) and the "tf\mapsrc" folder, where Hammer saves my TF2 maps to by default. You'll want to replace the "USERNAME" part of the second two with your steam log-on name. If you have an older install of Steam (you installed Steam from a HL2 or Episode 1 disk), you'll be using this or the second two instead:

    [​IMG]

    Once you're done changing the folder locations to match your set-up, save the file as "TF2_Open_Folders.bat", taking note of the folder you saved it in.

    Unfortunately, binding this to a key on the keyboard is slightly more complicated then the above method, for some reason, simply dropping and dragging won't work with batch scripts.

    Anyways, to bind the "TF2_Open_Folders.bat" script to a key, open hotkeys and right click on the key you want to bind the batch script to, select "New Hotkey" then select "Browse"

    [​IMG]

    Now goto the folder you saved the "TF2_Open_Folders.bat" in, select it, and hit "OK"

    [​IMG]

    Now the key should be bound to a key on your keyboard.

    [​IMG]

    Whoalla, no more clicking through three shortcuts every time you want to open those folders, open them all with the push of a button.
     
    • Thanks Thanks x 2
    Last edited: Jul 9, 2008
  3. Brandished

    Brandished L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    293
    Mirror mapping using a naming convention and batch file

    As you probably have noticed, for a few of the Valve maps, such as 2fort and Granary, one side of the map is a near mirror copy of the other. No matter which team you're on, red or blue, the orientation of the various objects in the map are the same (ie: resupply locker will be on the left side of the spawn room, ammo pack will be just to the right of the final control point, etc).

    While it's not too time consuming to reverse the orientation of the objects in the map (just rotate one half 180 degrees), changing entity properties such as which way a door opens or which team a Control Point belongs to is not so easy.

    As VMF files are basically XML files with a ".vmf" extension, you can make a lot of changes to your map with a simple text editor. With that in mind, I've found a fairly good method for quickly fixing up most of the problems created by mirroring one side of the map to another and I'm fairly certain Valve themselves use something similar when creating their symmetrical maps.

    For it to work right I follow these precedents:

    Naming scheme:
    For all entities that require a name ("func_door", "team_control_point", etc) I follow a specific naming scheme like "door_spawn_red_01" or "control_point_blue_01". The "_red_"" and "_blue_" parts of the name are especially important, you'll see why in a bit.

    Angles:
    I keep all moving entities ("func_door", "func_button", etc) that aren't going up and down moving in a direction that's 0, 90, 180, 270, or some multiple of 90 degrees.

    Now on with the tutorial:

    Here we have a control point map that's half done, cp_3points.vmf. The control points and spawn rooms have been created for the red team, but there is nothing finished for the blue team so far.

    [​IMG]
    skybox was removed for this example

    First thing we want to do is copy all the parts we want to mirror into a new vmf file or, as in this case, simply save the map under a new file name, "modify.vmf", and remove the parts that you don't want mirrored, which I've done here:

    [​IMG]
    skybox was removed for this example

    Now to create the mirror image effect, rotate the whole map 180 degress and save it, this will put all the objects in the map in the same starting location no matter which team you're on (in this case, the resupply locker will always be to your left). Flipping horizontally and vertically isn't recommended as it will not change the direction lights are pointed in.

    Although we've rotated all the objects in the map 180 degrees, the team settings for some of the entities have to be changed from red to blue and the door move directions all have to be reversed. Here's were scripting can save some time.

    The batch file I'm using to do the alterations uses an older DOS based program ("change.exe") that requires the 8.3 file names (12345678.123) as well as a limited 80 character length (IIRC) to the location name. So trying to process a "ThisIsMySuperLongMapFileName.vmf" file in the "C:\Program Files\Valve\Steam\SteamApps\USERNAME\sourcesdk_content\tf\mapsrc\folder" directory won't work.

    I've created a folder "C:\Scripts\Batch\Hammer\mirror" on my computer to extract the contents (the 2 bat files and change.exe) of the zip file to as well as copy the "modify.vmf".

    [​IMG]

    The batch file will fix up most of the map's entities to work for the red team, but before we run it, lets break down what it does.

    Code:
    CD C:ScriptsBatchHammerteam_change
    -Tell the computer where the file is we want to modify-

    Code:
    COPY modify.vmf 9999.txt
    -copy the contents of "modify.vmf" into a text file "9999.txt"-

    Code:
    CHANGE.EXE 9999.txt /FROM "0 90 0" /TO "zqzq 27zqzq 0" /IN "movedir"
    -Tells "CHANGE.EXE" to search "9999.txt" for lines containing "0 90 0". If a line containing the text is found, "0 90 0" is replaced with "zqzq 27zqzq 0" only if that line also contains the text "movedir". What where doing in these 4 lines is reversing the move direction. Just after this, will replace the "zqzq" with a "0".

    Code:
    CHANGE.EXE 9999.txt /FROM "WOOD/GRAIN_ELEVATOR_FACADE_08" /TO "CONCRETE/CONCRETEWALL004" /IN "material"

    Here were searching through all faces for the "WOOD/GRAIN_ELEVATOR_FACADE_08" material and replacing in with the "CONCRETE/CONCRETEWALL004" texture. In my sample map, the first texture was used to designate the red side of the base, here we're replacing it with another texture to designate the blue side of the base.

    Code:
    CHANGE.EXE 9999.txt /FROM "2" /TO "3" /IN "TeamNum"
    The lines in the vmf containing "TeamNum" designate the default owner of an entity. The 2 value here gives red ownership, we're replacing it with 3 which will give blue ownership.

    Code:
    CHANGE.EXE 9999.txt /FROM "_red_" /TO "_blue_"
    -Tells "CHANGE.EXE" to simply search "9999.txt" for "_red_" and replace it with "_blue_". This was the reason for the naming scheme, all the entities named red were on the red side of the base, where as we are replacing red with blue. This is important as it limits triggers to the doors in their section, otherwise, as an example, when a player triggered the door for the red respawn, the blue respawn door would open as well.

    Code:
    CHANGE.EXE 9999.txt /FROM "red_filter" /TO "blue_filter"
    This sets all filters that were set to the red team to now work for the blue team instead.
    Code:
    CHANGE.EXE 9999.txt /FROM "2" /TO "3" /IN "point_default_owner"
    For CP maps, this changes a control points default owner from red (2) to blue (3).

    Code:
    CHANGE.EXE 9999.txt /FROM "Red Control Point" /TO "Blue Control Point" /IN "point_printname"
    Also for cp maps, here were changing the print name (the name that pops up in the corner of the screen when a cp is captured) to be labeled for the blue team instead of the red team.

    Code:
    RENAME 9999.txt modify_1.vmf
    Renames "9999.txt" to "modify_1.vmf" so the file can now be loaded back into Hammer.

    Code:
    PAUSE
    Now the script waits for a key to be pressed before closing. I have this here so I can see if the script failed.

    Now open "modify_1.vmf" with Hammer copy its' contents back into the original map.

    [​IMG]
    skybox was removed for this example

    Were not done just yet, though. Not all settings can be changed this way, such as the incremental numbering system used for control points, and additive flag based values for entities such as func_door_rotating. We need to fix the properties for these entities before the map will run right. In this sample map it's pretty easy, just open the newly copied control point's properties and change the Index setting from 0 to 2.

    [​IMG]

    - Below is a link to the script and sample map file used in this example -
    http://www.mediafire.com/?0euajzmmwgv
     
    • Thanks Thanks x 4
    Last edited: Jul 10, 2008
  4. lucampos

    lucampos L1: Registered

    Messages:
    21
    Positive Ratings:
    0
    great dude! it makes alot faster when mapping, thanks!
     
  5. Spike

    Spike L10: Glamorous Member

    Messages:
    716
    Positive Ratings:
    82
    90ยบ rotation is something I always wanted to make.
     
  6. YM

    aa YM LVL100 YM

    Messages:
    7,099
    Positive Ratings:
    5,741
    whats wrong with replacing

    -press "Ctrl" & "M" to bring up the transformation window
    -change the Z value to 90
    -then hit "Enter"

    with

    -click selection in 2d view
    -draging corner until it is rotated though 90

    Its load quicker, posibly a third or even a quarter of the time. It seems you're just making an overly complex solution to a problem that shouldnt even exist.
     
  7. Brandished

    Brandished L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    293
    This isn't really a tutorial on rotating objects, but on what you can do with scripts, the rotating script was an example. Did you read the whole post?
     
    Last edited: May 27, 2008
  8. Ace

    Ace L3: Member

    Messages:
    112
    Positive Ratings:
    9
    Heehee

    1 MB Captain Picard image was making the thread take to long to load...

    I'm sure you could borrow one from Youme's apparently unlimited supply :D

    Anyway just let me check.
    You use Autohotkeys to make the scripts, and then hotkeys to run it?
     
  9. YM

    aa YM LVL100 YM

    Messages:
    7,099
    Positive Ratings:
    5,741
    Ok, so its a bad example to show how useful this is. Perhaps you could give some other examples that might actually save more time? (yes I see the placeholder posts, I'd just like an advance of what you might put in them)
     
  10. Lord Ned

    Lord Ned L7: Fancy Member

    Messages:
    421
    Positive Ratings:
    174
    Because As far as I know you can't snap to rotating in the 2d view? (Atleast for props)

    Though you can check it to 15 degree incrimentals.
     
  11. R3dRuM

    R3dRuM L6: Sharp Member

    Messages:
    303
    Positive Ratings:
    19
    If your view on the 3d screen or on the 2d screen changes a bit, the whole script will be off wont it? Because the script rely's on screen co-ordinates, not where the brushes are according to the screen.
     
  12. trainRiderJ

    trainRiderJ L5: Dapper Member

    Messages:
    203
    Positive Ratings:
    31
    Read his thing again, particularly this part:

    Basically you're just left with CTRL+M,tab,tab,tab,90,enter. If you can't see how this is useful you're just being stubborn.
     
  13. Fearlezz

    Fearlezz L10: Glamorous Member

    Messages:
    787
    Positive Ratings:
    240
  14. grazr

    aa grazr Old Man Mutant Ninja Turtle

    Messages:
    5,436
    Positive Ratings:
    3,561
    I'd probably find this most useful when rotating a props pitch/yaw (a prop with an unusual world alignment), as i spend a few moments through trial and error trying to figure out which is the props pitch and yaw. When i do rotate it's not normally a specific degree, but just to give the prop a unique standing in the environment.

    I do as Youme said, just double click a brush/prop and then rotate 90* in the 2D view. If i'm rotating something it is not normally 90* how ever.

    I look forward to some more advanced examples of these key scriptings. I would imagine triming off corners to be helpful.
     
  15. Laz

    Laz L7: Fancy Member

    Messages:
    461
    Positive Ratings:
    34
    this is why I love the G15 keyboard :D live macro recording without even having to open an extra program :p
     
  16. Brandished

    Brandished L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    293
    Finally got my other tutorials up after way too long after the first one was uploaded, you'll want to backup your map before trying the third one out. I'm still working on creating some sample scripts for the first tutorial, I should have some up in a few days.
     
    Last edited: Jul 9, 2008
  17. Brandished

    Brandished L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    293
    I had to remove the second tutorial because I couldn't geit it to work at first. I re-added it with the work-around.
     
  18. Brandished

    Brandished L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    293
    :mad: ENVY!!
     
  19. phantombadger

    phantombadger L2: Junior Member

    Messages:
    56
    Positive Ratings:
    1
    I have a G15 too, actually a 1st gen G15 so i have 18G keys to increase the scripts. with the info here I can increase the hotkey/scipts dramatically:woot:. nice tutorial.

    And why use MS paint to resize with scripts/hotkeys. Grab Photoshop and set up actions. its similar to the recording method but its proprietary so you dont need extra prgms; i'm sure gimp has this feature too as is the free PS clone.
     
    Last edited: Oct 19, 2008