Jump to content

Sign in to follow this  
Michael Müller

Rename dozens of layers using a CSV file

Recommended Posts

Hello there, 

 

I'm pretty new to c4d and python scripting so I don't even know if what I want to do is possible in theory, though it sounds somewhat plausible.

I have an excel list with 300 names and need all of them as MoText Elements. As each MoText is combined with several other Elements, which are all under one Null, I thought about renaming the Nulls and using their name as a driver for the MoText-text.

 

Hierarchy of my c4d file:

Null.1

->MoText (child of Null.1)

->Fancy-Element (child of Null.1)

Null.2

->MoText (child of Null.2)

->Fancy-Element (child of Null.2)

and so on..

 

Example of my .csv file:

Dennis,John,Markus...

 

I want to use ".1(2,3 etc.)" as an indicator of which name to grab from the .csv file. Maybe there is a way to import the .csv as a string and be able to split it with the comma as a separator, so you can get access to each name individually and globally replace all Null-names.

Null.1 -> Dennis

Null.2 -> John

Null.3 -> Markus

and so on

 

is this possible and how complicated would it be to write a script for that?

 

Thanks in advance!

 

Michael 

Share this post


Link to post
Share on other sites

Sure, this is possible and shouldn't be complicated.

There's really only this one line in the CSV?

And all you want is the Nulls renamed?

Wouldn't it be more efficient, to build the entire hierarchy based on the CSV?

Also it's always helpful to provide a scene for testing, when asking for scripts or alike. This can remove a lot of ambiguity and also spares the developer the need to setup her/his own scene (which the may already be a wrong basis and misleading).

Anyway, I'll be back...

Cheers

Share this post


Link to post
Share on other sites

This would be a pretty cool script and could have some nice potential features. For jumpstarting ideas and getting your scene sort of pre-organized. I would definitely be interested in something like this too.

Share this post


Link to post
Share on other sites

Well, then go ahead and let your ideas run free. The more detailed and accurate you describe your needs, the faster I can try to get you something.

Share this post


Link to post
Share on other sites

So, here you go as a starter.

 

The CSV file I used for testing:
test.csv

 

My test scene:
test_rename_object_from_csv.c4d

 

The script for Script Manager:

rename_objects_from_csv.py

 

And for discussion, the script itself:

# Rename Null objects based on information from a CSV file
import c4d

SEPARATOR = ','
NAME_INDEX_SEPARATOR = '.'

def main():
    filename = c4d.storage.LoadDialog(c4d.FILESELECTTYPE_ANYTHING, doc.GetDocumentPath(), c4d.FILESELECT_LOAD)
    if filename is None:
        return

    # Get names as array from CSV file
    fileCsv = None
    names = []
    try:
        # Read CSV file
        fileCsv = open(filename.decode("utf-8"), mode='r')
        # TODO Currently only reading first line, only (based on forum post)
        names = fileCsv.readline().split(SEPARATOR)
    finally:
        if fileCsv is not None:
            fileCsv.close()

    # Rename Null objects
    doc.StartUndo()
    obj = doc.GetFirstObject() # TODO only searching top-level of object hierarchy for Nulls to rename
    while obj is not None:
        if obj.CheckType(c4d.Onull):
            nameParts = obj.GetName().rsplit(NAME_INDEX_SEPARATOR)
            if len(nameParts) > 1 and nameParts[1].isdigit() and nameParts[0] == 'Null' and \
               int(nameParts[1]) >= 0 and int(nameParts[1]) < len(names):
                doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
                obj.SetName(names[int(nameParts[1])])
        obj = obj.GetNext()
    doc.EndUndo()
    c4d.EventAdd()

if __name__=='__main__':
    main()

Cheers

 

Edited by MighT
removed debug print

Share this post


Link to post
Share on other sites

@MighT

 

Great script - thanks for the effort!


Free C4D trainings from MAXON professional on our youtube channel

 

https://www.youtube.com/c4dcafe

Share this post


