[Closed] TextFile Reading and retrieve object, unable to get object's parameters afterwords
Hi,
I’m kind of stuck on a stupid thing, i don’t get where my mistake is, i wrote a little example code to create and write into text file and in second part retrieve the text file information :
Model = #($Model_001,$Model_002,$Model_003,$Model_012,$Model_008)
IntegerNum = 1
OriginalObject = $
TxtPath = maxFilePath + "Variable.txt"
newTXT = createFile TxtPath -- Create textfile
format "%
" Model.count to:newTXT -- scroll through Model
for i=1 to Model.count do
(
format "%" Model[i].name to:newTXT -- Model name
)
format "%
" IntegerNum to:newTXT -- Integer
format "%
" OriginalObject to:newTXT -- Source
close newTXT
print "______________________________________________________"
-- END OF WRITING FILE
-----------------------------------------------------------------------------------------
The generated Textfiles looks like that :
5
Model_001Model_002Model_003Model_012Model_0081
$Sphere:Test @ [-109.074417,-437.838104,51.278885]
And the Reading code looks like that:
-- READING TXT
TxtPath = maxFilePath + "Variable.txt"
openTxt = openFile Txtpath
ModelCount = readLine openTxt -- Read the Models count
ModelCount = ModelCount as Integer
for i = 1 to ModelCount do -- Read the Model name
(
append Model (readChars openTxt 9)
)
-- skipToNextLine openTxt
IntegerNum = readLine openTxt -- Read IntegerNum
IntegerNum = IntegerNum as integer
-- skipToNextLine openTxt
OriginalObject = readLine openTxt -- Read Objet Source
--OriginalObject = "$" + OriginalObject
close openTxt
select Model[1]
select OriginalObject
CopyObject = snapshotasmesh OriginalObject -- Objet de support en mesh
ModelLocal = Model[1]
ModelLocal.visibility = bezier_float()
The select seems to find the Model[1] but select OriginalObject doesn’t. Finally the snapshopasmesh and the ModelLocal.visibility doesn’t either.
PS : i also tried with a editpoly object as OriginalObject, same thing.
Thank you for your help
there are some mistakes in your code. save the origin node’s name (not the node itself). set some delimiter (token) between node names. and… if you want to do scripting in the future, start using XML format files.
Well, i’m just starting Maxscript and i taught it would be easier to write in text file instead of XML trough DotNET , but next i’ll do it, the node-based-structure is much better.
Thank you, for your advice
Hi Denis,
I’m also interesting in the better way to write/read XML file, but also there is XPath which is a bit difficult for me to understand. I found this can be useful link for the start (XPath and XML)
This snippet I used for storing some settings for my ScriptRun tool but I think that is not right choise. If you have free time can you show as some PRO – tip. Thanks in advance
(
local okToShow = true
dotnet.loadAssembly "system.xml"
struct sr_Settings_struct (lvDataArr, locX, locY, ms, mse, mcr, mxsArr, mxsENArr, mxsFav)
struct sr_XML_struct
(
xmlFile,
sioDir = dotNetClass "System.IO.Directory",
sioFile = dotNetClass "System.IO.File",
sysStr = dotNetClass "System.String",
fn compareFNames arr str ignoreCase:true multi:true =
(
if multi then (for p in 1 to arr.count where (sysStr.Compare arr[p][2] str ignoreCase) == 0 collect p).count
else (for p in 1 to arr.count where (sysStr.Compare arr[p] str ignoreCase) == 0 collect p).count
),
fn isValidXML file =
(
try ((dotNetObject "System.Xml.XmlDocument").Load file == undefined)
catch (okToShow = false ; ((trimleft ((s=filterstring (getCurrentException()) ":")[s.count]) " ")+" Do you want to edit 'srSettings.xml' ?"))
),
fn xmlCreator dnXML: prtNode: tagName: innerTxt: attArr: make:#xmlElement =
(
local tag = if make != #xmlComment then (dnXML.createElement tagName) else (dnXML.createComment tagName)
prtNode.AppendChild tag
if innerTxt != unsupplied do tag.InnerText = innerTxt
if attArr != unsupplied do (for i = 1 to attArr.count do tag.SetAttribute attArr[i][1] attArr[i][2]) ; tag
),
fn getXMLvalue dnXML: tagName: type: =
(
local val = (dnXML.selectNodes tagName).ItemOf[0].InnerText
if type != unsupplied then val as type else val
),
fn saveXML xmlFile pX: pY: msExt: mseExt: mcrExt: dirArr: favArr: =
(
xmldoc = dotNetObject "System.Xml.XmlDocument"
sRun = xmlCreator dnXML:xmldoc prtNode:xmldoc tagName:"ScriptRun" \
attArr:#(#("version","1.1a"), #("author","Branko Zivkovic"), #("email","barigazy@hotmail.com"))
sPaths = xmlCreator dnXML:xmldoc prtNode:sRun tagName:"FolderPaths"
cmt_sPaths = xmlCreator dnXML:xmldoc prtNode:sPaths tagName:"Child elements of the *FolderPaths* element tags (<fp_0 ... />, <fp_1 .../> etc.)" make:#xmlComment
for i = 1 to dirArr.count do
(
fpe = xmlCreator dnXML:xmldoc prtNode:sPaths tagName:("fp_" + ((i-1) as String)) \
attArr:#(#("path", dirArr[i][2]), #("use_subfolders", (dirArr[i][1] as String)))
)
sSett = xmlCreator dnXML:xmldoc prtNode:sRun tagName:"ScriptSettings"
sLocX = xmlCreator dnXML:xmldoc prtNode:sSett tagName:"location_x" innerTxt:(pX as String)
sLocY = xmlCreator dnXML:xmldoc prtNode:sSett tagName:"location_y" innerTxt:(pY as String)
sMS = xmlCreator dnXML:xmldoc prtNode:sSett tagName:"use_ms" innerTxt:(msExt as String)
sMSE = xmlCreator dnXML:xmldoc prtNode:sSett tagName:"use_mse" innerTxt:(mseExt as String)
sMCR = xmlCreator dnXML:xmldoc prtNode:sSett tagName:"use_mcr" innerTxt:(mcrExt as String)
sFav = xmlCreator dnXML:xmldoc prtNode:sRun tagName:"FavoriteScripts"
cmt_sFav = xmlCreator dnXML:xmldoc prtNode:sFav tagName:"Child elements of the *FavoriteScripts* element tags (<fs_0 ... </fs_0>, <fs_1 ... </fs_1> etc.)" make:#xmlComment
if favArr.count != 0 do
(
favArr = for s in favArr where sioFile.Exists s collect s
if favArr.count != 0 do (for i = 1 to favArr.count do xmlCreator dnXML:xmldoc prtNode:sFav tagName:("fs_" + ((i-1) as String)) innerTxt:(favArr[i]))
)
xmldoc.Save xmlFile
),
fn loadXML xmlFile = if (v=isValidXML xmlFile) != true then (if queryBox v title:"srSettings.xml Error Message" beep:false do edit xmlFile) else
(
local lvItms = #(), xpos, ypos, useMS, useMSE, useMCR, mxsFiles = #(), mxsFilesEN = #(), mxsFavorites = #()
xmldoc = dotNetObject "System.Xml.XmlDocument"
sioSOpt = dotNetClass "System.IO.SearchOption"
xmldoc.Load xmlFile
rootEle = xmldoc.DocumentElement
fp = rootEle.Item["FolderPaths"]
ss = rootEle.Item["ScriptSettings"]
fs = rootEle.Item["FavoriteScripts"]
if fp.ChildNodes.count == 0 then lvItms = #(#(false, (getDir #scripts))) else
(
for i = 1 to fp.ChildNodes.count-1 do
(
usf = fp.ChildNodes.ItemOf[i].Attributes.ItemOf["use_subfolders"].value as BooleanClass
dir = fp.ChildNodes.ItemOf[i].Attributes.ItemOf["path"].value
if sioDir.Exists dir do append lvItms #(usf,dir)
)
)
xpos = (ss.Item["location_x"].InnerText) as Integer
ypos = (ss.Item["location_y"].InnerText) as Integer
useMS = (ss.Item["use_ms"].InnerText) as BooleanClass
useMSE = (ss.Item["use_mse"].InnerText) as BooleanClass
useMCR = (ss.Item["use_mcr"].InnerText) as BooleanClass
if lvItms.count != 0 do
(
stateArr = #(useMS, useMSE, useMCR)
extArr = #("*.ms", "*.mse", "*.mcr")
for i = 1 to lvItms.count do
(
filtOpt = if lvItms[i][1] then sioSOpt.AllDirectories else sioSOpt.TopDirectoryOnly
for s = 1 to 3 where stateArr[s] do join mxsFiles (sioDir.GetFiles lvItms[i][2] extArr[s] filtOpt)
)
mxsFiles = makeUniqueArray mxsFiles
mxsFilesEN = for f in mxsFiles collect #(getFilenameFile f, toLower (trimleft (getFilenameType f) "."))
if fs.ChildNodes.count > 1 do
(
for i = 1 to fs.ChildNodes.count-1 where sioFile.Exists (fs.Item[("fs_" + (i-1) as string)].InnerText) do append mxsFavorites (fs.Item[("fs_" + (i-1) as string)].InnerText)
/*(
fsc = getXMLvalue dnXML:xmldoc tagName:("//fs_" + i as String)
if mxsFavorites.count == 0 then (if sioFile.Exists fsc do append mxsFavorites fsc)
else (if sioFile.Exists fsc and (compareFNames mxsFavorites fsc multi:false) != 1 do append mxsFavorites fsc)
)*/
)
srSETT = sr_Settings_struct lvDataArr:lvItms locX:xpos locY:ypos ms:useMS mse:useMSE mcr:useMCR mxsArr:mxsFiles mxsENArr:mxsFilesEN mxsFav:mxsFavorites
)
),
on create do
(
xmlFile = getdir #expression + "\\srSettings.xml"
if sioFile.Exists xmlFile then loadXML xmlFile else
(
local posX = ((dotNetClass "Screen").PrimaryScreen.WorkingArea.Width/2) - 120
local posY = ((dotNetClass "Screen").PrimaryScreen.WorkingArea.Height/2) - 11
saveXML xmlFile pX:posX pY:posY msExt:true mseExt:true mcrExt:false dirArr:#(#(false, (getDir #scripts))) favArr:#()
loadXML xmlFile
)
)
)
)