Is it possible to make an iteration through a selection of Materials.
I'd like to rename the texture after the material. It works but only on all materials in the doc. how do i specify it on to a selection of materials?
import c4d
from c4d import gui
from c4d import storage
import os
#Welcome to the world of Python
def changeTexture(shader):
# shader ID
texturePath = shader[c4d.BITMAPSHADER_FILENAME]
# split aboslute path
oldTexturename = os.path.split(texturePath)
fileExtension = os.path.splitext(texturePath)
# add prefix to
newTexturename = matName + fileExtension[1]
print (newTexturename)
newTexturePath = os.path.join(oldTexturename[0], newTexturename)
print (newTexturePath)
# rename texture
try :
os.rename(texturePath, newTexturePath)
print("Source path renamed to destination path successfully.")
except OSError as error:
print(error)
# Assign the new value
shader[c4d.BITMAPSHADER_FILENAME] = newTexturePath
shader.Message(c4d.MSG_UPDATE)
# Main function
def main() :
c4d.CallCommand(1029486) # Project Asset Inspector...
c4d.CallCommand(1029813) # Select All
c4d.CallCommand(1029820) # Globalize Filenames
# Iterate over selected material
mat = doc.GetFirstMaterial()
while mat:
shader = mat.GetFirstShader()
global matName
matName = mat.GetName()
while shader:
# Test if the current node is a bitmap shader.
if shader.CheckType(c4d.Xbitmap) :
changeTexture(shader)
shader (shader.GetDown())
shader = shader.GetNext()
mat = mat.GetNext()
# c4d.CallCommand(200000273) # Reload All Textures
# Execute main()
if __name__=='__main__':
main()