Bobskater - A Python obfuscation Library

Using: pythonv3.5

Recently I had the need to obfuscate/minify some Python files before I distributed them. Coming from the Javascript world of ES6 where transpilers and processors are plentiful, I figured there would be a mature, well maintained library similar to UglifyJS in the Python ecosystem. It turns out that this isn’t the case.

I decided to code and package my own, called bobskater. It turned out to be surprisingly easy with Python’s ast library, the invaluable documentation at GreenTreeSnakes, and a little library called astunparse. Being completely AST based, it won’t die on weird syntax or require special formatting. All obfuscation decisions are based on AST node type. There are a few limitations as of now but they’re all documented in the repository.

There do exist a lot of libraries/techniques out there (free and pay) that perport to do this, with mixed results:

  • Compile to .pyc using compile(). Even at the highest optimization you can still extract the original code, variable names, and docstrings with uncompyle6 in literally just one command.
  • Encrypt your modules and hook the import functionality to decrypt them when loading them at runtime. Too simple to reverse in my opinion.
  • Compile your modules using Cython, which removes Python bytecode though introduces it’s own set of problems, like not being able to call using named kwargs (see this citrisbyte article for more details). I could see this not playing nicely with Blender’s import system.
  • opy - For a Regex-based obfuscator it worked surprisingly well except for one or two corner cases. Doesn’t scale well though as all external identifiers that shouldn’t get obfuscated need to be manually entered. Requires specific source code formatting but isn’t an issue if your code is based on Python’s recommendations
  • pyminifier - An unmaintained, hybrid regex/token-based obfuscator. 80 open issues and seemingly errors in very basic cases.
  • pyobfuscate - Last updated in 2013, not particularly well documented and non hopeful open issues.
  • mnfy - Python minifier but no obfuscation.
  • and a few others.
Powered by Hexo Logo Fursona by TFOTR

Cubecus for Blender is here!

Cubecus is here! After a long time coming (4 years on and off), I’ve gotten my Blender level design add-on into a releasable state (even though in alpha and with some glitches). Don’t worry though! It’s going to get better with more releases where I plan to separate out the obfuscator it uses into a separate project and add more features and fixes.

If you’re unfamiliar, give it’s page a look. It explains about the different tools it adds and provides screenshots of use (like the one below).

I hope that everyone who’s asked about it and even those who’ve not, find it useful :)

Importing and Reloading Python Modules in Blender

Using: blenderv2.7 pythonv3.5

Python in Blender can be tiring. A simple problem becomes an arduous trek through docs, examples, and sometimes the C API to find the Blender way to write given Python code. This is due to the many quirks of Blender’s own internal Python environment.

Importing is one of those arduous tasks. Python provides a lot of functionality to import all different kinds of source and data files but Blender’s implementation makes design decisions that create issues. This is my deep dive into Blender’s Python import integration where at the end I provide a small module to make your future Blender importing life easier.

Importing

In a nutshell, Python’s module importing/loading comes bundled in the from ... import ... as ... syntax. This syntax ultimately compiles to various forms of the __import__() function. Instead of interfacing with this directly, Python provides importlib in Python 3.4+ (previously imp in Python < 3.4) to make interfacing with __import__() much easier. Blender decides to forgo this setup for its own implementation with the intention of making their own integration work better with the way Blender handles its data. This allows it to support nifty features like code files being text datablocks and inline python snippets in other parts of the application.

Compiling Blender as a Python Module for Windows 10 x64 using Visual Studio

Using: blenderv2.78 pythonv3.4 or v3.5

If you want to do unit tests of Blender Python code, it might be to your benefit to not startup Blender every time you want to run them but to just import Blender as a Python module and run them from the command line. This is especially important/nice if you want to automate your tests. Note, before you jump in, if you just need mathutils you can get that separately here.

Luckily, Blender’s build has a nifty feature by which you can compile it as a Python module and then import blender from Python…

1
2
import bpy #Starts up Blender as Python module
from mathutils import Vector #Import a Blender specific library

Unfortunately for me, all of the tutorials I found were not geared toward the target I was looking for: Blender 2.78, Windows 10 x64, Microsoft Visual Studio Express 2013+, and Python 3.5. After a couple days I was able to get it all building and working with this tool chain and it should work just as well for x86, a different Python version, or a different MSVS version.

Projected Balance Calculator Pt Deux

I updated the balance projection calculator thingy a week or so ago but I thought I’d write about it. Here’s a demo image

Graph of > 1 year worth of financial transaction projections. The blue graph represents the balance, the orange represents balance with a savings percentage (in this case %30), and the green represents the total amount possible to spend without hitting 0.

New features include:

  • Filled graph with colors for extra spice
  • Multiple graphs including maximum possible expenditures and savings percentages
  • Labels to show exactly what charges come out when (using a given label when setting up the charges)

One of the issues I noticed after I generated this is that you need to generate a sufficient amount into the future to make sure that really far off charges aren’t missed in the factors applied to the maximum possible expenditures graph.

Projected Balance Calculator

I always felt that if I was able to see how much money I would have in the future I could more accurately save, spend, and make bill payments instead of just worrying all the time. Such a tool exists as a part of Quicken but Quicken is a slow, confusing piece of software that I bought and ended up returning because it was that bad(!). I looked but Mint.com doesn’t have it, GnuCash doesn’t have it, and doing it in Excel would require I learn VB, which, let’s be honest, would probably result in me developing some serious medical condition.

I coded up the class model while I was at the barber shop in a Google Note and finished it in 3-ish hours at home. It honestly took more time to find the exact days that all my different services charge me on and put that in the program. It’s pretty small and it needs a few more useful features before I can really use it but even in its current state I already feel comfortable where I am financially. Code can be found on GitHub.

Graph of projected balance over 90 days from today using a mixture of bogus and real data.

The nicest part about this was I found libraries for both the plotting (Plotly) and for the reoccuring date problem (python-dateutil). My resultant program, including tests, was only about 100 lines long.

Anyway, it’s been a pretty tough week and I don’t think this will be my last endeavor with Python in the coming months. I want to revisit my music library and update that program so I guess we’ll see how that goes.

Why Python is so exciting for me

I’m in love with Python. Like madly in love. If I wasn’t pretty crazy about this one girl right now I’m sure that I’d probably start hanging out with Python more, take it out to dinner, get to know it better, you know how it is.

I remember when I first looked at Python and went “What in the hell, who would want to program in this?! No braces? Colons?? How do you read the code??” Eventually I grew to really love it and has become possibly my favorite to program in (on the same level as C#). I know some that will try to force their beliefs on others when it comes to languages and IDEs. I only want to share with you my excitement for this language. In terms of enjoyment though, I really do believe a favorite language is based on personal taste and people should respect that.

But why do I love it? I could probably go on and on about the exact features I like but that’d get a little repetitive. If I had to choose one thing I’d have to say the simplicity of the collections matched with their flexibility and power is my favorite feature. Also, the fact that there’s a library that pretty much does anything you want just one console command away doesn’t hurt either (with PyPI).

Python is also the first language I could just rattle out simple tools to serve my life with.