Random image of Textel, my bat fursona

COBERTOS

ConEmu and cmder fix for saving current working directory

ConEmu and tools built on it like cmder are awesome for Windows development. I can't see myself ever going back to conhost.exe with its lack of color, unicode-support, buffered-scrolling, tabs, configuration, sane hotkeys... ConEmu does come with a set of annoyances though.

The biggest ConEmu annoyance for me is it not properly persisting the current working directory in Git Bash. Long story short you need to add this snippet to your .bashrc.

#ConEmu Integration
if [[ -n "${ConEmuPID}" ]]; then
  #For WSL and cygwin/msys connector (which ConEmu will use for Git bash). It
  #sends an operating system command (OSC) to cygwin/msys connector to update the
  #cwd on PS1 print (the \$PWD in the below string, \w will not work).
  #https://conemu.github.io/en/ShellWorkDir.html#connector-ps1
  #https://github.com/Maximus5/ConEmu/issues/1752
  PS1="\[\e]9;9;\"\$PWD\"\007\e]9;12\007\]$PS1"
fi

ConEmu by default does not know when Git Bash changes its working directory. This breaks features like tab cloning (hotkey WinS), %CD% in tasks, and workspace persistence. To notify ConEmu of changes, you have to send an operating system command (OSC) using special control characters. The ConEmu documentation does describe how to set this up but I still found it buggy. After extensive testing, I found that only paths that contain the ~ character break it and submitted a bug for it. Using the above snippet I created implements the OSC described in the documentation and also fixes the bug.

This fixes tab cloning, %CD% in tasks, and workspace persistence for non-cmd shells. You can now use all of those features and do nifty things like using the same directory when opening a new task no matter what shell. All you need to do is add /dir %CD% to your "Task Parameters" so that when ConEmu starts that task, all commands will have %CD% as their current working directory.

Con Emu Task Parameters GUI with extra parameter

Read More »

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

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...

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.

The Build Process

  1. Install needed programs.

    • SVN: to check out the precompiled Windows dependencies
    • CMake: to make Blender
    • Microsoft Visual Studio >2013: For Blender compilation
    • Blender Source Code or Git: Blender source code as downloaded from the website or git to checkout the exact commit you want to build
    • Python installation of your desired version of the same bitness you want to build Blender in (Determining Python Bitness).
  2. Create a directory structure like the following

.
..
blender/                      #Blender source goes in here
build/                        #This is where you have CMake target
lib/win[dows,64]_vc[12,14]/   #Prebuilt binaries. The name matters! `windows` for 32 bit or `win64` for 64 bit, `vc12` for MSVS12 (2013) or `vc14` for MSVS14 (2015) and MSVS15 (2017)
  1. Checkout the precompiled Windows dependencies based on your bitness desired

  2. Download the source (or checkout the git repo) into the blender/ folder.

    • Blender's Git repos though you'll want to clone git://git.blender.org/blender.git specifically.
    • If you're using git make sure to checkout the specific commit you want and also git submodule update --init --recursive
  3. In build/, run cmake -DWITH_PLAYER=OFF -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_MODULE=ON -DPYTHON_VERSION=3.5 ../blender

    • For 64 bit, you might need to force CMake to use a specific generator. -G"Visual Studio 15 Win64" where 15 is your Visual Studio version
    • Replace the Python version number with your chosen Python version
    • There are other configurable options found in blender/CMakeLists.txt if you want to enable/disable other features (like the game engine)
  4. In the Visual Studio Developer Command Prompt, also in build/, run devenv Blender.sln /Build [TARGET] /Project INSTALL where [TARGET] is a Visual Studio release target. Most likely you'll want Release

    • You can see the other targets if you open the the .sln file.
  5. You now have the built files. Copy the files into the Python's global site-packages directory

copy bin\bpy.pyd C:\Python35\Lib\site-packages\
copy bin\*.dll C:\Python35\Lib\site-packages\
del C:\Python35\Lib\site-packages\python35.dll
xcopy /E bin\2.78 C:\Python35\2.78\

You can now open up python and test the module. Just open python on the command line and type import bpy and you now have access to Blender's Python modules. bpy.app will also give you useful information about the current build.

If you prefer to install it in a virtual environment, the commands above work just the same, though with the last one, make sure that 2.78 is copied into your virtual env Scripts folder and not the root

Troubleshooting:

  • Something with _Insert_n mentioning Eigen: Go into the mentioned .h (.hpp?) and change vector_base::_Insert_n to vector_base::insert in the corresponding .cpp file.
  • %1 is not a valid Win32 application: You have built the 32 bit Blender and tried running it from 64 bit Python. Rebuild with the correct bitness.
  • bpy: couldnt find 'scripts/modules', blender probably wont start.: You need to install the .pyd and related files in Python's site-packages, otherwise it cannot find the supporting files.

Thanks to all the other wonderful people who wrote tutorials that got me part of the way through this build!

Read More »