Count materials of each loaded .mat file

This is probably very easy but what I want is to get an array of the number of materials inside a .mat file array.

What I have so far is:

--array of mat files
mat = #("F:\\3dwannab Maps_Corona.mat", "F:\\3dwannab Set_Corona.mat", "F:\Basic_Corona_Set.mat")
 
--loop setup to get the count
for i in 1 to mat.count do (
 
)
 
--for getting materials only count (I need this to count for each mat). I'm not sure how to do this.. At the moment this works for the last material in that above array.
for i in mat do matFile = loadTempMaterialLibrary i
materialsOnly = for i in matFile where superclassof i == material collect i
materialsOnly.count

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
3dwannab's picture

One question

Lets say I've two arrays like so:

#("3dwannab Maps_Corona", "3dwannab Set_Corona", "Basic_Corona_Set", "Jakub_Cech_Corona_set", "Jeff_Patton_Paints_Corona_Set", "Siger_Corona_Set", "VisCorbel_Corona_Set", "VisCorbel_VRay_Set", "VizPark_Corona_Set")
#("6", "109", "20", "14", "11", "10", "79", "79", "5")

And I want to append each of their values to the corresponding mat like so:

New array should be:
#("3dwannab Maps_Corona 6", "3dwannab Set_Corona 109", "Basic_Corona_Set 20", "Jakub_Cech_Corona_set 14", "Jeff_Patton_Paints_Corona_Set 11", "Siger_Corona_Set 10", "VisCorbel_Corona_Set 79", "VisCorbel_VRay_Set 79", "VizPark_Corona_Set 5")

The MSX Array help doesn't show this (at least I can't see it) ;]

miauu's picture

.

Both arrays must have the same amount of elements.

(
	arr01 =	#("3dwannab Maps_Corona", "3dwannab Set_Corona", "Basic_Corona_Set", "Jakub_Cech_Corona_set", "Jeff_Patton_Paints_Corona_Set", "Siger_Corona_Set", "VisCorbel_Corona_Set", "VisCorbel_VRay_Set", "VizPark_Corona_Set")
	arr02 =	#("6", "109", "20", "14", "11", "10", "79", "79", "5")
 
	for i = 1 to arr01.count do arr01[i] += " " + arr02[i]
)
3dwannab's picture

Great, thank you.

The method of my madness was I just wanted to display info in a listbox without the need for using .net for columns. See finished but not fully tested script. It creates teapots from the materials inside .mat files and puts them on teapots. I made it so I could manage my .mat files better and update the Materials when I needed to. It recurses through directories also.

The code Swordslayer gave me didn't work as intended:
for i in matFile where isKindOf i Material collect i

So I went with this instead which accurately gets all the Materials and Maps.
for i in matFile where superclassof i == material collect i

Would you know why this is the case here? I've commented in the script where this is.

See attached script and code below also.

/*
3dwannab Materials in .mats to teapots in scene
 
v1.0 - 2016.01.25 - Initial Release
 
*/
if rol_Mats2Scene != undefined do (destroyDialog rol_Mats2Scene)
rollout rol_Mats2Scene ".mat Library Scene Creation"
 
