How to replace object with another

On 14/10/2015 at 15:02, xxxxxxxx wrote:

Hello,
Once again. I need your help!
How to replace "char" with "Not Found object" if char not exist ? **** (see image below) ****


**
Script :**

 import c4d  
  
def main() :  
      
    obj_parent = doc.SearchObject("Parent")  
      
    String = "Hello World"  
    iChar = 0  
      
    if iChar < len(String) :  
          
        for char in String:  
            newChar = doc.SearchObject(char).GetClone()  
            newChar.SetName(newChar.GetName() + '.temp' )  
            doc.InsertObject(newChar, parent=obj_parent)

On 14/10/2015 at 19:30, xxxxxxxx wrote:

Pending a solution I use this :

if doc.SearchObject(char) == None:
   continue

full Script:

 -----------------------------------------------  
import c4d  
  
def main() :  
      
    obj_parent = doc.SearchObject("Parent")  
      
    String = "Hello World"  
    iChar = 0  
      
    if iChar < len(String) :  
          
        for char in String:  
            if doc.SearchObject(char) == None:  
                continue  
            newChar = doc.SearchObject(char).GetClone()  
            newChar.SetName(newChar.GetName() + '.temp' )  
            doc.InsertObject(newChar, parent=obj_parent)

On 15/10/2015 at 09:04, xxxxxxxx wrote:

Hi,

I'm actually not quite sure, where you are heading. I don't see a replace operation in your code. What is so bad about your solution presented in second post? In what way does it work different from what you expect?
For the code in your second post, I'd recommend to do the SearchObject() only once, although the speed difference will probably only in very large and complex scenes.