Home Technical Talk
The BRAWL² Tournament Challenge has been announced!

It starts May 12, and ends Sept 12. Let's see what you got!

https://polycount.com/discussion/237047/the-brawl²-tournament

Maxscript - find name and position in a txt.file

Hey,

after I spend the day of unsuccessful tries I hope some here does have an idea.

For a mod-project I need a script which exports all selected objects of a scene to a txt.file (with name, pos, rotation)
Each object has it's own line and the file looks like this:

[Tower, x, y, z, rotation]
[Tower, x y, z, rotation]
[Gate, x, y, z, rotation]
etc

This part works, the problem is the next task.

At the end of the file I need new entries, where I have to list in which lines of the txt. file the object appears, like:

[Tower, Line 0, Line 1, Line x],
[Gate, Line 2],

My current approach is to save the names of the objects into an array and loop through the file, looking for the names and then printing the line.
  1. name_array = #()
  2. for o in selection do
  3. (
  4. prop_name = o.name
  5. appendifunique name_array prop_name
  6. )
  7. --open File again to read it
  8. in_file = openFile out_name mode:"r+"
  9. for i in name_array do
  10. (
  11. ?????
  12. )
  13. close in_file

I have looked at the filestream reference of the help file, but I don't have the feeling that the commands do what I'm looking for :/

Replies

  • monster
    Offline / Send Message
    monster polycounter
    The method you are considering would be really slow and difficult. I think the best method is to print both lists at the same time and then combine them. See below.
    1. (
    2. --CREATE A DATA STRUCT
    3. struct gameObject
    4. (
    5. name = "",
    6. positions = #(),
    7. rotations = #()
    8. )
    9. --SAVE THE CURRENT SELECTION
    10. selObjects = getCurrentSelection()
    11. gameObjects = #()
    12. --LOOP THROUGH SELECTION
    13. for obj in selObjects do
    14. (
    15. isNewName = true
    16. --CHECK IF GAMEOBJECT NAME HAS BEEN ADDED ALREADY
    17. for g = 1 to gameObjects.count while isNewName do
    18. (
    19. if obj.name == gameObjects[g].name do
    20. (
    21. --NAME EXISTS, ADD DATA
    22. append gameObjects[g].positions obj.pos
    23. append gameObjects[g].rotations obj.rotation
    24. isNewName = false
    25. )
    26. )
    27. --NAME DIDN'T EXIST YET, CREATE A NEW GAMEOBJECT
    28. if isNewName do
    29. (
    30. append gameObjects (gameObject name:obj.name positions:#(obj.pos) rotations:#(obj.rotation))
    31. )
    32. )
    33. --CREATE A STRING STREAM
    34. firstStream = "" as stringStream
    35. secondStream = "" as stringStream
    36. lineCounter = 1
    37. --FORMAT TO THE STREAMS CONCURRENTLY
    38. for obj in gameObjects do
    39. (
    40. format "[%" obj.name to:secondStream
    41. for i = 1 to obj.positions.count do
    42. (
    43. format "[%, %, %, %, %]\n" obj.name obj.positions[i].x obj.positions[i].y obj.positions[i].z obj.rotations[i] to:firstStream
    44. format ", Line %" lineCounter to:secondStream
    45. lineCounter += 1
    46. )
    47. format "]\n" to:secondStream
    48. )
    49. --CREATE FILE FROM STREAMS
    50. --FOR DEBUG PURPOSE I'M JUST PRINTING TO A SCRIPT WINDOW
    51. in_file = newScript()
    52. format "%" (firstStream as string) to:in_file
    53. format "%" (secondStream as string) to:in_file
    54. )

    Example of output:
    1. [Tower, 125.665, 131.43, 0.0, (quat 0 0 -0.41532 0.909675)]
    2. [Tower, -8.12219, -11.5719, 0.0, (quat 0 0 -0.266385 0.963867)]
    3. [Tower, 85.1183, 38.6826, 0.0, (quat 0 0 -0.41532 0.909675)]
    4. [Tower, 34.4258, 121.165, 0.0, (quat 0 0 -0.142008 0.989866)]
    5. [Tower, 137.626, -88.7488, 0.0, (quat 0 0 -0.133201 0.991089)]
    6. [Tower, -3.80859, -199.553, 0.0, (quat 0 0 -0.299809 0.953999)]
    7. [Tower, 202.062, 64.4379, 0.0, (quat 0 0 -0.266385 0.963867)]
    8. [Gate, -124.211, -190.847, 0.0, (quat 0 0 -0.269507 0.962998)]
    9. [Gate, -35.0458, 156.777, 0.0, (quat 0 0 0.126809 0.991927)]
    10. [Gate, -25.1697, 50.6722, 0.0, (quat 0 0 0.0161643 0.999869)]
    11. [Gate, -25.1697, 50.6722, 0.0, (quat 0 0 0.0161643 0.999869)]
    12. [Gate, 238.714, -196.688, 0.0, (quat 0 0 0.00794686 0.999968)]
    13. [Gate, -25.1697, 50.6722, 0.0, (quat 0 0 0.0161643 0.999869)]
    14. [Gate, -108.792, 54.8598, 0.0, (quat 0 0 0.281931 0.959435)]
    15. [Teapot, -68.9013, -9.15181, 0.0, (quat 0.0220851 -0.197951 -0.0668346 0.977681)]
    16. [Teapot, 72.2904, -88.2731, 0.0, (quat 0 0 -0.288409 0.957507)]
    17. [Teapot, 132.705, -191.435, 0.0, (quat 0 0 0.374879 0.927074)]
    18. [Teapot, -10.9261, -63.1287, 0.0, (quat 0.16181 0.163868 -0.201411 0.95205)]
    19. [Teapot, -31.7108, -108.121, 0.0, (quat 0 0 -0.516297 0.856409)]
    20. [Teapot, 68.8799, -40.1832, 0.0, (quat 0.0278817 0.141764 -0.203284 0.968401)]
    21. [Tower, Line 1, Line 2, Line 3, Line 4, Line 5, Line 6, Line 7]
    22. [Gate, Line 8, Line 9, Line 10, Line 11, Line 12, Line 13, Line 14]
    23. [Teapot, Line 15, Line 16, Line 17, Line 18, Line 19, Line 20]
  • Primergy
    Thank you, your solution is indeed better then mine^^ - thats the problem if you only have knowledge of some commands and want to do something more complex
Sign In or Register to comment.