Skip to content

Instantly share code, notes, and snippets.

@fredrikaverpil
Last active February 1, 2022 14:25
Show Gist options
  • Save fredrikaverpil/9970710 to your computer and use it in GitHub Desktop.
Save fredrikaverpil/9970710 to your computer and use it in GitHub Desktop.
Export and re-assign shaders, nodes and node data #maya
import maya.cmds as cmds
def getAttributes(object):
attributes = cmds.listAttr(object)
for attribute in attributes:
try:
print attribute + ' = ' + str(cmds.getAttr(object + '.' + attribute))
except:
pass
def getConnections( selected ):
connections = cmds.listConnections(selected, c=True)
for connection in connections:
print connection
for selected in cmds.ls(sl=True):
# Store attributes from (selected) transform node in JSON
transformAttrs = getAttributes( selected )
# To do: store transformAttrs in JSON
# Store attributes from shape node in JSON
relatives = cmds.listRelatives( selected )
for relative in relatives:
shapeAttrs = getAttributes( relative )
# To do: store shapeAttrs in JSON
# Store connections in JSON
connections = getConnections( selected )
# To do: export connected nodes
import maya.cmds as cmds
def getShaders( obj ):
cmds.select( obj )
cmds.hyperShade( shaderNetworksSelectMaterialNodes=True )
return cmds.ls(sl=True) # Returns all shaders associated with the object (shape, face etc)
def getShadersPerFace( shape ):
perFaceShaders= {}
for f in range( cmds.polyEvaluate(shape, f=True ) ):
face = shape+'.f[' + str(f)+ ']'
try:
shader = getShader(face)
perFaceShaders[face] = shader
except:
print 'Error: could not fetch shader for ' + face
return perFaceShaders
def recordShaderAssignment( shape, shaders ):
print type(shaders)
if str(type(shaders)) == '<type \'list\'>':
print 'To do: Store assignment of shader per shape'
if str(type(shaders)) == '<type \'dict'>':
print 'To do: Store assignment of shaders per face'
# ----------------------------------------------------------------------------------
# Get selection
selection = cmds.ls(sl=1)
for shape in selection:
# Get shaders for shape
shaders = getShaders( shape )
if len(shaders) == 1:
# Just one shader applied to shape
recordShaderAssignment( shape, shaders )
else:
# More than one shader assigned to object (per face shader assignment detected)
shadersPerFace = getShadersPerFace(shape)
recordShaderAssignment( shape, shadersPerFace )
# Re-set to original selection
cmds.select(selection)
import maya.cmds as cmds
# get shapes of selection:
shapesInSel = cmds.ls(dag=1,o=1,s=1,sl=1)
# get shading groups from shapes:
shadingGrps = cmds.listConnections(shapesInSel,type='shadingEngine')
# remove duplicate shading groups from list
shadingGrps = list(set(shadingGrps))
# get the shaders:
shaders = cmds.ls(cmds.listConnections(shadingGrps),materials=1)
# remove duplicate shaders from list
shaders = list(set(shaders))
# to do: export shading shaders/shadingGroups to disk
@dennisserras-acme
Copy link

Did you ever get this exporting?

@MrManly
Copy link

MrManly commented Aug 3, 2017

Do it! Do it! thumbs up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment