Thank you very much, right now i am quite happy how it works. only thing is the dialog box opens for each shader because its in the "changeshader" function wich repeats.
I d' like to have one gui-call at the start in the main function, but if i do that theni cant transport the "prefix" argument to the changeTexture(shader) function.
about the time investement i agree, but for this script was more an out of desperation approach in how cinema4d lacks in organizing textures, then a new grind in passion. the script helps me with a hack, to organize them on OS side, for team projects like for instance unity.
Best
Moko
import c4d
from c4d import gui
import os
#GUI
GROUP = 1000
TEXT = 1001
PREFIX = 1002
BTN_OK = 1010
BTN_CANCEL = 1020
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) :
dialog = userDialog()
dialog.Open(c4d.DLG_TYPE_MODAL, defaultw=200, defaulth=50)
prefix = dialog.TexturPrefix
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()
doc.StartUndo()
# Iterate over selected material
for mat in mats:
recurse_hierarchy(mat.GetFirstShader())
doc.EndUndo()
# Execute main()
if __name__=='__main__':
main()