(
	local matCount_Arr = #()
	local mapCount_Arr = #()
	local matLibDir_Arr = #()
	local matLibFiles_Arr = #()
	-- change this to your directory, script uses recurise scanning so set it as the root folder if you wish, check the checkbox 'Recurse Directories'.
	local intialMLPath = "F:\\SS_3diT\\Textures & Materials\\Corona Materials Collection\\"
 
	label lbl_directory "Looking in Directory: " align:#left across:2
	label lbl_M2SPath intialMLPath align:#left offset:[-180,0]
	button btn_getDir "Choose Library Directory" align:#left across:3
	checkbox chk_recurseDir "Recurse Directories" align:#center checked:false
	button btn_update "Update" align:#right
	listBox ml_list items:#() height:15
	label lbl_usage "Double click .mat file to create Teapots with all its materials in the current scene"
 
	fn initialDirCheck = (
		if DoesFileExist intialMLPath == false then (
			matLibDir_Arr = GetDir #matlib + "\\"
		)
		else (
			matLibDir_Arr = intialMLPath
		)
	)
 
	fn getMaxFiles_fn dir pattern = (
		if rol_Mats2Scene.chk_recurseDir.checked == true then (
			local dir1 = getDirectories (dir + "*")
			for d in dir1 do (
				join dir1 (getDirectories (d + "*"))
			)
			local files = #()
			for f in dir1 do (
				join files (getFiles (f + pattern))
			)
			appendIfUnique dir1 1 -- fix to get current folder too
			return files
		)
		if rol_Mats2Scene.chk_recurseDir.checked == false then (
			files = (getFiles (dir + pattern))
			return files
		)
	)
 
	fn getMatsInfo_Arr = (
 
		matCount_Arr = for i in matLibFiles_Arr collect
		(
			local matFile = loadTempMaterialLibrary i
			-- isKindOf didn't work as expected so I reverted to getting superclassof instead
			local materialsOnly = try(for i in matFile where superclassof i == material collect i)catch() --isKindOf i Material collect i
			if materialsOnly != undefined then materialsOnly.count else matCount_Arr = "UNKNOWN, Load plug-ins for"
		)
	)
 
	fn getMapsInfo_Arr = (
 
		mapCount_Arr = for i in matLibFiles_Arr collect
		(
			local mapFile = loadTempMaterialLibrary i
			-- isKindOf didn't work as expected so I reverted to getting superclassof instead
			local mapsOnly = try(for i in mapFile where superclassof i != material collect i)catch() --isKindOf i Material collect i
			if mapsOnly != undefined then mapsOnly.count else mapCount_Arr = "UNKNOWN, Load plug-ins for"
		)
	)
 
	fn mainFN = (
		initialDirCheck()
		matLibFiles_Arr = getMaxFiles_fn matLibDir_Arr "\\*.mat"
		getMatsInfo_Arr()
		getMapsInfo_Arr()
		matsNames_Arr = for i in matLibFiles_Arr collect (getFilenameFile i)
		list2 = for i = 1 to matsNames_Arr.count do matsNames_Arr[i] += " | " + matCount_Arr[i] as string + " Material/s | " + mapCount_Arr[i] as string + " Texture Map/s"
		ml_list.items = matsNames_Arr
		lbl_M2SPath.text = matLibDir_Arr
	)
 
	fn ResetViewports = (
		actionMan.executeAction 0 "40043" -- Selection: Select None
		viewport.ResetAllViews() --resets to Max Standard
		actionMan.executeAction 0 "331"  -- Tools: Zoom Extents All
		actionMan.executeAction 0 "311" --frames all viewports
		viewport.zoom .75 --fix to make perspective view appear closer
		viewport.setGridVisibility #all false --turns off all grids
		actionMan.executeAction 0 "40829" --show statistics
		completeredraw() --redraw the views
 
		--set max display colors (Choose #material or #object for either)
		displayColor.shaded = #material
		displayColor.wireframe = #object
	)
 
	fn ResetViewportsForCoronaIRRender = (
		if (matchPattern (renderers.current as string) pattern:"Corona*") then (
 
			viewport.ResetAllViews() --resets to Max Standard
 
			PersViewMatrix = viewport.getTM() -- Get perspective matrix3 (viewpoint)
			zoomAmount = .75 as float
 
			ViewCubeOps.Visibility = false
 
			viewport.setGridVisibility #all false --turns off all grids
			actionMan.executeAction 0 "63547"  -- Views: Viewport Materials Display as Realistic with Maps
 
			viewport.setLayout #layout_3hb
			actionMan.executeAction 0 "40829" --show statistics
 
			ViewPanelManager.SetSplitterBarPos 0 0.55
			ViewPanelManager.SetSplitterBarPos 1 0.65
 
			viewport.activeViewportEx 3
			viewport.setType #view_persp_user
			viewport.setTM PersViewMatrix -- Set the view stored above
			max zoomext sel
			viewport.zoom zoomAmount --fix to view appear closer
			actionMan.executeAction 0 "40406"  -- Views: Extended Views 3
 
			viewport.activeViewportEx 2
			viewport.setType #view_persp_user
			viewport.setTM PersViewMatrix -- Set the view stored above
			max zoomext sel
			viewport.zoom zoomAmount --fix to view appear closer
			--actionMan.executeAction 0 "369"  -- Views: View Edged Faces Toggle
			actionMan.executeAction 0 "272"  -- Views: Wireframe / Smooth+Highlights Toggle
 
			viewport.activeViewportEx 1
			max zoomext sel
			viewport.zoom zoomAmount --fix to view appear closer
			viewport.setType #view_top
 
			actionMan.executeAction 0 "331"  -- Tools: Zoom Extents All
 
			completeredraw() --redraw the views
			max tool maximize ; max tool maximize -- hack to get the viewport to update outline
 
			viewport.activeViewportEx 2 -- return to perspective
 
			try(if (for i in cameras where superclassof i == Camera collect i).count == 1 then (
				viewport.setType #view_camera
			))catch()
 
			--#layout_1 | #layout_2v | #layout_2h | #layout_2ht | #layout_2hb | #layout_3vl | #layout_3vr | #layout_3ht | #layout_3hb | #layout_4 | #layout_4vl | #layout_4vr | #layout_4ht | #layout_4hb
			--#view_left | #view_right | #view_top | #view_bottom | #view_front | #view_back | #view_iso_user | #view_persp_user | #view_camera | #view_grid | #view_none | #view_spot | #view_shape
 
		)
	)
 
	on rol_Mats2Scene open do (
		initialDirCheck()
		lbl_M2SPath.text = intialMLPath
-- 		matLibDir_Arr = intialMLPath
		matLibFiles_Arr = getMaxFiles_fn matLibDir_Arr "\\*.mat"
		getMatsInfo_Arr()
		getMapsInfo_Arr()
		matsNames_Arr = for i in matLibFiles_Arr collect (getFilenameFile i)
		list2 = for i = 1 to matsNames_Arr.count do matsNames_Arr[i] += " | " + matCount_Arr[i] as string + " Material/s | " + mapCount_Arr[i] as string + " Texture Map/s"
		ml_list.items = matsNames_Arr
	)
 
	on rol_Mats2Scene close do (
-- 		intialMLPath = undefined
	)
 
	on btn_getDir pressed do (
		matLibDir_Arr = getSavePath caption:"Choose .mat Library Directory or root Directory" initialDir:intialMLPath
		if matLibDir_Arr != undefined do (
			mainFN()
		)
	)
 
	on btn_update pressed do (
		mainFN()
	)
 
	on chk_recurseDir changed state do (
		if state then (
			mainFN()
		) else (
			mainFN()
		)
	)
 
	on ml_list doubleClicked itm do	(
		if querybox "Do you create a new scene ?" title:"Create New Scene" beep:true
		then (
			actionMan.executeAction 0 "40004"  -- File: New Scene
			teaRadius = 10
			matFile = loadTempMaterialLibrary matLibFiles_Arr[itm]
 
			-- used for testing only
			-- matFile = loadTempMaterialLibrary "F:\\SS_3diT\\Textures & Materials\\Corona Materials Collection\\3dwannab Set\\3dwannab Set_Corona.mat"
			-- print matFile
 
			materialsOnly = for i in matFile where superclassof i == material collect i
			delete $Teapot* ; teapotsArr = for i in 1 to materialsOnly.count collect Teapot radius:teaRadius smooth:on segs:10 body:on handle:on spout:on lid:on mapcoords:on pos:[0,0,0] pos:[teaRadius*3*i,0,0]
			for i = 1 to materialsOnly.count do objects[i].material = materialsOnly[i]
			print ("Teapots created with " + materialsOnly.count as string + " material/s")
 
 
			ResetViewports()
			ResetViewportsForCoronaIRRender()
		)
		else (
			teaRadius = 10
			matFile = loadTempMaterialLibrary matLibFiles_Arr[itm]
 
			-- used for testing only
			-- matFile = loadTempMaterialLibrary "F:\\SS_3diT\\Textures & Materials\\Corona Materials Collection\\3dwannab Set\\3dwannab Set_Corona.mat"
			-- print matFile
 
			materialsOnly = for i in matFile where superclassof i == material collect i
			delete $Teapot* ; teapotsArr = for i in 1 to materialsOnly.count collect Teapot radius:teaRadius smooth:on segs:10 body:on handle:on spout:on lid:on mapcoords:on pos:[0,0,0] pos:[teaRadius*3*i,0,0]
			for i = 1 to materialsOnly.count do objects[i].material = materialsOnly[i]
			print ("Teapots created with " + materialsOnly.count as string + " material/s")
			select teapotsArr
			max zoomext sel
			max select none
		)
	)
)
createDialog rol_Mats2Scene 600 278 style:#(#style_titlebar,#style_sysmenu,#style_minimizebox)
AttachmentSize
mats2scene.ms 8.57 KB
Swordslayer's picture

You almost had it :) mat =

You almost had it :)

mat = #(@"F:\3dwannab Maps_Corona.mat", @"F:\3dwannab Set_Corona.mat", @"F:\Basic_Corona_Set.mat")
 
matCounts = for i in mat collect
(
	local matFile = loadTempMaterialLibrary i
	local materialsOnly = for i in matFile where isKindOf i Material collect i
	materialsOnly.count
)
3dwannab's picture

Thanks Swordslayer

I never used the while command so I came up with this method which doesn't get an array like yours but might come in useful in my quest to learn MXS.

mat = #("F:\3dwannab Maps_Corona.mat","F:\3dwannab Set_Corona.mat","F:\Basic_Corona_Set.mat")
 
while mat.count >= 1 do (
	print mat[1]
	matFile = loadTempMaterialLibrary mat[1]
	materialsOnly = for i in matFile where superclassof i == material collect i
	matCount = materialsOnly.count
	print matCount
	deleteItem mat 1
)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.