Flawed Logic Here
-
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 tasksfixed :
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