Free script: Easy focusing while animating a camera in Blender

While animating a camera, it is often desirable to quickly set the focus on a specific object. The “Focus on Object” field in Blender is incredibly useful for this, allowing the quick selection of a focus object via the eyedropper tool. However, once this link is set, it remains permanent. If the link is severed, the focus on the specified object is lost. I sought a solution that would allow me to capture the correct focus distance based on the focus object before severing the link. This led me to develop a script that not only calculates but also sets the “Focus Distance” field automatically to the appropriate value. This script triggers when the focus object property changes from empty to populated. It then calculates the distance, logs it in the console, and sets the focus distance. Now, you can break the link to the focus object while retaining the focus based on the last calculated distance.

How to Use the Script:

  1. Open Blender and Set Up Your Scene: Ensure your scene contains at least one camera and other objects you might want to focus on.
  2. Open the Scripting Tab: Navigate to the ‘Scripting’ tab in Blender to access the Python console and scripting area.
  3. Paste the Script: Copy the script provided below and paste it into a new text editor within the Scripting tab.
  4. Run the Script: After pasting, run the script by pressing the ‘Run Script’ button. This will activate the script, and it will start monitoring changes to the focus object property.
  5. Use the Camera’s Focus Object Field: Go to your camera’s object data properties and under the depth of field settings, use the eyedropper to select a focus object from your scene. The script will automatically update the “Focus Distance” to match the distance to the selected object.
  6. View the Output: Check the console (located in the same Scripting tab) to see the printed focus distance. This value is also automatically set in the camera’s “Focus Distance” field.
  7. Sever the Link If Desired: You can now sever the link to the focus object without losing the focus distance, as it has been set manually based on the last calculation.
  8. Here is the script:

import bpy

# Global variable to keep track of the last focus object
last_focus_object = None

def update_focus_distance_if_needed(scene):
    global last_focus_object
    camera = scene.camera

    if camera and camera.data.dof.use_dof:
        current_focus_object = camera.data.dof.focus_object

        # Check if the focus object was None and now is set
        if last_focus_object is None and current_focus_object is not None:
            # Calculate and update the focus distance
            camera_location = camera.matrix_world.translation
            target_location = current_focus_object.matrix_world.translation
            focus_distance = (target_location - camera_location).length

            camera.data.dof.focus_distance = focus_distance
            print(f"Focus distance updated to: {focus_distance}")

        # Update the last known focus object
        last_focus_object = current_focus_object

def register_handlers():
    global last_focus_object
    # Initialize last focus object
    camera = bpy.context.scene.camera
    if camera and camera.data.dof.use_dof:
        last_focus_object = camera.data.dof.focus_object

    # Add handler
    bpy.app.handlers.depsgraph_update_pre.append(update_focus_distance_if_needed)
    print("Handler registered to monitor focus object changes.")

register_handlers()

Solution to “Incorrect function” when trying to initialize a disk in the Windows Disk Manager

I cloned my Windows installation to a larger M.2 disk and then put the old smaller M.2 disk into an external usb enclosure. But no computer would recognize it properly. The Windows Disk Manager wanted me to “initialize” the disk but neither the MBR nor the GPT option would work, they both throw an error saying “incorrect function”.

I finally decided to try the disk in another usb enclosure, and it started working immediately. It turns out I was trying to use a SATA M.2 disk in a enclosure meant for NVMe SSD enclosures. While they looked the same, the functionalities were different and not interchangeable.

So the lesson is that you need to make sure you have the right kind of enclosure depending on if your disk is a SATA M.2 or NVMe M.2 SSD disk.

SATA M.2: These SSDs use the Serial ATA (SATA) interface protocol, which is the same interface used by traditional 2.5-inch SATA SSDs and hard drives. They typically have slower transfer speeds compared to NVMe SSDs, but are still much faster than traditional hard drives.

NVMe SSD: NVMe stands for Non-Volatile Memory Express. This is a newer interface protocol designed specifically for SSDs to take advantage of the high-speed PCIe bus. NVMe SSDs offer significantly faster transfer speeds compared to SATA SSDs, making them ideal for high-performance applications and tasks that require fast data transfer rates.

Both SATA M.2 and NVMe M.2 SSDs are physically installed into the same M.2 slot inside a computer. The difference lies in how they communicate with the motherboard once installed.

The M.2 slot is a small, form-factor slot on the motherboard specifically designed to accommodate M.2 SSDs. This slot can support both SATA and NVMe M.2 SSDs, but the SSDs themselves use different interface protocols once connected.

