ScriptSpot

Forum topic · General Scripting

isolate number from format string with quote ""

By Michele71 · 2015-03-30

Description

Hi all :)

They are the last details of my script (Environment Lighting HDR Studio (MR) ) but now I've a problem that I can not solve...

I want to enter parameters in rollut taking them from a txt file, but the info in file txt are written as Reflections (Subactive Color): (color 255 255 255) or Glossines: 8.0 ecc.. Now I know this form to isolate number from string array:


test = "Max Distance FallOff: 2.0"
strArr = filterString test " "
valuespinner = strArr[4] as float
print valuespinner

but I can not transfer ONLY values in rollout, because I always error of various kinds (!)
If I try with "openFile" and "readValue", (for read the file txt) I've always the result, for example, "scale()", but never specific value. I had no trouble writing values to and from .txt, but now I can not do this..

The question is very simple: How isolate a number from a string with quote "" and restore the values of spinner etc. the rollout reading a file .txt?

Any help is very important to me :D

Thanks
Michele71

Comments (28)

victorgilbert · 2015-04-15

Thanks for the stuff. it is more like a gift for me to have such content as being fond of the codes.

Michele71 · 2015-04-02

Thanks barigazy for the link.

My programming is really basic ( :D ) and anything that can increase the information and study is well accepted! I find Dotnet too difficult for me now...

Thanks for the help and valuable resources!!

Michele71 · 2015-04-01

Well, with the FN of Barigazy I've solved many parameters that work very well, while for the pickcolor, don't find solution. The suggestion of pixamoon


(color (strArr[i+2] as float) (strArr[i+3] as float) (strArr[i+4] as float))

does not work. With the fn I have always the result of 0.0...

I keep (work permitting) to seek solution :)

pixamoon · 2015-04-01

ah, because last value has ")" -> "127.5)"
you need to remove ")" and then convert to float

`

pixamoon · 2015-04-01

something like this: ?


(
	local csvFile = "C:\\renderpreview_0004.txt"
	local adata = (dotnetClass "System.IO.File").ReadAllLines csvFile
	local strArr = #()
 
	for i = 1 to adata.count do (
		pInfo = (filterString adata[i] " ")
		for o in pInfo do append strArr o
	)
 
	for i = 1 to strArr.count do (
		case strArr[i] of (
			"Reflection": ( if strArr[i+1] == "Color:" and strArr[i+2] == "(color" do RefColor = (color (strArr[i+3] as float) (strArr[i+4] as float) ((substring strArr[i+5] 1 (strArr[i+5].count-1)) as float)) )
		)
	)
)

print RefColor

Michele71 · 2015-04-01

WoW! thanks, now try it! :)

`

pixamoon · 2015-04-01

great :)
but this is starting to be complicated reading, maybe it will be better to format differently on save so you get: (with "|" quote)
name|value
or for color
name|value|value|value <- format "%|%|%|%" a.name a.color.red a.color.green a.color.blue
Than will be very easy to read single values

Michele71 · 2015-04-01

Hahahahah yes, you're right :D
Unfortunately took an unexpected direction, I did not think of creating a command to reload for spinner, but I am learning a lot from this.

However your advice WORK and I am very happy, even if it is a bit "confusing" :)

Thanks a lot! really !!

`

pixamoon · 2015-04-02

yes, me too. this was nice brain training :)

One more idea for saving settings is .ini file. But you know this probably.
It looks very easy with code:

setINISetting IniFIle "Settings" "glossines" (value as string)
getINISetting IniFile "Settings" "glossines"

I think I'll be using ini or xml :)
Best,

`

pixamoon · 2015-04-02

wow, great examples and tutorials lol, thank you !

I would say .ini is good for just few values to save / load (because easy single lines to get and set values in code)
And for larger amount settings /data -> definitely faster XML !

What you think ?

barigazy · 2015-04-02

that's right

;)

`

pixamoon · 2015-04-02

:)

Thank you again for XML examples... I still need to learn this, I have few ideas where to use it, but didn't have time to learn it. Now will be very easy.

Thank you !

`

pixamoon · 2015-04-02

ah, and the last thing I saw somewhere to convert string to Poin3 but it works with color too!

a = execute "(color 10 10 10)"
a = execute "[10,10,10]"

then it can be much easier to get color form your text file:


