get node type in Python?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 01 January 2014   #1
Post get node type in Python?

I'm trying to get this to work in a Python version:

string $mySelection[] = `ls -sl`;
string $mySelectionShapes[] = `listRelatives -s $mySelection`;
string $mySelectionShapes[] = `listRelatives -s $mySelection[0]`;
string $sGroupTemp[] = `listSets -type 1 -o $mySelectionShapes[0]`;
string $tempy[] = `listConnections ($sGroupTemp[0] + ".surfaceShader")`;
string $typeTest[] = `ls -showType $tempy[0]`;   
if ($typeTest[1] != "surfaceShader")
	{
        yadda
        }
else if ($typeTest[1] == "surfaceShader")
	{
        yadda
        }


unfortunately, the direct conversion to Python doesn't yield the same result. In MEL, it correctly gives me "VRayMtl" (the type) but this Python code doesn't tell me the type, it just tells me the name and Tdata:

mySelection = cmds.ls( sl=True )
mySelectionShapes = cmds.listRelatives( mySelection[0], s=True )
global storedMaterialmari
sGroupTemp = cmds.listSets(type=1, o=str(mySelectionShapes[0]))
tempy = cmds.listConnections(sGroupTemp[0] + ".surfaceShader")

typeTest = cmds.ls(sGroupTemp[0] + ".surfaceShader", showType=True )
global surfshade
print typeTest


output:
[u'VRayMtl1SG.surfaceShader', u'Tdata']

I need to get the actual type to use for my conditional statement
 
Old 01 January 2014   #2
cmds.nodeType()
 
Old 01 January 2014   #3
that just returns "shadingEngine" for two different material types.

edit: got it. Thanks


import maya.cmds as cmds
textureName = "file1"

mySelection = cmds.ls( sl=True )
mySelectionShapes = cmds.listRelatives( mySelection[0], s=True )
global storedMaterialmari
sGroupTemp = cmds.listSets(type=1, o=str(mySelectionShapes[0]))
tempy = cmds.listConnections(sGroupTemp[0] + ".surfaceShader")
##typeTest = cmds.ls(sGroupTemp[0] + ".surfaceShader", showType=True )
##typeTest = mel.eval('ls -showType $tempy[0]')
typeTest = cmds.nodeType(tempy[0])
print typeTest
global surfshade
if typeTest != "surfaceShader":
	storedMaterialmari[0] = tempy[0]
	textureFileNode = textureName
	surfshade = cmds.shadingNode('surfaceShader', asShader=1)
	cmds.select( mySelection )
	cmds.hyperShade( assign=surfshade)
	cmds.connectAttr(textureFileNode + '.outColor', surfshade + '.outColor', f=1 )
elif typeTest == "surfaceShader":##restore the global variable mat
	cmds.select(mySelection )
	cmds.hyperShade( assign=storedMaterialmari[0])
	cmds.delete(surfshade)

Last edited by cgbeige : 01 January 2014 at 02:07 AM.
 
Old 01 January 2014   #4
Do I see it right that you try to find out the shaders which are connected to the selected objects? This can be done quite easy with the usage of pymel.

import pymel.core as pm
 selectedShapes = pm.listRelatives(pm.ls(sl=True), ad=True, type=["mesh", "nurbsSurface"])
 for shape in selectedShapes:
 	shadingEngines = shadingGroup = shape.outputs(type="shadingEngine")
 	for shadingEngine in shadingEngines:
 		surfaceShader = shadingEngine.surfaceShader.input()
 		if surfaceShader.type() == "lambert":
 			doSomeLambertThing
 		if surfaceShader.type() == "surfaceShader":
  			doSomeSurfaceShaderThing
  
 
__________________
www.renderwiki.com - www.openmaya.net
 
Old 01 January 2014   #5
ok - thanks.
 
Old 01 January 2014   #6
also in pymel, you could use the python builtin isinstance() command to check this as well.

say i wanted to check of all materials in a list were lambert i could do something like.

 if all(isinstance(i, pm.nt.Lambert) for i in mats):
    do stuff here!!!
 


also since it is class based in pymel, you can also check what features a shader has, by checking for attributes and methods useing the hasAttr() built. This can be usefull if your working with serveral material types, and dont want to check them all.
__________________
Twitter - GitHub
------------------
My Scripts - Maya tools and workflow scripts
psLink - Maya PhotoShop intergration.
 
Old 01 January 2014   #7
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 09:11 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.