Now it works as intended. You can select the materials for which you want to change the prefix of the texturename. The actual name of the texture is not changed. You need to do this with a renaming-tool at the OS level.
thanks to everyone
import c4d
from c4d import gui
import os
#GUI
GROUP = 1000
TEXT = 1001
PREFIX = 1002
BTN_OK = 1010
BTN_CANCEL = 1020
prefix = "placeholder"
class userDialog(gui.GeDialog):
def CreateLayout(self):
self.SetTitle("Add Prefix")
self.GroupBegin(GROUP, c4d.BFH_SCALEFIT, 3,5, title = "Adds a prefix to the texturename")
self.GroupBorder(c4d.BORDER_GROUP_IN)
self.GroupBorderSpace(20, 5, 20 , 5)
self.AddStaticText(TEXT, c4d.BFH_SCALEFIT, name="Prefix")
self.AddEditText(PREFIX, c4d.BFH_SCALEFIT)
self.AddButton(BTN_OK, c4d.BFH_SCALE, name="Ok")
self.AddButton(BTN_CANCEL, c4d.BFH_SCALE, name="Cancel")
self.GroupEnd()
return True
def InitValues(self):
#initiate the gadgets with values
self.SetString(PREFIX, "Tex_")
return True
def Command(self, id, msg):
#handle user input
if id==BTN_CANCEL:
self.Close()
elif id==BTN_OK:
self.TexturPrefix = self.GetString(PREFIX)
self.Close()
return True
# Adds a prefix to the texturename
def changeTexture(shader) :
print (prefix)
# shader ID
texturePath = shader[c4d.BITMAPSHADER_FILENAME]
# split aboslute path
oldTexturename = os.path.split(texturePath)
# add prefix
newTexturename = prefix + oldTexturename[1]
doc = shader.GetDocument()
doc.AddUndo(c4d.UNDOTYPE_CHANGE, shader)
# Assign the new value
shader[c4d.BITMAPSHADER_FILENAME] = os.path.join(oldTexturename[0], newTexturename)
shader.Message(c4d.MSG_UPDATE)
# Iterate a hierarchy
def recurse_hierarchy(shader) :
while shader:
# Test if the current node is a bitmap shader.
if shader.CheckType(c4d.Xbitmap) :
changeTexture(shader)
recurse_hierarchy(shader.GetDown())
shader = shader.GetNext()
# Main function
def main() :
mats = doc.GetActiveMaterials()
dialog = userDialog()
dialog.Open(c4d.DLG_TYPE_MODAL, defaultw=200, defaulth=50)
global prefix
prefix = dialog.TexturPrefix
doc.StartUndo()
# Iterate over selected material
for mat in mats:
recurse_hierarchy(mat.GetFirstShader())
doc.EndUndo()
# Execute main()
if __name__=='__main__':
main()