Confusing Syntax In The Template Scripts

Sorry if this is a stupidly basic question, but I can’t find an explanation for it through Google. I’m guessing it’s because I lack the knowledge about the terminology to be able to put it in to Google to make it pop out an answer, but here it is.

In the operator_modal.py template script, I see the following near the top of the ModalOperator class first_mouse_x: IntProperty()

What is it? It looks like it might be a shorthand declaration of a method by having it copy some previously defined function, but there’s no def keyword before it. I also thought it might be something along the lines of declaring methods as members/properties, but there’s no @Property annotation above it. What is the significance of this syntax?

What you’re seeing (the colon) is called annotation syntax.

In blender 2.8, this has replaced the old assignment (=) way of declaring built-in bpy.props properties. Annotations themselves aren’t a new thing, but python 3.6 introduced so-called variable annotations and this is what Blender now expects when any of the bpy.props built-ins are instantiated.

Read more

[…] the goal of annotation syntax is to provide an easy way to specify structured type metadata for third party tools and libraries via the abstract syntax tree and the annotations attribute.

Practically speaking it provides a way of accessing class variables like a dictionary (eg. through SomeClass.__annotations__).

Conversely, variables declared by annotation are not accessible directly (trying to access SomeClass.annotated_var would throw a NameError).

2 Likes

Thanks for the info. I get the feeling the differences between Python and other languages I use(d) are more significant than I anticipated and are quickly getting bigger. Like the default Python version that comes with the most up-to-date version of my laptop’s operating system is only 2.7. It seems a bit strange that the first mouse X variable appears to be defined as an integer, but I guess I’ll hopefully figure out why there isn’t a jump of up to 0.5 BUs when the example script starts, as I learn more about Python in Blender.

Thanks again for the helpful pointers. It’s really appreciated.