If statement for clipboard number that comes as a string?

Hello!

I’m trying to take clipboard value and, if it’s a number, let my code use it.

But I’m getting stuck over initial if statement, failing at decision, if the clipboard string is a number or not.

Would anyone know how to get this to work?

Thank you!

            tempintvalue = int(bpy.context.window_manager.clipboard)
                if tempintvalue is int:
                    self.message(f'test1')
                    outcome_value = int(bpy.context.window_manager.clipboard)
                else:
                    self.message(f'test2')
                    outcome_value = 0

The code above gives me following error

    tempintvalue = int(bpy.context.window_manager.clipboard)
ValueError: invalid literal for int() with base 10: 'tfdfew'

“tfdfew” is a random clipboard value that should be ignored in this case

This is the wrong approach. Your if statement is pointless, because it will only ever return true. Int(n) is int? Is, by definition, always true.

Instead, replace everything with this:


tempintvalue=bpy.context.window_manager.clipboard

if isinstance(tempintvalue, int): 
    do something 
else: 
    do something else 

I see.

I tried your snippet, but it keeps ignoring any numeral value every time, unfortunately.

You could always do it the “wrong” way:

tempintvalue=bpy.context.window_manager.clipboard

def getInt(t):
try: 
    return int(t)
except:
    return False

b = getInt(tempintvalue)
if b != False: 
    do something with the int
else:
    do something with the not-int