Link to post
Share on other sites
  • Topic Author
  •  

    @MighT Thank you so much for taking the time to write this script! It is exactly what I needed. Worked like a charm. How did you get into coding for C4D? I learned a little bit of python on codecademy but there is so many things here exclusive to C4D. I'd love to be able to write things like this on my own (Or at least be able to extend it to the case where there is a first Null without a digit, which I've tried without success.). Do you have any advice for me? 

    Thanks again!

    Michael

    Share this post


    Link to post
    Share on other sites

    You are welcome, glad it helped.

     

    Well, C4D has always been a hobby of mine since its Amiga times. Really just a hobby. I was fascinated by computer generated imagery and simulated physics. Yet, have never been able to produce anything anybody with a clear conscious would call art, because... probably because my brain doesn't work this way. I studied electrical engineering and worked as a system design engineer for almost 15 years (mainly C and VHDL programming). Around that time I decided to shift my focus onto something I like better and to try to hire at MAXON. Therefore I thought, it wouldn't hurt to have developed a plugin before. I took my year's vacation, MAXON's SDK documentation and the SDK examples and worked myself into it. Not that it helped with getting hired by MAXON (it took three more years to get into there), but for a reasonably experienced developer it was certainly possible to learn enough to get three plugins working (of which I released only two to the public and their functionality got redundant with MAXON's next major releases...). Only after I got hired by MAXON and worked inside the SDK Team I then also started familiarizing with the Python side.
    I doubt, you wanted to hear my life's story, but at least that's how I got into developing plugins. And you asked how I got into plugin development...

     

    Please, don't get me wrong, I certainly do not want to say, you need to study programming to get into it. Far from it. Actually I think it's more important to understand certain principles of C4D. Then, if you are able to program in Python, the SDK documentation and examples should get you going quite a bit. And finally, I think MAXON is quite generous in this regard, there's MAXON's SDK Team (and I'm not mentioning this, because I was in there) who provide free support to everybody in the Plugin Cafe forum. These guys usually get your problems solved quite quickly and nicely.

     

    Nowadays there are also other nice resources. Did you see @Cairyn's  spoonfed series? Recommended. The links somewhere here in the forum. If you are planning to reach an age of 150, you may also wait for me to release my long planned YT tutorials on C4D Python development. Unfortunately there's too much in the pipe to get to these. 

     

    Last note: I do also teach C++ and/or Python plugin programming for C4D to interested people (and also to not interested people who are forced by their boss to do it nevertheless). Maybe you are interested. While I usually do not do this for free, it is on a pay-what-you-want,-are-able-to-afford-and-deem-worthy basis. No strings attached. PM me, if you are interested. If enough people in here are interested, we can also do a Q&A chat session.

     

    Share this post


    Link to post
    Share on other sites

    Ah, I missed your question on the first Null without digit. Actually that's why it's always good to post a scene 😉

    How are the Nulls to be renamed identified? All Nulls on the topmost hierarchy level? Or just Nulls which begin with Null?

    I assumed it to be potentially dangerous, if the script simply renamed all Nulls, so I deliberately made it so that it only renames those named like this "Null.n" (with n a number).

    How is the logic then? If there is only "Null" it will be the name with index zero?

     

    Share this post


    Link to post
    Share on other sites

    Here, with this change it will rename all Null objects which name starts with "Null". If there's a numeric suffix and the index can be found in the name list, the respective name is used, in all other cases the first name (index zero) is used.

     

    Here, it's only the renaming section. Simply replace the block with this one:

        # Rename Null objects
        doc.StartUndo()
        obj = doc.GetFirstObject() # TODO only searching top-level of object hierarchy for Nulls to rename
        while obj is not None:
            if obj.CheckType(c4d.Onull):
                nameParts = obj.GetName().rsplit(NAME_INDEX_SEPARATOR)
                if nameParts[0] == 'Null':
                    doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
                    if len(nameParts) > 1 and nameParts[1].isdigit() and \
                        int(nameParts[1]) >= 0 and int(nameParts[1]) < len(names):
                        obj.SetName(names[int(nameParts[1])])
                    else:
                        obj.SetName(names[0])
            obj = obj.GetNext()
        doc.EndUndo()
        c4d.EventAdd()

     

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • Sir, I can't thank you enough for taking the time to write all this. Not only for completing the code and creating a very useful script for all of us, but also for explaining to me how you got into coding and what's the best approach for someone new to the game like me. In fact I kind of was interested in your lifestory, so thanks for sharing it! Feels good to get help from someone working at MAXON, they seem like a very user-connected company. As for my Part, I am probably going to take a look at Cairyn's Spoonfed Series as a first step and also just play around with the examples in the SDK documentation. If I feel like there is a chance for me to seriously get into python more and my mograph carreer allows me to do so, you'll be the first one I'll get in touch with. You definitely seem like you know what you're doing and I'd be happy to pay for your services once I am more committed.

     

    Thank you so much again for your time

     

    Michael

    Share this post


    Link to post
    Share on other sites

    Oh, sorry, I'm no longer at MAXON. I somehow missed that part.

    Share this post


    Link to post
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    Sign in to follow this  

    • Recently Browsing   0 members

      No registered users viewing this page.

    ×
    ×
    • Create New...