I'm trying to import a module (I think that's the right term) and then run it on the active object

In the console I can type:

> from bl_operators.uvcalc_smart_project import main
> main(C, .02, 30, 0.03, True, True)

... and it does smartuvproject on the active object.

In my script I have:

from bl_operators.uvcalc_smart_project import main
main(C, .02, 30, 0.03, True, True)

... and the console says that "name C is not defined"

I realise I'm missing something pretty basic here, to do with how a function is applied to an object in Blender scripts.

Perhaps the answer is, "go away and learn python." But this stuff is so Blender-specific (maybe even Blender 2.80 specific) that I'm not sure I'm going to find the answer anywhere except here!

Thanks in advance.


Py console convenience variables

C and D are abbreviated named variables for our convenience in the python console (see startup message)

PYTHON INTERACTIVE CONSOLE 3.7.1 (default, Oct 22 2018, 11:21:55)  [GCC 8.2.0]

Command History:     Up/Down Arrow
Cursor:              Left/Right Home/End
Remove:              Backspace/Delete
Execute:             Enter
Autocomplete:        Ctrl-Space
Zoom:                Ctrl +/-, Ctrl-Wheel
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

Simply replace C with bpy.context in your script

On importing

you are importing the method main from the module bl_operators.uvcalc_smart_project1 similarly

from bl_operators import uvcalc_smart_project


for keeping one main method distinct from another main method imported from elsewhere by using module as a namespace.

IMO something like

from bl_operators.uvcalc_smart_project import main as uv_smart_project
# and use via

is distinct and readable.

  • $\begingroup$ "distinct and readable" is right. That's very neat. Thanks as ever batFINGER - your help is greatly appreciated. $\endgroup$ – edna Apr 17 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.