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.
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.
