Flawed Logic Here



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

    On 13/12/2012 at 14:23, xxxxxxxx wrote:

    Thanks to everyone for their patience as I stumble through this learning process.

    So, could some one please tell me where my logic is flawed here?
    I just want to step through all the children of my Python Generator (which has been a good learning vehicle for me) and print their names – just to test my hierarchical iterator (which apparently isn't)

    ==== START ====

    import c4d
    #Welcome to the world of Python

    def init(op) :
        
        for obj in op:
            
            print(obj.GetName())
            
            if (obj.GetChildren()) :
                for child in obj.GetChildren() :
                    
                    init(child)

    def main() :
        
        init(op.GetChildren())

    ===== END ====

    Thanks!

    Jim



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

    On 13/12/2012 at 15:05, xxxxxxxx wrote:

    hi,

    please use [kode]somecode[/kode] tags, i could barely read your code. kode written with
    c of course :)

    ontopic :

    GeListNode.GetChildren() returns a list which is either empty or contains the children
    of that node. because of that if(GeListNode.GetChildren()) : willl never be executed.
    it is the shortend form for if (GeListNode.GetChildren() == True) :, which never will 
    be True. on top of that you pass a GeListNode to your init(op) method in your recursion 
    step init(child) , but in the beginning of init(op) you are iterating through the parameter 
    of init(op) , but a GelistNode is not iterable (a list). the mistake behind that is that you are 
    basicly doing one recursion step manualy in your method (you have got 2 loops there).

    def GetChildren(node) :
        print node.GetName()
        if (node.GetChildren() != []) :
            for child in node.GetChildren() :
                GetChildren(child)
    


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

    On 14/12/2012 at 04:13, xxxxxxxx wrote:

    Hello,

    Checking for the truthfulness of a list is valid. It will yield False if the list is
    empty, and True if at least one element is present. Also, you can directly
    iterate over the list, because if there are not children, no iteration will take
    place.

    def print_hierarchy(op, indent=0) :  
      print '    ' * indent + op.GetName()  
      for child in op.GetChildren() :  
          print_hierarchy(child, indent + 1)
    

    Next, you shouldn't call GeListNode.GetChildren() twice if not really
    necessary. Instead, save it temporarily.

    children = op.GetChildren()  
    if children:  
      # Do some stuff with the children..  
    else:  
      # Fall back to what to do when no children are present.
    

    Best,
    Niklas



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

    On 14/12/2012 at 06:08, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    please use [kode]somecode[/kode] tags, i could barely read your code. kode written with
    c of course :)

    No problem! I looked for it in the wsywig but didn't see it as an option.

    Thanks guys for your replies. It's very helpful of you.
    I will get on it right away.

    Jim



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

    On 14/12/2012 at 06:52, xxxxxxxx wrote:

    Guys, I tested both of your examples and (of course) they worked well!
    After comparing them to mine, I don't feel so terribly far off 🙂 from what I was trying to achieve. I see the flaws now in my code.

    Another question though...of course I would like to start growing my own class library which would include routines like this, so is there much more to add in order to import this file as an independent class? Is there an example / walk through somewhere i could check out?

    Thanks again. Greatly appreciated.

    Jim



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

    On 14/12/2012 at 10:40, xxxxxxxx wrote:

    threre are quite some thread on this multiple file topic here, this one is just a few days old :

    https://plugincafe.maxon.net/topic/6805/7560_reloading-dialog



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

    On 14/12/2012 at 12:18, xxxxxxxx wrote:

    Thanks Ferdinand. I'll check it out.

    Meanwhile, can anyone tell my why neither of these functions (derived from the above responses) will return an object and exit the routine?:

    def SearchFor(obj, name_to_match) :
        
        if (obj.GetName() == name_to_match) :
            return obj
      
        elif (obj.GetChildren() != []) :
            for child in obj.GetChildren() :
                SearchFor(child, name_to_match)
    

    or...

    def SearchFor(obj, name_to_match) :
        
        if obj.GetName() == name_to_match:
            return obj
        
        for child in obj.GetChildren() :
            SearchFor(child, name_to_match)
    

    The call:

    x = SearchFor(op, 'some_name_that_exists')
    


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

    On 14/12/2012 at 14:08, xxxxxxxx wrote:

      
    def SearchFor(obj, name_to_match) :
        if (obj.GetName() == name_to_match) :
            return obj
         for child in obj.GetChildren() :
                **[x]** SearchFor(child,  name_to_match)
    

    your recursion return value chain is broken at x. you have to pass the result back, so that the chain
    can fold itself back to the point you called it. ps : you should stick with Nikklas examples, he is by
    far the better and more experienced coder than me, his code is better even on these simple tasks :)

    fixed :

      
    def SearchFor(obj, name_to_match) :
        result = None
        if (obj.GetName() == name_to_match) :
            return obj
        for child in obj.GetChildren() :
            result = SearchFor(child, name_to_match)
        return result
    

    ps : or to make it a bit less pseudo tech gibberish : think of it as a chain of people. you ask
    the first person for the name of the person number n. unless you are not directly asking
    person n, you have to tell them also, that they have to reply the result of the question they
    have asked the next person back to the person they have been asked by. the result will
    be a chain of questions until person n is reached, followed by a series of answers the whole
    way back.



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

    On 15/12/2012 at 00:28, xxxxxxxx wrote:

    Hi Ferdinand,

    thanks for the flowers. :'D

    Anyway, your example is not quite correct, depending on what result you are
    trying to achieve. Your second code-snippet returns the last object that can be
    found given the passed name, but I think Jim wanted to retrieve the first object
    in the hierarchy that can be found given that name.

    def SearchFor(object, name) :  
      if object.GetName() == name:  
          return object  
      for child in object.GetChildren() :  
          result = SearchFor(child, name)  
          if result:  
              return result
    

    To even more extend that, you can write a more abstract function working
    with another function to find the matched object.

    def SearchFor(object, callable) :  
      if callable(object) :  
          return object  
      for child in object.GetChildren() :  
          result = SearchFor(child, name)  
          if result:  
              return result
    

    This function can be used by passing a function accepting one argument.

      
    from functools import partial  
      
    def SearchFor(object, callable) :  
      if callable(object) :  
          return object  
      for child in object.GetChildren() :  
          result = SearchFor(child, callable)  
          if result:  
              return result  
      
    def CheckName(op, name) :  
      return op.GetName() == name  
      
    name = 'NameToSearchFor'  
      
    # Example 1: Using a lambda function.  
    print SearchFor(op, lambda o: o.GetName() == name)  
      
    # Example 2: Using a lambda-function wrapping CheckName()  
    print SearchFor(op, lambda o: CheckName(o, name))  
      
    # Example 3: Usingg functools.partial.  
    print SearchFor(op, partial(CheckName, name=name))
    

    Best,
    Niklas



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

    On 17/12/2012 at 06:20, xxxxxxxx wrote:

    Thanks guys! What a help 👏👍

    Niklas, your examples are insightful. I actually had a version of this "search" function that I was using to pass a function name to be called. That way, I could execute any function depending on which object I was looking for. It was working fine, but then I started trying to incorporate passing an array of arguments to the search function, so that when the object is found, it gets passed along with the argument array to the function assigned...is that too unclear? :)

    Your help will allow me to move back to this point.

    SearchFor(object_to_find, function_to_call, args = [array, of, arguments]) :

    Thanks again guys!

    Jim



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

    On 17/12/2012 at 07:43, xxxxxxxx wrote:

    Hi Jim,

    you can either use the functools.partial function that I have used in the snippet above, or pass the values this way:

    def SearchFor(object, callable, **args=(), kwargs={}** ) :  
      if callable(object, ***args, **kwargs** ) :  
          return object  
      for child in object.GetChildren() :  
          result = SearchFor(child, callable)  
          if result:  
              return result
    

    Best,
    Niklas



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

    On 17/12/2012 at 08:20, xxxxxxxx wrote:

    Oh Great! I'll have to read up on this.
    Thanks Niklas!

    *edit - So it looks like functools.partial is a good fit for where I'm going with this. I see that in your example where you assign "result" you're not passing any arguments... *head scratch* was this intentional? They could still be passed, yes?

    Thanks again.

    Jim



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

    On 17/12/2012 at 09:31, xxxxxxxx wrote:

    Hi Jim,

    No it was not intentional, I'm sorry. 😊

    def SearchFor(object, callable, args=(), kwargs={}) :  
      if callable(object, *args, **kwargs) :  
          return object  
      for child in object.GetChildren() :  
          result = SearchFor(child, callable, args, kwargs)  
          if result:  
              return result
    

    Best,
    Niklas



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

    On 17/12/2012 at 12:17, xxxxxxxx wrote:

    Yay! Getting somewhere.
    Thanks so much guys! I appreciate your help greatly.

    def searchFor(object, callable, **kwargs) :
        if callable(object, **kwargs) :
            return object
        
        for child in object.GetChildren() :
            result = searchFor(child, callable, **kwargs)
            
            if result:
                return result
      
      
    def assignVars(obj, **kwargs) :
        if obj.GetName() == kwargs['ident']:
            return obj
      
      
      
    def main() :
        kwargs = {'arg1' : 0, 'ident' : 'gamma', 'arg3' : .0001}
        
        myresult = searchFor(op, assignVars, **kwargs)
        if myresult:
            doc.SetActiveObject(myresult)
    

    Jim



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

    On 17/12/2012 at 14:11, xxxxxxxx wrote:

    hi,

    i do not want to interfer with your urge to discover python, but using the the name of an 
    object is not a safe way to identify an object. it has been discussed here quite often.
    safe ways would be a link gui, an inexclude gui, selections or object properties.

    i hope you do understand that this isn't meant in an offensive way, after all the effort
    you have put in, to clim this hill.



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

    On 18/12/2012 at 06:28, xxxxxxxx wrote:

    Thanks Ferdinand.
    I do understand and have seen this sentiment elsewhere.

    For what I am doing is creating an object group that has:  a cloner, a clone source group (objects and deformers). The clone source is replicated under the cloner so I can gain access to individual clone deformers. Therefore, I need to target / reference the local objects / deformers to control them with local user data.

    So ultimately, I'm just referencing objects and deformers locally to control them locally rather than looping through the entire list of objects. Otherwise, if I copy my object for the scene the user data loses it's local connections and interferes with the other copies.

    Using a link or in/exclude gui is not an option (as far as I know), because I do not want the end user touching any of the source objects / deformers as everything will be controlled via user data options and all source objects / deformers will be hidden via layer settings.

    Perhaps I'm approaching this as a neanderthal, but I'm will to take the time to work it out through exchanges like this one. So, I welcome any thoughts on how to improve my methods (since they are based on a limited knowledge of both Python and the C4D SDK)

    I appreciate your comments and welcome more. I've used all the knowledge offered here to improve my understanding of these subjects.

    Thank you much,

    Jim 🙂


Log in to reply