1
$\begingroup$

I was trying to scale a bunch of objects with a script and I tried this line on the console :

thing.scale= Vector((1, 1, 1))

It works fine and set the scale of the object to 1 but then I tried to use the same code in a loop on the text editor and I got an error saying that Vector is not defined :

Python: Traceback (most recent call last):
  File "E:\w3D-mqbaka\Mobiverse\looop__\f_cking_modif\08.blend\scale_the_big_ones.py", line 6, in <module>
  NameError: name 'Vector' is not defined

This is the code I tried to run :

import bpy;

visible_objects=[ob for ob in bpy.context.view_layer.objects if (ob.visible_get() and ob.type== 'MESH')];

for stuff in visible_objects:
    stuff.scale= Vector((1, 1, 1));#reset scale
    if (stuff.dimensions.z >= 0.3):
        stuff.scale= Vector((1/stuff.dimension.z, 1/stuff.dimension.z, 1/stuff.dimension.z));
        stuf.scale= Vector((1/3, 1/3, 1/3));

My question is not how to makie the code working since I know that I can just remplace Vector((1, 1, 1)) by (1, 1, 1) and the code will run with the expected outcome.

My question is why is Vector running on the console and not on the text editor?

$\endgroup$

1 Answer 1

1
$\begingroup$

The answer is actually quite simple. While typing my question, I scrolled on my console to see if I imported a pyhton library that would have Vector defined in until I got to the top and saw the console welcome text that says :

PYTHON INTERACTIVE CONSOLE 3.10.2 (main, Jan 27 2022, 08:34:43) [MSC v.1928 64 bit (AMD64)]

Builtin Modules:       bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.context, bpy.utils, bgl, blf, mathutils
Convenience Imports:   from mathutils import *; from math import *
Convenience Variables: C = bpy.context, D = bpy.data

Which says that the math and mathutils library that define Vector has already been preimported in the console for convenience but one need to import it manually when typing a script in the text editor.

$\endgroup$
1
  • $\begingroup$ So basically one needs to from mathutils import Vector (and whatever else one may want, or import mathutils and use mathutils.Vector to keep the global namespace clean) $\endgroup$
    – Luc
    Commented Apr 25 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .