Python scripts

Python scripts

Those scripts should work as expected:

  1. Process one model from the command line
  2. Automatic BakeMyScan tests

Those don’t work at the moment and will be fixed soon (they are here for me to fix them, so don’t even bother reading them):

  1. Process multiple models
  2. Process a Sketchfab collection
  3. Import processed models in a grid layout

And others will come!

0 - Before getting started

Get ready to use the command line

These scripts are still unstable, and might not work in some cases. Although they should work, proceed with caution!

First make sure that you followed the related setup instructions, and navigate with your command prompt into the directory containing the scripts.

If you do not know what a command prompt is (don’t worry, we’ve all been there!), I’d suggest getting familiar with it first: you’ll find some example instructions for Windows here and for MacOS there. If you use linux, I’ll assume that you know how to use a terminal ;)

Finally, make sure to replace “blender” in every command by the correct executable. It might be “blender”, “/path/to/blender/blender.exe”, or “blender.exe” if you are using the powershell on Windows. I’ll leave that up to you to find your way!

If something fails

If some of the scripts fail, you can try to:

  1. check the terminal output to try and understand what happened
  2. run the command without -b, which will open blender’s GUI, and look at the blender file to understand what went wrong.

If this still does not work, have a look at the current issues to see if you can relate to something I’m working on or planning to implement, and if you think you found a bug, do not hesitate to open a new issue.

1 - Process one model

The whole process is automated thanks to the scripts/bakemyscan.py python script that blender can execute from the command line. Check the script body to understand what happens.

Usage

blender -b -P scripts/bakemyscan.py -- --help
blender -b -P scripts/bakemyscan.py -- IN OUT [-Z] [-M METHOD] [-X TARGET] [-R RESOLUTION] [-c COLOR] [-m METAL] [-r ROUGH] [-g GLOSS] [-a AO] [-n NORMAL] [-d HEIGHT] [-e EMIT] [-o ALPHA]

Examples

For instance on my linux computer, all the following commands would be acceptable to process model.obj into model.fbx:

blender -b -P bakemyscan.py model.obj model.fbx

blender -b -P bakemyscan.py model.obj model.fbx -Z

blender -b -P bakemyscan.py model.obj model.fbx -M MMGS -X 0.005

blender -b -P bakemyscan.py model.obj model.fbx -M INSTANT -X 4000

blender -b -P bakemyscan.py model.obj model.fbx -M QUADRIFLOW -X 3000

blender -b -P bakemyscan.py model.obj model.fbx -M MESHLAB -X 500

blender -b -P bakemyscan.py model.obj model.fbx -M ITERATIVE -X 1000

blender -b -P bakemyscan.py model.obj model.fbx -M DECIMATE -X 1000

blender -b -P bakemyscan.py model.obj model.fbx -M NAIVEQUADS -X 0.01

blender -b -P bakemyscan.py model.obj model.fbx -R 2048 –color albedo.png –normal nm.jpg –metallic metal.tif –roughness rough.jpeg –ao ambientOcc.jpg –displacement heightmap.PNG –emission emit.jpg –opacity alpha.png

2 - Automatic tests

Build Status

I also use a python script to run tests with Travis (see the build history here), in order to validate that I did not break everything while modifying the code, and also to obtain that nice green badge (I hope it’s still green!!).

The script is available here: tests/tests.py, and you can launch it on your computer to see if everything happens correctly with:

blender -b -P tests/tests.py

Note that the tests depending on external software might fail if they were not configured properly.

3 - Process multiple models

Does not work yet (I still have to… fix stuff)

Really, do not try it yet ;)

To process multiple models, you can use the scripts/batch_processing/blender_bake_multiple_models.py script, which is a wrapper around bakemyscan.py.

The command line syntax is as follows:

python3.5 bakeAll.py -i INDIR -o OUTDIR -p PREFIX [-t TARGETFACES] [-r RESOLUTION]

with:

Please note that to be matched automatically, the albedo texture file associated with your model must contain “alb” or “col” (case-insensitive) in its name, while the normal texture must contain “nor”. Files called myObject_Albedo.png or COLOR_myObject.jpg will therefore be identified as an albedo map (but not myObject_image.jpg) and texture_norMalS.png as a normal map.

The script will ask you to manually assign albedo and/or normal map textures if it does not find textures with such patterns.

For instance, the following directory structure can be automatically processed:

/home/loic/tmp/
  model1/
    model1.fbx
    textures/
      normals.png
      albedo.png
  elephant/
    thatstheelephant.obj
    theelephant_NORMALS.png
    someCOlOrs.png
  model-123456789ABCDEF/
    source/
      strangeName17-FTW.ply
    textures/
      theelephant_NORMALS.png
      someCOlOrs.png

The command used would be:

python3.5 bakeAll.py -i /home/loic/tmp -o /home/loic/lowpoly -p elephants -t 2500 -r 2048

terminal

Upon completion (and after having received info similar to the screenshot above), the directory /home/loic/lowpoly should look like so:

/home/loic/lowpoly/
  elephants_01.fbx
  elephants_01_ALBEDO.png
  elephants_01_NORMALS.png
  elephants_02.fbx
  elephants_02_ALBEDO.png
  elephants_02_NORMALS.png
  elephants_03.fbx
  elephants_03_ALBEDO.png
  elephants_03_NORMALS.png
  list.csv
  blender_log_001.txt
  blender_log_002.txt
  blender_log_003.txt

IMPORTANT: The last files are the logs from blender, which you can inspect if something went wrong during the transformation to lowpoly, and the list.csv file keeps track of the models previously processed, so that existing models are not overridden when running the script multiple times in a row (to modify problematic models or add new ones for instance).

4 - Process a Sketchfab collection

Does not work anymore either

The documentation below is outdated!!

IMPORTANT: as you can check in the source files, the python scripts only use your Sketchfab email adress and password in the login_to_sketchfab function (found in downloadSketchfab.py) in order to automatically login to your Sketchfab account from selenium.

Usage

python3.5 downloadSketchfab.py -u URL -o OUTDIR [-m MAIL] [-p PWD]

Example

To download this stick in /home/loic/tmp:

python3.5 downloadSketchfab.py -u https://sketchfab.com/models/644fb3e8dc134d8f994f4446bfaf1718 -o /home/loic/tmp -m loic@mail.com -p mypassword

To download all models from this collection stored in /home/loic/tmp (you will be prompted for your Sketchfab user name and password if you don’t write them in the command):

python3.5 downloadSketchfab.py -u https://sketchfab.com/norgeotloic/collections/lowpoly-assets -o /home/loic/tmp

Note that the models name, url and license as well as the author name and profile url are dumped to a credits.md file in the output directory, to keep track of the objects you downloaded and allow you to give appropriate credit.

Once the downloads are complete, you can then use the bakeAll.py script to process your models!

5 - Import processed models

I also wrote a script (importAll.py) to import the baked models in blender on a grid layout, similar to the one below:

fossils

It is used like so:

blender --python importAll.py -- -i INDIR [-r NUMROWS]

with:

Note that as I did not specify the –background option, a new blender window will open, in which you’ll be able to adjust the grid layout (using for instance Individual origins as pivot point to scale and rotate objects, or Manipulate center points to change the margin).

I would advice you to save the newly (and nicely!) created scene by packing the textures using File -> External data -> Pack all in .blend, which will then allow you to remove the temporary directories and models which were created in the process.