import maya.cmds as cmds
import pymel.core as pm

####		File textures display	-	ANIM ONLY		#####
# Display udim textures loaded via file node on selected shapes without going to the actual file node & generating preview by clicking
def main():
	shape = getNodes()
	analyzeShader(shape)
	


def getNodes():
	node = cmds.ls(selection = True)
	
	shape = cmds.listRelatives(allDescendents=True, type='shape')

	return(shape)


def analyzeShader(shape):
	
	current = 0
	for i in shape:
		if str(shape[current]).endswith("Shape") == True:
			# Check for a file texture in the shader
			shader = cmds.listConnections(shape[current], type='shadingEngine')
			
			print(shader)
			
			usedTextureNodes = (pm.listHistory(shader, type="file"))
			#check if list is empty

			if not usedTextureNodes:
				pass
			else:
				print(usedTextureNodes)

				try :
					mel.eval('generateUvTilePreview ' + usedTextureNodes[current])
				except IndexError:
					pass
		current += 1
	
	# select the actual shape
	cmds.ls(shape)
	children_nodes = cmds.listRelatives(allDescendents=True, type='shadingEngine')




if __name__ == "__main__":
	main()
