Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- Find and modify matching elements
- created by matortheeternal
- }
- unit UserScript;
- uses mteFunctions;
- const
- // if print is true the script will print its log to the xEdit console
- print = true;
- // if save is true the script will save its log to the xEdit root directory
- save = true;
- var
- log: TStringList;
- procedure ModifyElementValue(var e: IInterface);
- const
- // path to modify
- path = 'DATA\Position\Z';
- modifier = -1.0;
- var
- oldValue, newValue: real;
- begin
- oldValue := genv(e, path);
- newValue := oldValue + modifier;
- log.Add(Format('Changed %0.2f to %0.2f on %s', [oldValue, newValue, Name(e)]));
- senv(e, path, newValue);
- end;
- procedure FindElements(var g: IInterface; var lst: TList; s: string);
- var
- i: Integer;
- e: IInterface;
- begin
- for i := 0 to ElementCount(g) - 1 do begin
- e := ElementByIndex(g, i);
- if Signature(e) = s then
- lst.Add(TObject(e))
- else if ElementCount(e) > 0 then
- FindElements(e, lst, s);
- end;
- end;
- function Initialize: Integer;
- var
- i, j: Integer;
- filename: string;
- UserFile, group, e: IInterface;
- lst: TList;
- oldValue, newValue: real;
- GroupSignatures, ElementSignatures: TStringList;
- begin
- // initialize
- log := TStringList.Create;
- GroupSignatures := TStringList.Create;
- ElementSignatures := TStringList.Create;
- lst := TList.Create;
- // let user specify file to use
- UserFile := FileSelect('Select a file');
- if not Assigned(UserFile) then begin
- AddMessage('No user file specified, terminating.');
- exit;
- end;
- AddMessage('Script is using '+GetFileName(UserFile));
- // load groups and records
- GroupSignatures.Add('WRLD');
- ElementSignatures.Add('REFR');
- // find elements
- for i := 0 to Pred(GroupSignatures.Count) do begin
- AddMessage('Finding elements in group '+GroupSignatures[i]);
- group := GroupBySignature(UserFile, GroupSignatures[i]);
- for j := 0 to Pred(ElementSignatures.Count) do begin
- AddMessage(' Finding elements with signature '+ElementSignatures[j]);
- FindElements(group, lst, ElementSignatures[j]);
- end;
- end;
- // loop through elements
- for i := 0 to Pred(lst.Count) do begin
- e := ObjectToElement(lst[i]);
- ModifyElementValue(e);
- end;
- // print/save
- if print then begin
- AddMessage(' ');
- AddMessage(log.Text);
- end;
- if save then begin
- filename := 'FindElements_'+StringReplace(GroupSignatures.Text, #13#10, '-', [rfReplaceAll])+'.txt';
- log.SavetoFile(filename);
- end;
- // clean up
- log.Free;
- lst.Free;
- GroupSignatures.Free;
- ElementSignatures.Free;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment