A new version of Cineversity has been launched. This legacy site and its tutorials will remain accessible for a limited transition period

Visit the New Cineversity
   
 
Using Python script in C4D to select multiple objects using a portion of the name.
Posted: 20 June 2014 11:34 AM   [ Ignore ]  
Total Posts:  16
Joined  2014-06-17

Hi,
I was wondering if there is a script I can run that would allow me to SEARCH for multiple objects using only a portion of the name (all objects I am searching for having that portion in common) and SELECT those objects, in order to group them under a null. Essentially, the same function as the search bar in Cinema 4D.[/size]

Profile
 
 
Posted: 20 June 2014 12:05 PM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
Total Posts:  365
Joined  2006-05-17
def main():
    
name "Cube"
    
c4d.CallCommand(100004766)
    
objList doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0)
    for 
obj in objList:
        if 
obj.GetName().find(name) == int(-1):
            
doc.SetSelection(objc4d.SELECTION_SUB)   
    
c4d.EventAdd 

You can try this.
It is pretty shakey, but it will select any object that has the word “Cube” in the name.
You can change

name "Cube" 

to whatever you want to search for.

pretty much

name "Cube" 

is the search string

c4d.CallCommand(100004766

selects everything in the scene

objList doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0

this builds a list from the selection

for obj in objList

this loops through the list

if obj.GetName().find(name) == int(-1): 

this looks to see if the search string is in the object name

doc.SetSelection(objc4d.SELECTION_SUB

if the search string is NOT in the object name then the object is removed from the selection.

Again, there are probably cleaner ways of constructing this sort of process, but this one is the simplest I can think of.

Profile
 
 
Posted: 20 June 2014 12:07 PM   [ Ignore ]   [ # 2 ]  
Total Posts:  16
Joined  2014-06-17

Hi Dr. Sassi,

Thank you for the reply! I actually have already tried both of those commands, and neither are able to select objects by searching only part of the name, unfortunately. At least not the way I was hoping. And they only select one object instead of going through all of them.

Profile
 
 
Posted: 20 June 2014 12:08 PM   [ Ignore ]   [ # 3 ]  
Total Posts:  16
Joined  2014-06-17

Thank you Patrick!

I will try that and let you know how it goes.

Profile
 
 
Posted: 20 June 2014 12:12 PM   [ Ignore ]   [ # 4 ]  
Administrator
Avatar
Total Posts:  12043
Joined  2011-03-04

Hey DPU_Tigers,

I have taken my message down, as I think Patrick’s has way more potential :o) I think it might have “noised up” the thread otherwise. ;o)

Thanks for the feedback!

Selecting objects and grouping them under a Group Null*: Note that animations which are already applied to the single objects will suffer in most cases!
*Grouping many selected objects under a new Null, e.g., the group command, takes the average potion of all selected objects, and changes so the reference of the origin.

Good luck with the idea!

Sassi

 Signature 

Dr. Sassi V. Sassmannshausen Ph.D.
Cinema 4D Mentor since 2004
Maxon Master Trainer, VES, DCS

Photography For C4D Artists: 200 Free Tutorials.
https://www.youtube.com/user/DrSassiLA/playlists

NEW:

NEW: Cineversity [CV4]

Profile
 
 
Posted: 20 June 2014 12:23 PM   [ Ignore ]   [ # 5 ]  
Total Posts:  16
Joined  2014-06-17

Patrick,

I tried running your script, and it seems to be selecting everything (I think because of the call command 100004766), but I am trying to select just the objects that have a certain couple letters in their name.  What changes to the script you wrote above could I make to achieve this?

Your help is greatly appreciated,
Thanks

Profile
 
 
Posted: 20 June 2014 12:40 PM   [ Ignore ]   [ # 6 ]  
Administrator
Avatar
Total Posts:  365
Joined  2006-05-17

you want to specify the word/letters in

name "cube" 

So if you want to find “ex” you would change

name "cube" 
to
name "ex" 

It should be noted that this will select anything that has “ex” in the name.
If you have “Extrude” that would be selected.
If you have “extend” that would be selected.
If you have “exit” that would be selected.
And so on.

Profile
 
 
Posted: 20 June 2014 02:15 PM   [ Ignore ]   [ # 7 ]  
Total Posts:  16
Joined  2014-06-17

Patrick,

For some reason, no matter what I change the name to, it still selects all of my objects. I’m not sure if maybe its the subtraction command that isn’t working, or maybe the objects I’m working with specifically are the problem. Does this code separate child objects from parent objects?

Thanks,

DPU_Tigers

Profile
 
 
Posted: 20 June 2014 02:53 PM   [ Ignore ]   [ # 8 ]  
Administrator
Avatar
Total Posts:  365
Joined  2006-05-17

The code does what you asked, which was to select all objects that have a specific string.
So in essence I select everything and then filter out everything that does NOT have that string.
The result is something that has every object that contains that string.
This code ignores all hierarchy, as that was not specified in the initial request…the initial request was to select anything with the string.

Profile
 
 
Posted: 20 June 2014 04:54 PM   [ Ignore ]   [ # 9 ]  
Total Posts:  16
Joined  2014-06-17

Patrick,

I’m sorry, I’m still new to using python in C4D and didn’t realize that the hierarchy would get in the way.
Is there a way around the hierarchy so that you code applies to the children, not the parents only?
I’m terribly sorry for my miscommunication, and any trouble it caused you. I greatly appreciate that you are still trying to help!

Many thanks,

DPU_Tigers

Profile
 
 
Posted: 20 June 2014 05:39 PM   [ Ignore ]   [ # 10 ]  
Administrator
Avatar
Total Posts:  365
Joined  2006-05-17
name "Cube"
    
    
roots doc.GetObjects()
    print 
roots
    c4d
.CallCommand(100004794# Invert Selection
    #doc.SetSelection(roots, c4d.SELECTION_SUB)
    
c4d.CallCommand(100004766)
    for 
parent in rootsdoc.SetSelection(parentc4d.SELECTION_SUB)
    
objList doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0)
    for 
obj in objList:
        if 
obj.GetName().find(name) == int(-1):
            
doc.SetSelection(objc4d.SELECTION_SUB)   
    
c4d.EventAdd 

you can try this.
Short of actually traversing every hierarchy this is again the quickest way I can think of.

Pretty much it removes the object at the scene root and then filters children.
Without knowing your scene structure or writing something much more complicated, this is where it stands.
Perhaps Rick might know how to deal with the hierarchy traversal better.
The alternative is make sure the parent doesn’t contain your search string.

Profile
 
 
Posted: 23 June 2014 02:47 PM   [ Ignore ]   [ # 11 ]  
Total Posts:  16
Joined  2014-06-17

Thank you so much, this really helps.

Profile