PDA

View Full Version : Python rename script


mayajunky
10-28-2010, 09:09 PM
Hey all, have had a bit more time to work with python and thought I'd share my simple rename script for others to use/learn from. Props to everyone else posting examples up, as it really helps everyone learn more about scripting in C4D. I learned a lot from the Split Joints scripts, so thanks man! Of course I still don't understand the If __name__=='__main__': line yet. :P Please feel free to rip this script apart and make it much.. much cooler. :P I do also have a variation of the script which just grabs the parents name as the base name if anyones interested.


import c4d
from c4d import documents, gui
#import the documents library to use it's available functions in the script
#import the gui library to use message dialogs

#main function declaration
def main():
#get the active document before attempting to get selected objects
doc = documents.GetActiveDocument()
#get current object selected in current active doc
selectedObjects = doc.GetSelection()

if len(selectedObjects) != 1:
gui.MessageDialog("Please select 1 parent.")

else:
#define the label name for the rename dialog, which is the first parameter in InputDialog()
guiLabel = "Children Prefix Name"
#define the default prefix name of the children if the user closes gui without adding a string value
#the script will use this name, also appears highlighted in the dialog itself, second parameter of InputDialog()
nameDefault = "Child"
#get the name of the object to be copied to children names through a dialog pop-up
replaceName = gui.InputDialog(guiLabel,nameDefault)

#create a list to hold reference to the parents children
childrenList = selectedObjects[0].GetChildren()

#store the total count of children
childCount = len(childrenList)

#initialize the while loop counter
currentCount = 0

#loops while under count since is currentCount is initialized at zero
#would loop while <= if currentCount was initialized at 1
while currentCount < childCount:
#set the name of the children in the list based on the current loop count
childrenList[currentCount].SetName(replaceName + "_" + str(currentCount))
#add 1 to the loop count so doesn't loop forever
currentCount += 1

#add the event to c4d's global event list (actually runs the script)****
c4d.EventAdd()

#handles exit points for main function?
if __name__=='__main__':
main()


Oh yea and it's pretty bloated because it's heavily documented.

Scott Ayers
10-28-2010, 10:16 PM
There seems to be a problem with your list and while code. Because only the first child is being renamed instead of all the children.
Normally in Coffee I use GetDown() inside of the while loop to get at the child objects.
This python list stuff is all new to me.


Sebastian has already answered the question about __name__=='__main__' here:
http://forums.cgsociety.org/showthread.php?t=921256
http://forums.cgsociety.org/showthread.php?f=182&t=925185

It's basically there just as a double check to call the main() function of a script when the user executes the script manually by hand with the Script Manager. Which is why it doesn't get used in the tags.


-ScottA

mayajunky
10-28-2010, 10:19 PM
Hmmm are you using the downloaded zip or copy/paste out of the code here? Cause as you know the formatting gets all messed up. They are all renaming here, well should i say all first generation children. It's not setup for grandchildren, etc... You in R12?

The while loop is renaming the current child in the list based on the currentCount value. It doesn't really have to "getDown" in the loop since it's using the loop count to define which object in the list to grab for renaming.

Thanks for the links! Hmm not sure why it's not working for you exactly. Working on a new one here for generating joint chains based off a count and offset. Got that working but the gui stuff for the user input is being a pain!

Scott Ayers
10-28-2010, 10:24 PM
I used the zipped version.
It's working fine for the first child. I didn't realize it was only supposed to rename just the first child object.

-ScottA

mayajunky
10-28-2010, 10:26 PM
Well depends what you mean exactly, when you say first child, do you mean just the first child of the siblings on the same level? All the siblings of the direct children should rename. But no it doesn't affect grandchildren the way it's written. It's all just practice really, becoming familiar with the syntax and function lists. :P

CGTalk Moderation
10-28-2010, 10:26 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.