CGTalk > Software > Autodesk 3ds max > 3dsMax SDK and MaxScript
Login register
Thread Closed share thread « Previous Thread | Next Thread »
 
Thread Tools Search this Thread Display Modes
Old 01-06-2011, 06:48 PM   #1
thorsten hartmann
hot chip
 
thorsten hartmann's Avatar
Thorsten Hartmann
3D Artist
infinity vision
Berlin, Germany
 
Join Date: Oct 2004
Posts: 1,387
How create a external File?

Hi Guys,

I want create a File, for example "data.txt". And i want this text as data.

# Name
# Level
[grid]
1;Ux;Uy;Uz;45
2;Ux;Uy;Uz;45
3;Ux;Uy;Uz;45
4;Ux;Uy;Uz;45
[end]


Thanks for help.

mfg
hot chip
 
Old 01-06-2011, 08:34 PM   #2
TheGrak
Click, type, repeat.
 
TheGrak's Avatar
portfolio
Garrick Campsey
denton, USA
 
Join Date: Apr 2010
Posts: 633
I just became interested in this as well, and after a quick search a few days ago, I came across this bit of code. I can't remember where I saved it from, so if you know who wrote it, chime it. ( :

This script exports the currently selected models height, width, and length to a .csv file.

Code:
------------------------------------------- -- ExportAsCVS.ms -- exports the currently selected models -- height, width, length (in that order) ------------------------------------------- global theFileStream fn exportAs fileName = ( -- create file theFileStream = createFile fileName; if theFileStream != undefined then ( -- file ok to write selection = getCurrentSelection() if selection.count > 0 then ( -- write header printToFile ("x, y, z, Pieces, Name") -- write data for obj in selection do ( _name = obj.name _height = floorToNDecimals(obj.height) as string _width = floorToNDecimals(obj.width) as string _length = floorToNDecimals(obj.length) as string -- assuming pieces is always 1 _pieces = "1" -- write to file printToFile (_length+","+_width+","+_height+","+_pieces+","+_name) ) ) else ( -- error printToFile ("Error: Nothing Selected!") ) ) printToFile ("Yeah!") -- close close theFileStream; ) fn printToFile theString t:0 = ( -- auto tab (empty string) tabString = " "; tabString = substring tabString 1 (t*4); -- print format "%%\n" tabString theString to:theFileStream; ) fn floorToNDecimals theNumber N:2 = ( return floor( ((pow 10 N) * theNumber) ) / (pow 10 N) ) exportAs("Test2.csv");


Loading in the values can be done thru the 'stringStream' thingy in maxscript. Check the help.
__________________

 
Old 01-06-2011, 09:56 PM   #3
thorsten hartmann
hot chip
 
thorsten hartmann's Avatar
Thorsten Hartmann
3D Artist
infinity vision
Berlin, Germany
 
Join Date: Oct 2004
Posts: 1,387
Hi Garrik,


thank you very much, i will try that.

mfg
hot chip
 
Old 01-06-2011, 11:25 PM   #4
PEN
R&D/Technical Director
 
PEN's Avatar
portfolio
Paul Neale
PEN Productions
Canada
 
