Listing textures from selected objects

3,810 views
Skip to first unread message

martin...@gmail.com

unread,
Mar 18, 2011, 2:50:02 AM3/18/11
to python_inside_maya
Does anyone know the name of the command or the way that I can get the
full path of the textures from a selected object?

Thanks,
-m

Alexander Mirgorodsky

unread,
Mar 18, 2011, 8:00:36 AM3/18/11
to python_in...@googlegroups.com
Hi Martin!
Do You  mean by word "object"
If this is geometry object that it does not store this information.
You can find it by going througр the chain "geometryShape -> Shading Engine -> Shader(Surface, Displace, Volumetric etc) ->Color(Float)Attribute->TextureNode->FileName").
Can You explain the details of your problem?

Alex Mirgorodsky.
Shading TD/ Lighting TD
Renovation Entertainment.


paradigm3d

unread,
Mar 18, 2011, 5:14:37 AM3/18/11
to python_inside_maya
I have this mel script that do that thing, just convert this to python
code, I am just lazy now to convert it... :D sorry :D

proc string strip( string $object )
{

string $buffer[];
tokenize $object "." $buffer;
return $buffer[0];
}

global proc getFileAttribute()

{

//know what is selected.
string $obj[] = `ls -l -sl`;

//find the size of selection.
int $objSize = size($obj);
//if it is more than one...
if ($objSize > 1)
{
confirmDialog -t "Object Select"
-message "Please select only one object."
-messageAlign "center"
-button "OK"
-defaultButton "OK";
return;

}

//if it is less than one...
else if ($objSize < 1)
{
confirmDialog -t "Object Select"
-message "Please select one object."
-messageAlign "center"
-button "OK"
-defaultButton "OK";
return;

}

//select the shapeNode.
string $objShape[] = `pickWalk -direction "down" $obj`;

//know what shading engine is connected.
string $connection[] = `listConnections -d true -source false
-p false -type "shadingEngine" $objShape`;
print $connection;


//strip the ".outColor" from the result of the material
string $sgList[] = `listSets -type 1 -ets -o $objShape`;
string $materialList = strip( `connectionInfo -sfd ($sgList[0] +
".surfaceShader")`);
print $materialList;


string $texture = "";

string $class[] = getClassification( `nodeType $materialList` );

if ( "shader/surface" == $class[0] && `connectionInfo -id
( $material + ".color" )` )
{

$texture = strip( `connectionInfo -sfd ( $material +
".color" )` );

}

return $texture;



}



On Mar 18, 2:50 pm, "martinmrom...@gmail.com"

martin...@gmail.com

unread,
Mar 18, 2011, 11:57:58 AM3/18/11
to python_inside_maya
Hi
Alexander,

Basically what I am trying to do is to get a list of all of the
textures in my scene.
From time to time I see people working on scenes where they are
pulling textures from different locations. I want to make a tool that
will gather the location of every texture in my scene.

Here is a little bit of my sudo code.

