Returning Object from a function.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2011 at 11:31, xxxxxxxx wrote:

    I'm trying to create a function that will basically search through all the render settings someone has set looking for a setting called something specific. If it finds that renderdata, then return it so then I can edit it instead of making a new one.

    So this is what I've got:

    def GoDown(obj) :
        x=obj
        if obj is None: return
            
        print obj.GetName()
        if obj.GetName()=="5":
            print "Found #5"
            print "5 is: " + str(x)
            print "Obj: " + str(x.GetName())
            return x
        
        if obj.GetDown() :
            GoDown(obj.GetDown())
        if obj.GetNext() :
            GoDown(obj.GetNext())

    def main() :
        RD=doc.GetFirstRenderData()
        rd2=GoDown(RD)
        print RD.GetName()    
        print rd2

    if __name__=='__main__':
        main()

    So the function basically runs through and does GoDown and GetNext until I've either found the object or run out of objects.  Then each time it goes through it checks to see if the object is called 5(as it is in my test scene). So it works, it finds what I'm looking for and then stops looking. I want to return this renderData object I find but I can't seem to make it work. return obj is not working there, it always comes back as None. Anyone can point me in the right direction?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2011 at 11:57, xxxxxxxx wrote:

    Unfortunately i can't try it in Cinema now, and I can't find any issues.

    You may try this code I just sett up from scratch.

    def Walk(obj) :   
        if not obj: return   
        if obj.GetDown() : return obj.GetDown()   
        while not obj.GetNext() and not obj.GetDown() :   
            obj = obj.GetUp()   
        return obj.GetNext()   
      
    def FindRenderData(doc,name) :   
        rd   = doc.GetFirstRenderData()   
      
        while rd.GetName() != name:   
            rd   = Walk(rd)   
      
        return rd
    

    There may be an issue in the Walk Function cause I'm on a mobilr device. ;)

    Cheers,nux



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2011 at 12:06, xxxxxxxx wrote:

    Thanks Nux that seems to work. Now to try understand it some more.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2011 at 12:14, xxxxxxxx wrote:

    Actually I may have spoken a bit too soon perhaps. It works if that renderData exists, but if it does not, I am getting an error saying that the NoneType attribute has no GetName.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2011 at 12:41, xxxxxxxx wrote:

    I'm not sure if you already know this or not Bret.
    But the code you posted doesn't step through the currently active render setting's elements.
    It steps through the hierarchy of saved render settings presets you have(or don't have).
    That's why it produces "none".

    Searching through the individual render settings, of a particular saved Render Settings preset. Is different than searching through the hierarchy of saved presets at the bottom of the render setting window.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2011 at 12:51, xxxxxxxx wrote:

    It's just poor wording. I'm looking to see if there is a Render Setting Preset. So I'm going through the preset hierarchy. The problem is my Code is working, it checks to see if there's a preset called "5" and if it is it prints and does what I ask it to. But I can't return this value to a new variable. Obviously if it does not exist it should return None, but it does exist, the code can see that 5 exists, but it does not return the Preset(in this case a RenderData type that has the name 5).



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2011 at 14:01, xxxxxxxx wrote:

    Ok. So if I understand what you're trying to do. You're able to find the specific custom render setting preset ok.
    But once you've found it. You can't change any of it's elements(settings) ?

    If I'm on the right track here. Then the most likely problem is that you don't have this custom render setting selected as the currently active preset. Which is probably why you can't do anything with it. Even after you've found it in the hierarchy.
    The SetActiveRenderData() function might be what you're looking for.

    Try this:

    import c4d  
    from c4d import gui  
      
    def GoDown(obj) :  
      x=obj  
      if obj is None: return  
        
      print obj.GetName()  
      if obj.GetName()=="5":  
          print "Found #5"  
          print "5 is: " + str(x.GetType())  
          print "Obj: " + str(x.GetName())  
          doc.SetActiveRenderData(obj)  
          return x  
        
      if obj.GetDown() :  
          GoDown(obj.GetDown())  
      if obj.GetNext() :  
          GoDown(obj.GetNext())  
      
    def main() :  
      RD=doc.GetFirstRenderData()  
      rd2=GoDown(RD)   
      c4d.EventAdd()         
      
    if __name__=='__main__':  
      main()  
    

    -ScottA


Log in to reply