Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Importing a text file and creating geometry at coords

Importing a text file and creating geometry at coords

Anonymous
Not applicable
514 Views
6 Replies
Message 1 of 7

Importing a text file and creating geometry at coords

Anonymous
Not applicable
So basically I want to make a Maxscript that would open a text file and create different objects at the coordinates listed in the file... I can't find anything related to this in the forums (Only a year old topic) so if you could point me to any kind of documentation or example script?

Thanks!
0 Likes
515 Views
6 Replies
Replies (6)
Message 2 of 7

gplourde
Participant
Participant
Search the maxscript help for "Read and Write Geometry Data" and "FileStream Values". What you want to do is use the openfile function and read each line of your text file. Should be quite easy to do.
0 Likes
Message 3 of 7

Anonymous
Not applicable
just a quick example writing and reading back pos data:

-- What:
-- Takes all scene objs coords and put them to file koe.txt at "scripts" folder under max root
-- Then it prompts to pick that same file and reads the value line by line
-- Why:
-- to show how to write and read a data file in maxscript :-)
-- How:
-- first create some objects and then run this script
(
local theFile
-- take all scene objects
local theArr = objects as array

-- 1. read objs positions and put them to file
--------------------------------------------------

-- give file a name
theFileName = (GetDir #scripts + "\\koe.txt")
-- try to create a file
try(record_file = createFile theFileName)catch(record_file=undefined)
-- write data to file
for o in objects do format "%\n" (o.pos as string) to:record_file
-- try to close it
try(close record_file)catch()



-- 2. read the file and animate objs from x-axis to orig positions
--------------------------------------------------------
fn doSomethingWithIt arr = (
i = 1
while (not eof theFile) do
(
obj_pos = execute(readLine theFile)
--format "obj_pos: %\n" obj_pos
Point pos:obj_pos
with animate on
(
if arr != undefined do at time 0 arr.pos =
if arr != undefined do at time 30 arr.pos = obj_pos
)
i+= 1
)
)

theFilename = getOpenFileName filename:"koe.txt" types:" Text File (*.txt)|*.txt|All Files (*.*)|*.*"

if theFilename != undefined do
(
theFile = openFile theFilename
doSomethingWithIt theArr
close theFile
)
)


cheers
-R
0 Likes
Message 4 of 7

Anonymous
Not applicable
Thank you both,

Perpixel, i'm a bit confused here... The Maxscript is the videos that come with the program? Where can I find them?

Ripley, I didn't have time to try out your script, but having a quick look: Does it just change the coordinates of previusly created objects that were saved in the file?

I would actually need to create them when I open the file!

The thing I want to do is actually a bit harder, as the text file will be split in different sections starting with a word (OBJ) and ending in 'END' so my goal is to be able to create different objects (for example, a box for the 'OBJ's, and then write them with some other paremeters.

But well, I have to take this step by step.

Will try to look at it later deeper in your script, and thank you! 🙂
0 Likes
Message 5 of 7

gplourde
Participant
Participant
What I'm talking about is not in the videos but in the help files. Check in 3dsmax menu help->maxscript reference. Search in that for the documents and sample code.

I think what you want is this,


f = openfile "d:\\objects.txt"

if f != undefined do
(
LIGHTIMPORT = false
OBJIMPORT = false

while not eof f do
(
line = readline f

if line == "OBJ" do
(
format "Begin object import\n" to:listener
OBJIMPORT = true -- You got the OBJ section so switch to obj import mode
continue -- go to next line
)
if line == "LIGHT" do
(
format "Begin light import\n" to:listener
LIGHTIMPORT = true
continue -- go to next line
)
if line == "END" do
(
format "Done Section\n" to:listener
LIGHTIMPORT = false
OBJIMPORT = false
continue -- go to next line
)

if OBJIMPORT == true do
(
args = filterstring line ":"

if args == "box" then
(
p = filterString args ","
b = box name:args pos: as float,p as float,p as float]
)
else if args == "sphere" then
(
p = filterString args ","
s = sphere name:args pos: as float,p as float,p as float]
)
-- add more obj types

continue -- go to next line
)
)
)


And read a file like this,


OBJ
box:imabox:10,10,10
sphere:imasphere:0,0,0
END
LIGHT
END
0 Likes
Message 6 of 7

Anonymous
Not applicable
Thank you so much! I've understood the code and all, and fixed it to my own file, but one problem appears


--No readline function for ''Path of my file''


What I understand is that the script doesn't know what to do with my file... Could it be that the file is not a '*.txt* but a '*.ide' ? Basically these ides file can be opened with notepad.

I will do some researching, looking in the reference files and in the internet, thanks 🙂

Edit: I'm not using the same command to open my file that yours, I do:

idename = getOpenFileName caption:"Open IDE File" types:"IDE File (*.ide)|*.ide|"


Ahh, I guess that could be the problem now 😛

Edit: I solved it by pointing your opencommand to my idename, but I now get another problem, oops.

Edit: All solved, I got it working. Thanks all :) I just need now to find how to scale these boxes down, and if there is a list of object to create
0 Likes
Message 7 of 7

Anonymous
Not applicable
The first error looks as if you are trying to do readLine() on the string containing the filename rather than a file. getOpenFileName() only returns a string; you still need to open the file itself. After your line, include openFile idename and you should be good.

EDIT: Sorry, didn't notice your edit. But, as for scaling the boxes, you can include additional parameters when you create them:
 b = box name:args pos: as float,p as float,p as float] width:5 length:5 height:5

(Note: this line is taken from Ripley's sample code and refers to the variables args and p; obviously, change them to whatever you're using)
0 Likes