ScriptSpot

Forum topic · General Scripting

Resource: Get All Textures Function

By MattOstgard · 2011-12-20

Description

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 (10)

fajar · 2014-09-04

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 · 2014-07-12

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

simpler code

antomor · 2014-07-11


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

return all textures of selection objects

MattOstgard · 2014-07-11

Added your version. Thanks, Antomor.

vallex · 2014-09-03

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 · 2014-09-04

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

it does not work for blend materials

artrender.info · 2013-04-13

-- Unable to convert: undefined to type: MtlBase

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

MattOstgard · 2011-12-21

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

Anubis · 2011-12-21

what about ...

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

MattOstgard · 2011-12-21

I edited the post. Thanks again :P