Join Date: Jun 2002
Posts: 7,416
Code:
f=createFile ((getDir #scripts)+"myFileName.txt") format "data: %" "some thing to put in the file" to:f close f
__________________
Paul Neale
http://paulneale.com
 
Old 01-07-2011, 07:35 AM   #5
lo
the frequentest!
portfolio
Rotem Shiffman
Tel Aviv, Israel
 
Join Date: Jul 2005
Posts: 2,837
If you have all your text in array before writing I find that
Code:
(dotnetClass "System.IO.File").WriteAllLines <filename> <string array>

works much faster
 
Old 01-07-2011, 10:00 AM   #6
thorsten hartmann
hot chip
 
thorsten hartmann's Avatar
Thorsten Hartmann
3D Artist
infinity vision
Berlin, Germany
 
Join Date: Oct 2004
Posts: 1,387
Oh guys,


thank you very much. I will try the codes.

mfg
hot chip
 
Old 01-07-2011, 01:27 PM   #7
PEN
R&D/Technical Director
 
PEN's Avatar
portfolio
Paul Neale
PEN Productions
Canada
 
Join Date: Jun 2002
Posts: 7,416
Quote:
Originally Posted by lo
If you have all your text in array before writing I find that
Code:
(dotnetClass "System.IO.File").WriteAllLines <filename> <string array>

works much faster


So this is faster then format? I'll have to give it a try. Thanks.
__________________
Paul Neale
http://paulneale.com
 
Old 01-07-2011, 02:10 PM   #8
Gravey
3D Dude
Joel Hewitt
Sydney, Australia
 
Join Date: Dec 2005
Posts: 880
Quote:
Originally Posted by PEN
Quote:
Originally Posted by lo
If you have all your text in array before writing I find that
Code:
(dotnetClass "System.IO.File").WriteAllLines <filename> <string array>
works much faster
So this is faster then format? I'll have to give it a try. Thanks.
if you're interested in speed, my guess would be that formatting everything to a stringstream and then formatting the resulting stringstream to a file would be faster (and safer than regular formatting to a file too). My reasoning is that the array of mxs strings wouldnt need to be converted to a dotnet string array before the WriteAllLines function can execute which i dont think scales as well as the stringstream method for large outputs. Please note that my thoughts have not been benchmarked...
 
Old 01-08-2011, 12:25 AM   #9
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,511
Send a message via ICQ to denisT
Quote:
Originally Posted by PEN
So this is faster then format? I'll have to give it a try. Thanks.


my guess is that format has to be faster if you put only one string (it doesn't matter how long the string is).

however if the task doesn't need any particular file format i always save(export) data to XML. it might be slower at the write/read stage but much faster at parse/search.

Last edited by denisT : 01-08-2011 at 12:33 AM.
 
Old 01-11-2011, 11:29 PM   #10
LoneRobot
Swami Lama's Lovechild
 
LoneRobot's Avatar
portfolio
Pete Addington
Technical Director
Nexus
United Kingdom
 
Join Date: Dec 2004
Posts: 1,784
I would second that, XML would be perfect for the sort of thing you are trying to store. My thinking - Is performance really an issue for strings of this length? I wouldn't think so.
__________________
Regards,
Pete

Read my technical blog - lonerobot.net
 
Old 01-12-2011, 07:49 PM   #11
TheGrak
Click, type, repeat.
 
TheGrak's Avatar
portfolio
Garrick Campsey
denton, USA
 
Join Date: Apr 2010
Posts: 633
(I don't mean to hijack this thread, and I think this relates closely with creating an external file)
I'm writing data to an external file, then trying to load that data back in. This is the code so far:
Code:
f=createFile "test.objekt" format "line1:%" "data1\n" to:f format "line2:%" "data2" to:f close f o=openFile "test.objekt" mode:"r+" myData = readDelimitedString o "line2:" print myData

Gives error: Unable to convert: <File:C:.../test.objekt> to type: String
I'm sure I'm missing something obvious, a syntax error. Or perhaps I'm not loading the data in? Hmm
__________________


Last edited by TheGrak : 01-12-2011 at 08:58 PM.
 
Old 01-12-2011, 08:54 PM   #12
lo
the frequentest!
portfolio
Rotem Shiffman
Tel Aviv, Israel
 
Join Date: Jul 2005
Posts: 2,837
I'm not sure you can write to a file without supplying a full path. Is the file actually being created, and if so, where?
 
Old 01-12-2011, 08:57 PM   #13
TheGrak
Click, type, repeat.
 
TheGrak's Avatar
portfolio
Garrick Campsey
denton, USA
 
Join Date: Apr 2010
Posts: 633
lo,
the file is being created at:<File:C:\Documents and Settings\Administrator\Desktop\grakScripts\test.ob jekt>
the script is creating the test.objekt file in the same directory as the script thats running. I think thats the default location if you don't supply a dir, I'm probably wrong on that one though.
__________________

 
Old 01-12-2011, 09:06 PM   #14
TheGrak
Click, type, repeat.
 
TheGrak's Avatar
portfolio
Garrick Campsey
denton, USA
 
Join Date: Apr 2010
Posts: 633
Got it working
Code:
f=createFile "test.objekt" format "line1:%" "data1\n" to:f format "line2:%" "data2" to:f close f o=openFile "test.objekt" mode:"r+" skipToString o "line2:" myReadData = readChars o 5 print myReadData

Needed to read in the data!

Here's a better way of doing that (can read in the whole line instead of certain number of characters)
Code:
f=createFile "test2.objekt" format "Name:%" "myName\n" to:f format "Type:%" "myType\n" to:f format "Size:%" "155.548\n" to:f format "Created:%" "01.12.11\n" to:f close f o=openFile "test2.objekt" mode:"r+" skipToString o "Size:" myReadData = readLine o print myReadData close o
__________________


Last edited by TheGrak : 01-12-2011 at 09:25 PM.
 
Old 01-12-2011, 10:36 PM   #15
thatoneguy
Interesting User Title
 
thatoneguy's Avatar
portfolio
Gavin Greenwalt
Exec. VP of Pixel Mergers and Acquisitions
Straightface Studios
Seattle, USA
 
Join Date: Jan 2005
Posts: 2,332
I do it slightly differently in that I create a stringstream and then at the end of the string-stream do a:

Code:
format stringstreamVar to:fileVar


what I like about doing it that way is you can put all of your file creation at the end of the string creation and a crash or error won't leave an orphaned file.

Also instead of using "createfile" I'll often do:

Code:
openFile filePath mode:"w"


which will create a file if it doesn't exist, or overwrite an existing file.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 05:25 PM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.