Notifications
Clear all

[Closed] variables reading string values

Hi Max scripting brains trust

I am wondering is it at all possible to extract a value from a file string in a for i loop where each value in string is different ie


for i = 1 to 5 do
(
newmat = VRayMtl()
newmat.name = ("m_con_type_" + i as string)
newmat.texmap_diffuse = Bitmaptexture fileName:(@"\\foo\blah\rendermap_00" + i as string + "_d100_s20_b10.exr")
meditmaterials[i] = newmat

)

I am still feeling my way around with max script but in this simple example I just created a simple loop that creates 5 materials , assigns a bitmap where i is the number of a bitmap
so each material gets file “rendermap_001_d100_s20_b10.exr” number 1 to 5 . So then I thought would it be possible to read values from the string . ie I create variables d = diffuse , s = spec , b = bump then read those values from the string and assign to a parameter.

Would this be possible

b

7 Replies

Yes, you can parse the string to get the values out.

For simplicity, the convention used to name the textures should always be the same.

If you will decide what that convention will be, then you have full control of it, and so you can adapt it at your whish.

For example, given the following texture name: “stone_pink_001_d100_s20_b10.exr” we can break it as:

  1. Name
  2. Index
  3. Diffuse amount
  4. Specular amount
  5. Bump amount
  6. Extension

If we split the string by “_” and “.”, then we would get:
#(“stone”, “pink”, “001”, “d100”, “s20”, “b10”, “exr”)

Now you can see that the last 4 values are Diff, Spec, Bump and extension and so we can easily parse it to get the values you need as follow:

I’ve added “dsb” letters to the filter to avoid later parsing of the strings.

(
   	
   	fn GetValuesFromString str =
   	(
   		str = filterString str "_.dsb"
   		
   		bump = str[str.count-1] as float
   		spec = str[str.count-2] as float
   		diff = str[str.count-3] as float
   		
   		return #(diff, spec, bump)
   	)
   	
   	GetValuesFromString "stone_pink_001_d100_s20_b10.exr"
   	
   )

You could also use RegEx for this, but it would be harder to build the expression if you have never used it, and in this case I see no big benefit using it.

it’s too simple to be right what’s if any letter of “dsb” used in the ‘name’ part?

It should work too, as it reads right to left.

I mistyped the function name, it should be “GetValuesFromString” instead of “GetMatValuesFromString”. Fixed now.

2 Replies
(@denist)
Joined: 11 months ago

Posts: 0

you are right. me bad. sorry

thought it works i would not filter using a letters. e.g a texture has ‘bmp’ extension

(@polytools3d)
Joined: 11 months ago

Posts: 0

If it is a .BMP file it should also work, as “.” and “b” will be filtered out leaving “mp” as the last item in the array.

I should probably use a different convention for naming the textures, and so avoiding any possible conflict.

if the tecture format is exact ‘name-index-diffuse-spec-bump-extension’ a parsing could be:

fn parseMyTexture texturename = 
(
	file = getfilenamefile texturename
	parts = filterstring file "_"
	count = parts.count
	bump = (substring parts[count] 2 -1) as float
	spec = (substring parts[count-1] 2 -1) as float
	diff = (substring parts[count-2] 2 -1) as float
	index = parts[count-3] as integer
	techcount = 0
	for k = 0 to 3 do techcount += parts[count-k].count + 1
		
	name = substring file 1 (file.count - techcount)
	#(name, index, diff, spec, bump)
)	

parseMyTexture "stone_pink_001_d100_s20_b10.exr"

Thanks for the answers gents , I will digest

b