Home Development Creating Overrides with Linked Libraries

Creating Overrides with Linked Libraries

by Patrick W. Crawford
Blender Python Overrides cover

Have you ever library linked a group from another blender file, but wished you could easily modify one part of it? Well, you could always modify the library linked source file, or… you could use this little trick.

How to Create Easy Overrides Using Python

Video Tutorial

See the video playback from the World Blender Meetup Day (March 17th, 2018)

Text Tutorial Overview

Firstly, what is an override and why are they useful? Consider the situation where an animation project uses the same rig in multiple scenes. The optimal way to keep everything in sync and not duplicate data is to use library linking.

However, if you want to make individual changes to the linked library, it can be cumbersome. You usually end up having to modify the source character rig file (which then propagates to all other scenes), or make a branch of the character rig just for that one scene (messy and hard to keep up to date!). The third option is to simply append the data and make it local and modify it as needed (even messier!). This is where overrides step in. They allow you to only modify what you need in the file you need it. All the while, you get to maintain the library linking.

In this tutorial, an override is accomplished by creating a short python script which runs once when the blender file is opened. It runs after the library data is loaded, thus acting like a macro or sequence of actions that are taken every time the file is opened.

Note: The text tutorial here is abbreviated, see the video playback for full context

 

How it works, and how to set it up

When you have a linked library group, rig, material, etc loaded in another blender file, what really happens under the hood similar in a way to the action of “appending” the data. The key difference is that this “appended” data isn’t actually saved with the blender file, the library linked data is always re-loaded from the source file.

The Blender interface mostly prevents you from directly modifying these link, but it turns out you can manipulate these objects through Python. This means you can do anything – delete objects, move vertices, swap materials and images, and more. You can modify any of the linked data on the fly. Your changes, however, do not persist with the blender file. Even after saving, your changes on linked-data would be reset the next time you open the file.

The trick, thus, is to create a python script which describes all the changes you want to make, and automatically re-runs these changes (ie re-run the script) every time the blender file is opened. From the artists’ perspective, it’s as if those changes really are saved with the file!

You can make as many overrides as you want, and as complex as needed, but keep the following in mind:

  • Name your text file something that ends in ‘.py’
  • Be sure to tick the “register” box so that the script auto runs when opening the file
  • When opening a new Blender session, you may need to click “reload trusted” (python scripts initially disabled)
  • Rendering on command line or in the cloud? No worries! Just be sure to set the “auto-execute python scripts” (ie the -y flag)
  • If your library goes missing, or if your library changes (e.g. objects renamed or deleted), the script will fail and possible show a popup error message

Sample Code

Find below the final code for both the day and night scenes of the provided tutorial files, both representing different kinds of overrides. Note that you save and run these scripts in the scene files, not in the sources of the library-linked assets.

Day scene code example

In this first file “foxy_scene_day.blend”, we will implement two different overrides:

  • Hide the hat in both the viewport and for rendering
  • Change the texture of the hat from one image to another

This of course is done all while keeping the file library linked and without modifying that original file at all. The first step is knowing the name of the object you want to hide. If unsure, it can be quick to check in the source library blend, or by checking against all objects loaded in the blend file. Then, you simply need to assign the hide and hide_render attributes.

import bpy

# Hide the hat in viewport and render
bpy.data.objects["FOXY.hat"].hide = True
bpy.data.objects["FOXY.hat"].hide_render = True

For the second override, we first need to grab the image datablock. This can be done by either checking autocomplete against bpy.data.images, or by dragging and dropping the image fromt he dropdown menu in any UV image editor, onto the text editor window. We then can directly assign the replacement image to load.

import bpy

# Change the hat's image texture from one image to another on disk
bpy.data.images["foxy hat.png"].filepath = '//textures/foxy hat alt.png'

Running these scripts will immediate take effect, but to keep the effect even after closing and reopening the files, be sure to check register, and choose “reload trusted” if it appears upon next open.

Night scene code example

In the “foxy_scene_night.blend” file, the goal is to simply move the hat off of the character’s head . This way, Foxy can move around without affecting the hat. You can accomplish this by assigning a new object to act as the hat’s parent, changing it from something attached to the proxied rig.

import bpy

hat = bpy.data.objects["FOXY.hat"]
override = bpy.data.objects["hat_override"]

hat.parent = override
hat.location = (0,0,0)

Note that clearing the location at the end can help make it easier to transform and move around, though it might not line up perfectly to the location of the empty.

Remember, be sure to name the python script with “.py” and check the “Register” box for overrides to work.

 

Learn more about python in Blender!

Have any specific questions, or running into issues? Post a comment below, and I’ll do my best to get back to you with some help. The Foxy model and low poly tree model both modified from CC0 sources on BlendSwap, everything else was made for this tutorial – use it how you like (CC0)!

You may also like