Move World Editor Objects in Bulk via Script

Discussion in 'Utilities and programming' started by Tmanning47, Nov 25, 2024.

  1. Tmanning47

    Tmanning47
    Expand Collapse

    Joined:
    Jun 8, 2014
    Messages:
    13
    Just set this up so I figured I would share, hope this helps someone! My world editor freezes if I try moving upwards of 10,000 objects so I edited the locations via the config files instead.

    1. Make a new object, place it roughly centered in your group of objects then note the starting xyz values, move it to the desired location then make note of the new xyz values and calculate the difference.
    2. Backup your files, i recommend zipping the whole level folder just in case you need to revert
    3. Place all objects you want to move into their own subfolder in the editor
    4. Locate the parent folder of that new folder in windows explorer
    5. Create the following .ps1 file and right click to run in powershell (adjust the values in the file that you want to move the objects by):

    param (
    [double]$xAdjust = 3609.57,
    [double]$yAdjust = 4323.134,
    [double]$zAdjust = 0
    )
    # Function to process each JSON object
    function Process-JsonObject {
    param (
    [PSCustomObject]$jsonObject
    )
    if ($jsonObject.position) {
    $jsonObject.position[0] += $xAdjust
    $jsonObject.position[1] += $yAdjust
    $jsonObject.position[2] += $zAdjust
    }
    return $jsonObject
    }
    # Get all .json files in the current directory and subdirectories
    $jsonFiles = Get-ChildItem -Path . -Filter *.json -Recurse
    foreach ($file in $jsonFiles) {
    $content = Get-Content -Path $file.FullName -Raw
    $jsonObjects = $content -split "(?<=\})\s*(?=\{)"
    $processedObjects = @()
    foreach ($jsonObject in $jsonObjects) {
    try {
    $item = $jsonObject | ConvertFrom-Json
    $processedObject = Process-JsonObject -jsonObject $item
    $processedObjects += $processedObject | ConvertTo-Json -Depth 100 -Compress
    } catch {
    Write-Warning "Invalid JSON: $jsonObject"
    }
    }
    # Save the modified JSON objects back to the file, preserving formatting
    $processedJson = $processedObjects -join "`n"
    Set-Content -Path $file.FullName -Value $processedJson
    }
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice