Discussion:
[Bf-python] questions about best practice
Jenny
2014-12-03 16:05:52 UTC
Permalink
Hello,

I have a few questions.

I was wondering what are the best practices for testing Blender scripts /
add-ons. Are there testing frameworks that can be easily integrated? How
should I go about writing tests when my functions are a series of operator
calls? Should I mock them?

Secondly, when I open a script in the Blender Text Editor to run, and I'm
using an external module or package, how do I add that module / package to
the path? Right now, in my script, `import foo` just gets me `No module
named foo`.

Thank you,
Jenny
Dan Eicher
2014-12-03 19:53:33 UTC
Permalink
Post by Jenny
Hello,
I have a few questions.
I was wondering what are the best practices for testing Blender scripts /
add-ons. Are there testing frameworks that can be easily integrated? How
should I go about writing tests when my functions are a series of operator
calls? Should I mock them?
You can run python unittest from inside blender, I used to do this when
messing with the bpy to hunt bugs or make sure I didn't break anything when
adding new stuff.
Post by Jenny
Secondly, when I open a script in the Blender Text Editor to run, and I'm
using an external module or package, how do I add that module / package to
the path? Right now, in my script, `import foo` just gets me `No module
named foo`.
I tend to use sys.path.append() a lot since fedora doesn't have
/usr/local/lib in python's default path but you can also drop the module in
blender's addon module directory too. Not really sure how it works when
python is bundled with blender though...

Dan
Sybren A. Stüvel
2014-12-03 21:19:52 UTC
Permalink
Post by Jenny
Post by Jenny
Secondly, when I open a script in the Blender Text Editor to run, and I'm
using an external module or package, how do I add that module / package to
the path? Right now, in my script, `import foo` just gets me `No module
named foo`.
This is due to the current working directory being set to Blender's
directory, and not the directory of the .blend file.
Post by Jenny
I tend to use sys.path.append() a lot since fedora doesn't have
/usr/local/lib in python's default path but you can also drop the
module in blender's addon module directory too. Not really sure how
it works when python is bundled with blender though...
Loading your script as an add-on gives the added advantage that you
can press F8 to reload it. However, usually I want to put my .blend
file and Python scripts in the same directory. If you want to do that
too, take a look at the approach I describe at
http://stuvel.eu/blog/203/reloading-your-code-in-blender This gives
you a setup that allows you to load modules from the same directory as
your .blend file, and easily reload your code. The reloading isn't
flawless, but it works most of the time ;-)
--
Sybren A. Stüvel

http://stuvelfoto.nl/
http://stuvel.eu/
Campbell Barton
2014-12-03 22:54:44 UTC
Permalink
Note, we have some example Python tests here:

https://developer.blender.org/diffusion/B/browse/master/tests/python/

eg:
https://developer.blender.org/diffusion/B/browse/master/tests/python/bl_pyapi_mathutils.py

I've been using Python unittest module recently and its quite useful,
however this does rely on you being able to automate your script so
interface, cursor-events etc can be bypassed.
Post by Sybren A. Stüvel
Post by Jenny
Post by Jenny
Secondly, when I open a script in the Blender Text Editor to run, and I'm
using an external module or package, how do I add that module / package to
the path? Right now, in my script, `import foo` just gets me `No module
named foo`.
This is due to the current working directory being set to Blender's
directory, and not the directory of the .blend file.
Post by Jenny
I tend to use sys.path.append() a lot since fedora doesn't have
/usr/local/lib in python's default path but you can also drop the
module in blender's addon module directory too. Not really sure how
it works when python is bundled with blender though...
Loading your script as an add-on gives the added advantage that you
can press F8 to reload it. However, usually I want to put my .blend
file and Python scripts in the same directory. If you want to do that
too, take a look at the approach I describe at
http://stuvel.eu/blog/203/reloading-your-code-in-blender This gives
you a setup that allows you to load modules from the same directory as
your .blend file, and easily reload your code. The reloading isn't
flawless, but it works most of the time ;-)
--
Sybren A. Stüvel
http://stuvelfoto.nl/
http://stuvel.eu/
_______________________________________________
Bf-python mailing list
http://lists.blender.org/mailman/listinfo/bf-python
--
- Campbell
Loading...