3
$\begingroup$

I am attempting the check whether a function returns True or False in order to procede to the next step of a separate function.

For example, here is a stripped down version of what I have:

def target(modifier):
    if modifier == 'MIRROR':
        return True
    elif modifier == 'ARRAY':
        return False

def execute(self, context):
    if target is True:
        print("True")
    else:
        print("False")

This does not work, though. How can I check what the return boolean of target() is?

$\endgroup$
  • $\begingroup$ Okay I believe I've got it with: "if target(modifier) is True:" Is there a better way to do this? $\endgroup$ – Jonathan Williamson Jul 3 '13 at 20:47
  • $\begingroup$ Yes, that is the way to do it. Unless target() is run and assigned to a variable, there is nothing to pass in to execute(). $\endgroup$ – CharlesL Jul 3 '13 at 21:13
  • 4
    $\begingroup$ Pure Python questions would be better placed over at Stack Overflow. $\endgroup$ – Aldrik Jul 4 '13 at 1:29
  • 1
    $\begingroup$ Good call @Aldrik $\endgroup$ – Jonathan Williamson Jul 4 '13 at 15:15
4
$\begingroup$

The way you are doing it in the second block is not what you think you are doing. The line

if target is True:

will ask if a function reference is the same as a literal True...which it is not. You want to invoke the function, so use the parenthesis and pass in the string you want.
If the parameter "context" is the one you are interested in comparing (if you are using BPY, it isn't, heads up), it will instead be:

if target(context):

Notice I drop the is True", if is doing that for you already, you don't need to do it again.

$\endgroup$
  • $\begingroup$ Is doing "if target(context):" the same as doing "if target(context) is True:"? $\endgroup$ – Jonathan Williamson Jul 3 '13 at 21:27
  • $\begingroup$ Yes, without redundancy. "if" will only execute true statements, so just returning true is enough. Checking if True = True is a tautology. $\endgroup$ – Kirbinator Jul 3 '13 at 21:43
  • 1
    $\begingroup$ @JonathanWilliamson No, if obj: checks truthiness. if obj is True: checks identity. $\endgroup$ – Aldrik Jul 4 '13 at 1:05
  • $\begingroup$ @Aldrik: Good point, you would filter out returned strings or numbers this way. $\endgroup$ – Kirbinator Jul 7 '13 at 20:08
4
$\begingroup$

With True/False/None you can check identity.

if target(obj) is True:
    # do_something

Though in most cases there is no need to be so explicit and you can do...

if target(obj):
    # do_something

In some cases you may want to test against True though.

$\endgroup$
1
$\begingroup$

How about something like this:

import bpy

# Input String the name of modifier
def exists(modifiername):
    if bpy.context.active_object.modifiers.get(modifiername):
        print("yes, true")
        return True
    else:
        print("not true")
        return False

exists("Mirror")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.