uki-dev / blendquery

CadQuery and Build123d integration for Blender
36 stars 5 forks source link

Using blendquery add-on with classes and defined functions #10

Open pbrugugnoli opened 1 week ago

pbrugugnoli commented 1 week ago

I'm currently working with Blender 3.6. Although I have no prior experience with Python scripting in Blender, I installed the BlendQuery add-on, tested it, and loved it!

After conducting some initial tests, I attempted to copy and paste some of my own projects (Build123d scripts that work perfectly in VSCode with OCP). However, I noticed that the Build123d module (import build123d as bd) is not accessible inside my classes or defined functions.

Code 1 ` import build123d as bd

def create_cube_at(x, y, z, size): return bd.Pos(x, y, z) * bd.Box(size, size, size)

b1 = bd.Pos(20, 0, 0) * bd.Box(5, 5, 5) # works fine b2 = create_cube_at(-20, 0, 0, 8) # error "Failed to regenerate BlendQuery object: NameError: name 'bd' is not defined" `

Interestingly, I was able to get it to work by either importing build123d inside the class or function scope or by declaring it as global. The following code works just fine:

Code 2 ` import build123d as bd

def create_cube_at(x, y, z, size): global bd return bd.Pos(x, y, z) * bd.Box(size, size, size)

b1 = bd.Pos(20, 0, 0) * bd.Box(5, 5, 5) # works fine b2 = create_cube_at(-20, 0, 0, 8) # works fine as well `

Could someone explain why this happens? Is it related to how Python scripts work in Blender, something specific to Blender 3.6, or does it have to do with the way the add-on operates?