← Blender.Today
  • Published 17 Feb 2023

Pylance Error

Hi, there!

I'v got some code issues indicated by the VSCode Pylance checker.

The first is when I was trying to rigister a sub menu to view3D add menu.

Here is the problematic code segment:

bpy.types.VIEW3D_MT_add.append(generate_item)

Pylance Error4.png In VSCode, the Pylance(the python checker) deems the variable "VIEW3D_MT_add" to be not a member of bpy.types. However, in my opinion, it may be dynamically created after Blender initiates. Although, the script can run without any issue, the error prompts indicated by the editor are somewhat annoying.

The other one is the property statement in operators, the Pylance regards it as an illegal annotation. The code is as below:

f_length : bpy.props.FloatProperty(name = "length", default = 0)

Pylance Error6.png

I am wondering if anyone had encountered the same issues before, or there are some python type hint rules to get around this.

And by the way, I am not an English native speaker. If there is any grammar error, or unorthodox usage of English in this post, feel free to point out that for me, I will really appreciate it.

zingerbinet1
17 Feb 2023

4 Comments

zingerbinet1 Author

I have figured out one of the problems. Here I share the solution for the first one: Use the "getattr" function to get the dynamically created member.

a_menu = getattr(bpy.types, "VIEW3D_MT_add")
a_menu.append(generate_item)

The other problem is still not solved by me. Hope someone could.

Blender_Defender

Pylance checking and VSCode Autocompletion are not official features and probably, there are multiple ways to add that support to VSCode. What tool are you using for the Pylance checking? Does the code work in Blender itself?

zingerbinet1 Author

Firstly, grateful for your attention.

The tool I am using is just the Pylance extention for VSCode. The older version of it was fine. But after I updated to the new version, the python grammar check is very strict.

The code works fine in Blender. I think maybe the danamically created variables and special usage of variable statements are the keys to cause the problem.

Blender_Defender

Well, using only Pylance and nothing else, VS Code shouldn't recognize the bpy module. Strange. I use Blender Autocomplete with VS Code and Pycharm and I haven't had any errors with it so far.