Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login
    1. Maxon Developers Forum
    2. moko
    3. Best
    M
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 10
    • Best 2
    • Controversial 0
    • Groups 0

    Best posts made by moko

    • RE: Texture renaming

      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()
      
      posted in Cinema 4D SDK
      M
      moko
    • RE: Texture renaming

      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()
      
      posted in Cinema 4D SDK
      M
      moko