1
$\begingroup$

I want to check if all object transforms are at zero with python. What I have so far is this:

 object = bpy.context.active_object

 if any(object.location) or any(object.rotation_euler):
    print(object.location)
    print(object.rotation_euler)

This statement any(object.rotation_euler) doesn't seem to work correctly as it returns true despite there being no rotation.

How do I change the statement to make it work?

$\endgroup$
1
  • 1
    $\begingroup$ If object has a non-zero location, the conditional evaluates to True, and the rotation of (0,0,0) will be printed. Also,this approach is very vulnerable to floating point errors. A tiny translation or rotation (resulting from say, +x and -x rotations) may display as 0, but not be, at high precision. $\endgroup$ Apr 16 '19 at 23:02
1
$\begingroup$

You can try rounding the values to 2-3 decimal points when comparing with 0 to make sure you're not falling victim to floating point errors:

object = bpy.context.active_object

def nozeros( vec, decimal_points = 2 ):
    ''' Returns True if none of the elements in the provided vector vec
        equate to zero when rounded by the provided number of decimal_points
    '''

    return any( round( v, decimal_points ) for v in vec )

if nozeros(object.location) or nozeros(object.rotation_euler):
    print(object.location)
    print(object.rotation_euler)

If either the location or rotation of the active object is non-zero this will script print the values of both.

$\endgroup$
2
  • 2
    $\begingroup$ suggest reducing to any(round(v, decimal_points) for v in vec) $\endgroup$
    – batFINGER
    Apr 17 '19 at 9:56
  • $\begingroup$ @batFINGER Definitely more elegant, done $\endgroup$
    – TLousky
    Apr 17 '19 at 10:00

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.