0

I use python in blender, and there was a version upgrade from 2.80. I want to keep backward compatibility. For example older style code was:

class fixFaces(bpy.types.Operator):
    feetMesaure = BoolProperty(name="feet measure", description="feet measure", default=True)

the newer style is:

class fixFaces(bpy.types.Operator):
    feetMesaure : BoolProperty(name="feet measure", description="feet measure", default=True)

I can detect the blender version. Is there any way to use older and newer style in one code?

For example in C, I could do:

#ifdef _VERSION < 2.80
    #define _EQ =
#else
    #define _EQ :
#endif

feetMesaure _EQ BoolProperty(name="feet measure", description="feet measure", default=True)

I read about python macros, but this is not clean, how should I use it.

Robert
  • 7,394
  • 40
  • 45
  • 64
  • Maybe this helps? https://blender.stackexchange.com/questions/56741/how-can-i-dynamically-generate-operator-classes – zvone Mar 29 '22 at 15:43
  • How should I use it? I have little python knowledge. How should I create : operator to BoolProperty (bpy.props.BoolProperty) in blender 2.79 while not in newer? – Tamás Konkoly Apr 04 '22 at 12:49

1 Answers1

0

You can try catching SyntaxError exception using eval, or exec statements of python. The ':' operator will give SyntaxError in old versions whereas in later versions, code will execute successfully. Try -

try:
     eval('feetMesaure : BoolProperty(name="feet measure", description="feet measure", default=True)')
except SyntaxError:
     feetMesaure = BoolProperty(name="feet measure", description="feet measure", default=True)

More info is at this link - Failed to catch syntax error python

YadneshD
  • 396
  • 2
  • 12