Resource: Get All Textures Function

EDIT (2014-07-11): Antomor replied with a version reduced to just 2 lines. This will give you a string array of file paths rather than the actual bitmap instances, though. Use Anubis' getClassInstances version below for bitmapTexture instances.

for obj in selection do join ff (usedMaps obj)
ff = makeUniqueArray ff
print ff

============================

EDIT (2011-12-20): As Anubis pointed out there is a much much better way to do this. Really to the point where there isn't any reason to make a function for it, but here is a better version anyway:

(
	fn GetBitmapTextures theObjects =
	(
		texMaps = #()
		for obj in theObjects do
		(
			join texMaps (getClassInstances bitmapTexture target:obj asTrackViewPick:off)
		)
		makeUniqueArray texMaps
	)
	texMaps = (GetBitmapTextures selection)
	for texMap in texMaps do print (texMap.filename)
	OK
)

============================

ORIGINAL:
-- DON'T USE THIS. Keeping for search keywords.

Hey all,
I wrote a quick function for getting all the textures associated with an object. It seems like something a lot of other people might also need so thought I'd post it up. I've seen a couple similar functions out there, but non of them really seem to take into account either MultiMaterials, or maps that might contain submaps (like normal maps).

Thanks to the-generalist.com for posting this resource, which didn't do what I wanted, but still was a great starting point.

Just do GetBitmapTextures [object] and it will return an array of bitmapTexture maps. From there you can get/set any of the properties.. like returnedArray[1].filename for instance.

-- DON'T USE THIS. Keeping for search keywords.
 
fn GetBitmapTextures theObjects =
(
	-- Because some submaps can also contain submaps we have to collect materials first, and then still append subMapContainers in the loop bitmapTextureMaps loop
	subMapContainers = #()
	for obj in theObjects do
	(
		if obj.material != undefined then
		(
			submatcount = getNumSubMtls obj.material
			for i in 1 to submatcount do
			(
				append subMapContainers (getSubMtl obj.material i)
			)
			append subMapContainers (obj.material)
		)
	)
	bitmapTextureMaps = #()
	for subMapContainer in subMapContainers do
	(
		subtexcount = getNumSubTexmaps subMapContainer
		for i in 1 to subtexcount do
		(
			theSubTexMap = (getSubTexMap subMapContainer i)
			if theSubTexMap != undefined then
			(
				if (getNumSubTexmaps theSubTexMap) > 0 then
				(
					append subMapContainers theSubTexMap
				)
				else if classof theSubTexMap == bitmapTexture then
				(
					append bitmapTextureMaps theSubTexMap
				)
			)
		)
	)
	makeUniqueArray bitmapTextureMaps
)

texmaps = (GetBitmapTextures selection)
for texmap in texmaps do print (texmap.filename)

Comments

Comment viewing options

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

good work vallex , just as

good work vallex , just as barigazy said .... need some case of thing .....and will be done...also need some consideration of vray disp map, displacement map thing, env...and else.

fajar's picture

atomor fn is good but not

atomor fn is good but not return class bitmap instead filename only, for in anubis , its not really work on max 9 when object using mental ray material

antomor's picture

simpler code

for obj in selection do join ff (usedMaps obj)
ff = makeUniqueArray ff
print ff

return all textures of selection objects

vallex's picture

I made this one, it works

I made this one, it works fine:
Tested with most of materials and maps

fn getBitmapFromMaterial mat = (
	local maps = #()
	fn checkMat mtl maps = (
		if mtl != undefined do (
			if hasProperty mtl "filename" do appendIfUnique maps mtl.fileName
			if hasProperty mtl "HDRIMapName" do appendIfUnique maps mtl.HDRIMapName
			---- AND OTHER FILE TYPES ----------------------------
			if (superclassof mtl == textureMap) then (
				local subs = getNumSubTexmaps mtl
				if subs > 0 do for i=1 to subs do checkMat (getSubTexMap mtl i) maps
			) else (
				if mtl.numsubs != undefined and (mtl.numsubs > 0) do 
				for i=1 to mtl.numsubs do checkMat mtl[i] maps
			)
		)
	)
	checkMat mat maps
	maps
)
print (getBitmapFromMaterial meditMaterials[activeMeditSlot])
barigazy's picture

...

Your fn have limitations. You will need to consider much more map types

barigazy

MattOstgard's picture

Added your version. Thanks,

Added your version. Thanks, Antomor.

artrender.info's picture

it does not work for blend materials

-- Unable to convert: undefined to type: MtlBase

I mean your second code, but the first works! THX

MattOstgard's picture

...

... hahahaha.. man.. well thanks..

Anubis's picture

...

what about ...
texmaps = for obj selection collect (getClassInstances bitmapTexture target:obj asTrackViewPick:on)

my recent MAXScripts RSS (archive here)

MattOstgard's picture

I edited the post. Thanks

I edited the post. Thanks again :P

Comment viewing options

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