(
	local csvFile = "C:\\renderpreview_0004.txt"
	local adata = (dotnetClass "System.IO.File").ReadAllLines csvFile
	local strArr = #()
 
	for i = 1 to adata.count do (
		pInfo = (filterString adata[i] " ")
		for o in pInfo do append strArr o
	)
 
	for i = 1 to strArr.count do (
		case strArr[i] of (
			"Reflection": ( if strArr[i+1] == "Color:" then RefColor = execute (strArr[i+2] + " " + strArr[i+3] + " " + strArr[i+4] + " " + strArr[i+5]) )
		)
	)
)
print RefColor

But if you have ini file it will be so easy to convert string "(color 10 10 10)" to color value.
Sorry I forgot about it yesterday.
Best,

`

pixamoon · 2015-04-02

and even better:

this is what I found in maxscript help about execute

readExpr <filestream>

and

execute <filestream>

maxscript sample:



s=stringstream "random 0. 1.;random red blue"
readvalue s -- read and evaluate the first value
readvalue s -- read and evaluate the second value
seek s 0 -- position at beginning of stringstream
readexpr s -- read and evaluate the first expression
readexpr s -- read and evaluate the second expression
seek s 0 -- position at beginning of stringstream
execute s -- evaluate all expressions

and this should be very easy to read from txt file :)

Michele71 · 2015-04-01

Heheheheh I have noticed of this :D The problem is that then the development there is always something wrong... your advice anyway are very useful, I discovered things I did not know :) thanks!!!

barigazy · 2015-03-30

Also to store some data of any tool (read/write) the best and fastest way will be XML.

barigazy · 2015-03-30

Yup. Can be done with .net but ...

test = "Max Distance FallOff: 2.0"
fn extractValue string asFloat:on nums:"1234567890." =
(
name = trimright (trimleft string nums) nums
string = substituteString string name ""
if asFloat then string as float else string
)
extractValue test

Michele71 · 2015-03-30

Thanks barigazy, I try your function :)

At this point I agree with you as regards the format xml....

Thanks Again!!

`

pixamoon · 2015-03-30

why not go with loop and case (of course after filtering)

so if you have strArr array:


for i in strArr.count do (
	case strArr[i] of (
		"Glossines:":(glossines = strArr[i+1] as float)
		"GlossySample:":(glossyS = strArr[i+1] as float)
		--- etc
	)
)

just an idea.
value is always next after name
and "Glossines:" u have probably as a rollout elements .text

It doesn't work for color but than you can do

(color (strArr[i+2] as float) (strArr[i+3] as float) (strArr[i+4] as float))

ah but u need to have written in file "Glossy_Sample" instead of "Glossy Sample"
or remove spaces before writing so you get "GlossySample"

Michele71 · 2015-03-30

Thanks pixamoon :)

Before posting my question, I have tried a simil loop, but without success. Now I perform also your advice and see what appears!

Thanks for your patience and response :)

sure

pixamoon · 2015-03-30

thats the 1st way (loop every line to get values):


(
	local csvFile = "C:\\renderpreview_0004.txt"
	local adata = (dotnetClass "System.IO.File").ReadAllLines csvFile
		
	for i = 1 to adata.count do 
	try(
		pInfo = (filterString adata[i] " ")
		case pInfo[1] of (
			"Glossines:":glossines = pInfo[2] as float
			
		)
	)
	catch(print ("Error in line: " + i as string))
)

print glossines

and this is 2nd (append all single words to one array and than loop it and search for values)


(
	local csvFile = "C:\\renderpreview_0004.txt"
	local adata = (dotnetClass "System.IO.File").ReadAllLines csvFile
	local strArr = #()
	
	for i = 1 to adata.count do (
		pInfo = (filterString adata[i] " ")
		for o in pInfo do append strArr o
	)

	for i = 1 to strArr.count do (
		case strArr[i] of (
			"Glossines:":glossines = strArr[i+1] as float
			
		)
	)
)

print glossines

Of course you need single words, like "Glossines:", "GlossySample" etc

What you think ?

Michele71 · 2015-03-30

Yea, I'm doing the tests, but I think the concept is right ;) I try to do the tests then I'll know!

thanks again!!!!

Michele71 · 2015-03-30

Yes pixamoon, I have already read that discussion, but I was not help ... I thank you anyway for your attention and response :)

`

pixamoon · 2015-03-30

I'm bit not sure how and which values you want to get from txt file.
I dont understand where you get "scale()" etc. instead of value...
Can you post or send me part of this txt file ? pixamoon@gmail.com

Michele71 · 2015-03-30

Sure! I attach here the file exemple file .txt

What is on in rollout is printed in the .txt file, so not everything is written (plese see Environment Lighting HDR Studio (MR) to understand the working). The value to get from .txt is  number and true or false (to send in spinner rollout ecc).

Attachments