Home Technical Talk

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.
name_array = #() 
        for o in selection do
        ( 
            prop_name = o.name 
            appendifunique name_array prop_name
        )
         
    --open File again to read it
    in_file = openFile out_name mode:"r+"    
     for i in name_array do
      (
        ?????
       ) 
        
    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
    Options
    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.
    (
    	--CREATE A DATA STRUCT
    	struct gameObject
    	(
    		name = "",
    		positions = #(),
    		rotations = #()
    	)
    	
    	--SAVE THE CURRENT SELECTION
    	selObjects = getCurrentSelection()
    	gameObjects = #()	
    	
    	--LOOP THROUGH SELECTION
    	for obj in selObjects do
    	(
    		isNewName = true
    		
    		--CHECK IF GAMEOBJECT NAME HAS BEEN ADDED ALREADY
    		for g = 1 to gameObjects.count while isNewName do
    		(
    			if obj.name == gameObjects[g].name do
    			(
    				--NAME EXISTS, ADD DATA
    				append gameObjects[g].positions obj.pos
    				append gameObjects[g].rotations obj.rotation
    				isNewName = false
    			)
    		)
    		
    		--NAME DIDN'T EXIST YET, CREATE A NEW GAMEOBJECT
    		if isNewName do
    		(
    			append gameObjects (gameObject name:obj.name positions:#(obj.pos) rotations:#(obj.rotation))
    		)
    	)
    	
    	--CREATE A STRING STREAM
    	firstStream = "" as stringStream
    	secondStream = "" as stringStream
    	lineCounter = 1
    	
    	--FORMAT TO THE STREAMS CONCURRENTLY
    	for obj in gameObjects do
    	(
    		format "[%" obj.name to:secondStream
    		
    		for i = 1 to obj.positions.count do
    		(
    			format "[%, %, %, %, %]\n" obj.name obj.positions[i].x obj.positions[i].y obj.positions[i].z obj.rotations[i] to:firstStream
    			format ", Line %" lineCounter to:secondStream
    			lineCounter += 1
    		)
    		
    		format "]\n" to:secondStream
    		
    	)
    	
    	--CREATE FILE FROM STREAMS
    	--FOR DEBUG PURPOSE I'M JUST PRINTING TO A SCRIPT WINDOW
    	in_file = newScript()
    	format "%" (firstStream as string) to:in_file
    	format "%" (secondStream as string) to:in_file
    )
    

    Example of output:
    [Tower, 125.665, 131.43, 0.0, (quat 0 0 -0.41532 0.909675)]
    [Tower, -8.12219, -11.5719, 0.0, (quat 0 0 -0.266385 0.963867)]
    [Tower, 85.1183, 38.6826, 0.0, (quat 0 0 -0.41532 0.909675)]
    [Tower, 34.4258, 121.165, 0.0, (quat 0 0 -0.142008 0.989866)]
    [Tower, 137.626, -88.7488, 0.0, (quat 0 0 -0.133201 0.991089)]
    [Tower, -3.80859, -199.553, 0.0, (quat 0 0 -0.299809 0.953999)]
    [Tower, 202.062, 64.4379, 0.0, (quat 0 0 -0.266385 0.963867)]
    [Gate, -124.211, -190.847, 0.0, (quat 0 0 -0.269507 0.962998)]
    [Gate, -35.0458, 156.777, 0.0, (quat 0 0 0.126809 0.991927)]
    [Gate, -25.1697, 50.6722, 0.0, (quat 0 0 0.0161643 0.999869)]
    [Gate, -25.1697, 50.6722, 0.0, (quat 0 0 0.0161643 0.999869)]
    [Gate, 238.714, -196.688, 0.0, (quat 0 0 0.00794686 0.999968)]
    [Gate, -25.1697, 50.6722, 0.0, (quat 0 0 0.0161643 0.999869)]
    [Gate, -108.792, 54.8598, 0.0, (quat 0 0 0.281931 0.959435)]
    [Teapot, -68.9013, -9.15181, 0.0, (quat 0.0220851 -0.197951 -0.0668346 0.977681)]
    [Teapot, 72.2904, -88.2731, 0.0, (quat 0 0 -0.288409 0.957507)]
    [Teapot, 132.705, -191.435, 0.0, (quat 0 0 0.374879 0.927074)]
    [Teapot, -10.9261, -63.1287, 0.0, (quat 0.16181 0.163868 -0.201411 0.95205)]
    [Teapot, -31.7108, -108.121, 0.0, (quat 0 0 -0.516297 0.856409)]
    [Teapot, 68.8799, -40.1832, 0.0, (quat 0.0278817 0.141764 -0.203284 0.968401)]
    [Tower, Line 1, Line 2, Line 3, Line 4, Line 5, Line 6, Line 7]
    [Gate, Line 8, Line 9, Line 10, Line 11, Line 12, Line 13, Line 14]
    [Teapot, Line 15, Line 16, Line 17, Line 18, Line 19, Line 20]
    
  • Primergy
    Options
    Offline / Send Message
    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.