def Textures( listOfTexturesFilePaths=[], destinationDir:
'''
Parameter Example:
listOfTexturesFilePaths = [ 'c:/temp/water.tga', 'c:/temp/
textures/ground.tga', 'c:/temp/textures/stuff.tga' ]
destinationDir = 'c:/newLocation'
'''

# look at import os

pass

# Do the copy of the maya file and patch it to the new textures

Hope this makes a lot of sense and thanks for your help.

Martin

On Mar 18, 5:00 am, Alexander Mirgorodsky <amir...@gmail.com> wrote:
> Hi Martin!
> Do You  mean by word "object"
> If this is geometry object that it does not store this information.
> You can find it by going througр the chain "geometryShape -> Shading Engine
> -> Shader(Surface, Displace, Volumetric etc)
> ->Color(Float)Attribute->TextureNode->FileName").
> Can You explain the details of your problem?
>
> Alex Mirgorodsky.
> Shading TD/ Lighting TD
> Renovation Entertainment.
>
> On Fri, Mar 18, 2011 at 9:50 AM, martinmrom...@gmail.com <

martin...@gmail.com

unread,
Mar 18, 2011, 12:03:47 PM3/18/11
to python_inside_maya
Hi there paradigm3d,

No worries, that's why I am in the process of creating the tool :),
thanks for sharing. Listing the textures is only a part of the tool
that I am working on. Thanks

-M

Amorano

unread,
Mar 19, 2011, 4:53:56 PM3/19/11
to python_inside_maya
import pymel.core as pm
textureFileNodes = pm.ls(typ='file')
for fileNode in textureFileNodes:
print fileNode.getAttr('fileTextureName')

pymel of course, but easy enough in straight maya.cmds as well.

Cheers.

On Mar 17, 11:50 pm, "martinmrom...@gmail.com"

Alexander Mirgorodsky

unread,
Mar 20, 2011, 10:06:10 AM3/20/11
to python_in...@googlegroups.com
Hi Martin! I think is what I understood You.
Below I give snipets from my render-toolkit code.
It only extracts because this toolkit complikated and renderman-aimed.
But I hope it will come across you on the right ideas. 
So if we really want geting info from selected obj then:

1. Getting list shading engines:
 def getsListShadingEngines(currentObj):
sgConections = mc.listConnections(currentObj,t="shadingEngine", d=1)
if sgConections != None:
##remove duplicates from list
inicSgConnections =[ u for u in sgConections if u not in locals()['_[1]'] ]
return inicSgConnections

2. Finding shaders connected to shading engine:

def rpExportMetaDataSG(currentObj, currentSG, time):
mayaShadersList = []
co_shader_strings = "" # Its may co-shader special variable, in your case its may be ane variable 
shaderStringContent = "
if mc.connectionInfo( (currentSG+".surfaceShader"), isDestination=True) == 1:
mayaShadersList = mc.listConnections((currentSG+".surfaceShader"))
##!!!!!Check -If Not Connect
currentMayaShader = mayaShadersList[0]
shaderType = mc.nodeType(currentMayaShader)
#print currentMayaShader

# Ok Martin - we know maya shader BUT if this "layer shader" or other specific shaders like "useBackground"

if shaderType == "layeredShader":
layerShadersConnection = mc.listConnections(currentMayaShader,t="lambert")
layerShadersConnection += mc.listConnections(currentMayaShader,t="blinn")
.....................................
unicLayerShadersConnection = [ u for u in layerShadersConnection if u not in locals()['_[1]'] ]
countLayers = len(unicLayerShadersConnection)-1
while countLayers >=0:
     co_shader_strings = #// MARTIN,  NEXT YOUR CODE FOR working with unicLayerShadersConnection[countLayers]
     .........
countLayers -= 1

elif shaderType == "useBackground":
shaderStringContent = # In this case Your procedure for getting connected slots in useBackgound shaders

##elif shaderType == ##"Other specific"###:

else:
shaderStringContent = ### Your procedure  for finding connected slots in current shader

return shaderStringContent #My procedure return string - but your code can return list of texture(file) nodes  and file places on storage.

3. Example for working with finded(in consruction above) surface shader

def mMainShaderAttrs(currentMayaShader,time):
surfaceColorContent = ''
if mc.connectionInfo((currentMayaShader+'.color'), isDestination=1):
listColorTextures = mc.listConnections(currentMayaShader+'.color')
if str(mc.nodeType(listColorTextures[0])) == 'file':
mayaColorTextureFile = mc.getAttr(listColorTextures[0]+'.fileTextureName', time=time)
.................................

PS. I use time attribute as I work with animated shader attributes. You can remove "time" - flag

Alex Mirgorodsky.
Shading TD/ Lighting TD
Renovatio Entertainment.




PyInUrEye

unread,
Mar 20, 2011, 3:40:20 PM3/20/11
to python_inside_maya
This is cool. Thanks for sharing. I have a tool that does it in mel
and I've been meaning to convert it to python for a while now. I
shall try this soon and post anything worth while. Nice work.
> On Fri, Mar 18, 2011 at 6:57 PM, martinmrom...@gmail.com <

Martin La Land Romero

unread,
Mar 22, 2011, 12:59:07 PM3/22/11
to python_in...@googlegroups.com, Alexander Mirgorodsky

Thanks so much Alexander and Amorano, I really appreciate you guys taking the time to answer my question. 

I am almost done with the tool. The  functionally of the tool is not only to list  all the textures in your scene but also to  export the current maya file as well as all the textures being used in that particular scene to a user designated  location. 

Here is part of the tool, still work in progress.


import maya.cmds as cmds
import os
import shutil


def copyToNewLocation():
   
"""
    This function queries the location of the opened file as well as the path of all the textures.
    It will then place the textures and the the scene file on the expecified path by the user.
    """

    newLocation
='C:\Users\FFION2\Documents\TestTextureProject\NewLoc' #hardcoded > enter your target directory.
   
#multipleFilters = "Maya Files (*.ma *.mb);;Maya ASCII (*.ma);;Maya Binary (*.mb);;All Files (*.*)"
   
#newLocation = cmds.fileDialog2(fileFilter=multipleFilters, dialogStyle=2)  #

    mySceneAssets
= cmds.file( q=True, l=True, loc=True )

#It goes trough each texture and the maya file and place them in the new location.
   
for i in mySceneAssets:
       
print(i)
        shutil
.copy(i,newLocation )

#Call the function.
copyToNewLocation
()


Thanks again for your help guys, I am really enjoying scripting in Pytho




Reply all
Reply to author
Forward
0 new messages