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

    Rename object by filename

    Cinema 4D SDK
    python
    3
    10
    1.8k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      Jasper Sek
      last edited by Manuel

      Hi there! Do someone know a way to rename a nullobject in c4d by the filename, so that the name of the null-object changes, if the c4d-file changes? I mean something like the token-system at the renderpath.
      I never used python before, just a bit of xpresso. That would save me and maybe others a lot of time.

      1 Reply Last reply Reply Quote 0
      • lasselauchL
        lasselauch
        last edited by

        You just need to add a python tag to your object and paste the following code.

        Simple as that:

        import c4d
        
        def main():
            if not doc:
                return
            doc_name = doc.GetDocumentName()
        
            null = op.GetMain()
            if not null:
                return
        
            null.SetName(doc_name)
        

        Download here: null-docname.c4d

        Hope it helps.

        Cheers,
        Lasse

        J 1 Reply Last reply Reply Quote 3
        • J
          Jasper Sek
          last edited by

          Wow, Thanks a lot!

          1 Reply Last reply Reply Quote 0
          • J
            Jasper Sek @lasselauch
            last edited by

            @lasselauch What if i just want the name without .c4d?

            1 Reply Last reply Reply Quote 0
            • lasselauchL
              lasselauch
              last edited by

              http://letmegooglethat.com/?q=split+extension+from+filename+python

              😁

              J 1 Reply Last reply Reply Quote 0
              • J
                Jasper Sek @lasselauch
                last edited by

                @lasselauch sorry i'm too bad at this. cant fix that. i found this before, but i don't know how to implement it in c4d.

                1 Reply Last reply Reply Quote 0
                • lasselauchL
                  lasselauch
                  last edited by

                  Of course there are a lot of ways to spin this, but I guess in most cases this should be enough:

                  # import os
                  import c4d
                  
                  def main():
                      if not doc:
                          return
                      docname = doc.GetDocumentName()
                  
                      # docname, _ = os.path.splitext(docname)
                      docname = docname.replace('.c4d', '')
                      
                      null = op.GetMain()
                      if not null:
                          return
                  
                      null.SetName(docname)
                  

                  Download: null-docname.c4d

                  Cheers,
                  Lasse

                  1 Reply Last reply Reply Quote 0
                  • ferdinandF
                    ferdinand
                    last edited by ferdinand

                    Hi @Jasper-Sek,

                    thank you for reaching out to us. And thank you @lasselauch for jumping in. The solutions provided by @lasselauch are correct, but one might want to consider that Cinema is also capable of opening non-c4d files (I know, it's crazy ;)). So you should use os.path.splitext(docname).

                    Cheers,
                    Ferdinand

                    PS: @lasselauch I know that you meant this in a light-hearted manner, but "let-me-google-that-for-you" can be misconstrued. People have different information needs and things that seem trivial or obvious to you, are not for them. Programming can be a confusing topic and we would like this to be a welcoming place for everyone and subsequently stay away from all "RTFM"-notions.

                    MAXON SDK Specialist
                    developers.maxon.net

                    lasselauchL 1 Reply Last reply Reply Quote 1
                    • lasselauchL
                      lasselauch @ferdinand
                      last edited by

                      @zipit said in Rename object by filename:

                      PS: @lasselauch I know that you meant this in a light-hearted manner, but "let-me-google-that-for-you" can be misconstrued. People have different information needs and things that seem trivial or obvious to you, are not for them. Programming can be a confusing topic and we would like this to be a welcoming place for everyone and subsequently stay away from all "RTFM"-notions.

                      Yep, definitely meant in a light-hearted manner!!! 🙂
                      You're totally right.. that's why I provided the correct answer.

                      Cheers,
                      Lasse

                      1 Reply Last reply Reply Quote 0
                      • J
                        Jasper Sek
                        last edited by

                        @zipit @lasselauch thank you both for your time and knowledge!

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post