Grabbing all the textures on a mesh?

Blitz3D Forums/Blitz3D Programming/Grabbing all the textures on a mesh?

Guy Fawkes(Posted 2013) [#1]
Hi all. I just wanted to know how to grab more than one texture name from a model, if there is more than 1 texture applied. I have this code I made here, but it is only capable of grabbing 1 texture... I need to be able to grab all the names of all the textures on a 3D model, regardless if it is a parent or a child.

Here's the code:

Function ReadMeshTextures$(M)
	For S=1 To CountSurfaces(M)
		Surf = GetSurface(M,S)
		Brush = GetSurfaceBrush(Surf)
		Tex = GetBrushTexture(Brush)
		TexName$ = TextureName(Tex)
	Next
	Return TexName$
End Function


If you need me to explain more indepth, please ask me.

Thank You all so kindly, and have a good day :)


Rroff(Posted 2013) [#2]
Are you loading the model with loadanimmesh?

EDIT: This is the code I used in an old project - I've no idea if its fully working or at all useful its a long time since I touched it - doesn't seem to do anything with the surfaces just iterates through the model groups:

Function meshMatList(this.meshCache)
	Local surface
	Local brush
	Local texture
	Local child
	Local count
	
	For count = 1 To CountChildren(this\entity)
		child = GetChild(this\entity,count)
		If (child) Then
			surface = GetSurface(child,1)
			brush = GetSurfaceBrush(surface)
			texture = GetBrushTexture(brush,0)
			;conlog(stripExt$(stripPath$(TextureName$(texture))))
			matApply(child,stripExt$(stripPath$(TextureName$(texture))))
			; apply material
			FreeBrush brush
		EndIf
	Next
End Function



Guy Fawkes(Posted 2013) [#3]
Yea, I'm using LoadAnimMesh(), although if possible I would like to capture ALL the textures for both static & non static B3D models.


Yasha(Posted 2013) [#4]
Ignoring for the moment the matter of anim meshes, your original code is largely fine for iterating over the textures of a static mesh (it doesn't handle multitextures - you need to iterate over the index parameter to GetBrushTexture if surfaces have multiple textures attached, but that's secondary too).

Your main issue is simply in how the texture names are returned. You're overwriting the name string with each new texture that is discovered so that the function will only ever return the last one found. Therefore, you need to think of a way to return a collection of strings, rather than a single string value.

There are several ways to do this:

-- most obviously, combine the texture names into a single multi-string. There are in turn varying levels of sophistication for this; something as simple as using + with a separator space would work for most purposes.

-- you could put the strings into an array. This requires either setting an upper limit on their number, or to do a pre-pass where you just count the strings and then resize an array appropriately, before putting them into it in a second pass.

-- you could use one of the string-based array types in the archives; these have no size constraint and can also hold substrings without problems (in a regular multi-string, what would happen if the texture name included a space? It would corrupt the listing! These fix that). They are however very, very slow.

-- you could use a linked list of type objects holding string values.

You'd also need to decide how to deal with the fact that texture names may not be unique, and may also be the empty string if the texture was created rather than loaded. You might want to remove duplicates, and skip over or mark null entries, depending on what yo want the list for.


Guy Fawkes(Posted 2013) [#5]
O, I just want the list so I can export the correct textures from the correct locations to an export folder Blitz creates automatically.


Guy Fawkes(Posted 2013) [#6]
Does anyone know how to do this?


RemiD(Posted 2013) [#7]

you could put the strings into an array



Example : (assuming your code works to retrieve one texture name)




Guy Fawkes(Posted 2013) [#8]
Hi RemiD. How do I return this in the ReadMeshTextures() function?


RemiD(Posted 2013) [#9]
A better question would be : why do you absolutely want to use return to retrieve and store several textures names...


John Blackledge(Posted 2013) [#10]
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=1626 ; Model's Texture names

Any use?