So, regardless of whether you have a SATA M.2 SSD or an NVMe M.2 SSD, you would physically install it into the same M.2 slot on your motherboard. However, the interface protocol and speed capabilities differ between the two types of SSDs.

Automating the import of hundreds of sprites to Construct

I’m making a game where an area of ground is broken piece by piece. I managed to create a python script that uses a voronoi pattern to shatter my image into several small irregular pieces. Here’s how that looked:

I then needed to import those 200 pieces into Construct. I figured out that if I set the position of each piece to 0,0, they would form the full image perfectly. After that I can just crop the pieces to their actual pixels and things work really nicely: the voronoi jigsaw puzzle comes together like it should.

The only pain point I had left was importing 200 sprites one by one into Construct. I knew that I could drag them all at the same time on top of Construct, which would create an animation. But I needed to create several individual sprites instead of animation frames.

The real life saver in this situation turned out to be the Project Folder feature in Construct (works best in Chrome), since it gives direct access to the project file structure. So here are the steps I performed and the scripts I used:

Please don’t attempt this with your production code, instead start a new empty Construct project and save it as a Project Folder on your local hard disk

1. Use a Python script to loop through all the image files in a folder and have it generate the JSON files Construct expects to have in the objectTypes folder. Here is the script for that (zipped due to security considerations). To run that, just unzip, place it in the same folder with the images, open a terminal there and type: python generateProjectJSON.py and hit enter. A bunch of JSON files should appear which you should copy to your Project Folder’s objectTypes folder.

2. Next the project.c3proj file needed to have the files listed in the items array in this manner:

“objectTypes”: {
“items”: [
“myImage_0”,
“myImage_1”,
“myImage_2”
],

So made a script to generate the array. Note that this is a very simple script and could be improved to support more files name conventions. At the moment it is just adding an incrementing number at the end of each image name. This script will output a simple array in a file called names.txt. Copy the array to your project.c3proj file.

3. The images needed to be renamed with -default-000.png appended to their file name. Time for another script, run this similarly in the folder with the images. After that I copied the images into the images folder inside the Project Folder.

4. That’s basically it but I also wanted the sprites already placed in the layout at coordinates 0,0 so I wrote one more script to do that. Run this script where you have your JSON files (that we generated earlier) and it will give you a new file called new_layout.json. Take the “instances” array from the generated file and merge it with your Layout 1.json file (which can be found in the layouts folder).

Now just reload the project and something magical happens: all your images will appear in the project and in the layout! Even if it’s hundreds or even thousands of images.

So there we have it. This workflow is obviously not useful if you just need to import a small number of sprites. But if you needs hundreds, then this can definitely save you some frustrating manual labor.

Finally, an insanely good pixel art AI

As a 2D game creator, I have been searching for an text-to-pixel art generator AI for a long time. It looks like we have finally arrived! Check out the results I got for the prompt

“a sprite sheet of several pixel art doors”!


Wow! Those are INSANELY good compared to previous pixel art generative AI’s!

So by now you must be dying to know which tool I used to generate those and how much it costs! Well I have good news for chatGPT users! I used Dalle-3, which you can now use for free with a chatGPT subscription! That’s right, since I already subscribe to chatGPT, I generated those awesome images for free!
This is getting really exciting for indie game developers everywhere!

Download a basic empty WordPress Block theme

If you are looking to download a basic, barebones WordPress block theme to test this new feature in WordPress, look no further. Below is a download link to a basic empty block theme, generated with the Create Block Theme plugin from the WordPress developers. Simply unzip this file into your themes-directory, activate it in the themes section of the dashboard and you should be good to go.

Basic block theme

How to pause browser execution when F8 is not working

Let’s break down a simple trick that can help you manipulate and understand your code better.

To begin with, access your developer console. This can usually be found in your browser’s Developer Tools under the ‘Console’ tab. Depending on the browser you’re using, you might need to use different shortcuts (like F12) or methods to open it. But don’t worry, a quick search on how to open the developer console in your specific browser should get you on the right track.

Once you’ve opened the console, the next step involves entering a particular command. All you need to do is simply paste the provided command line in the console. This is what we’re going to use to manipulate our code. After pasting the command, hit the ‘Enter’ key to execute it.

document.addEventListener('keydown', function (e) {

if (e.keyCode == 119) { // F8

debugger;

}

}, {

capture: true

});

Now, your code should still be in an ‘unpaused’ state. But when you press F8 on the keyboard, it should pause. It’s like freezing a moment in time, letting you thoroughly inspect and understand how your code behaves for specific elements. This can be especially useful when debugging hover-effects and mouseovers.

With this simple trick, your web development toolkit has a new superpower! Experiment, explore, and let your code reveal its secrets to you. Happy coding!”