2
$\begingroup$

How to create the generated "UV Grid" texture with the help of the Blender Python API?

$\endgroup$

2 Answers 2

3
$\begingroup$

Either generate a checkerboard and assign the pixels to a new image data block or just call bpy.ops.image.new() and pass UV_GRID:

enter image description here

One downside of using the operator is that you would have to supply a reliable (unique) name for the image data block otherwise it automatically gets renamed to Untitled.001, Untitled.002... due to Blender's nature of handling data:

import bpy
import secrets

# Generate "unique" name
image_name = secrets.token_hex(5)

# Call the operator 
bpy.ops.image.new(
        name=image_name, 
        width=1024, 
        height=1024, 
        color=(0.0, 0.0, 0.0, 1.0), 
        alpha=True, 
        generated_type='UV_GRID', # BLANK, COLOR_GRID
        float=False, 
        use_stereo_3d=False, 
        tiled=False
    )

# Get the image from data blocks
image = bpy.data.images.get(image_name)

# Display the result
if image:
    bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
    # Change area type
    area = bpy.context.window_manager.windows[-1].screen.areas[0]
    area.type = 'IMAGE_EDITOR'

    # Assign the image
    bpy.context.area.spaces.active.image = image

Related:

$\endgroup$
1
  • $\begingroup$ Perfect! Exactly what I needed. $\endgroup$ Aug 22, 2020 at 17:19
1
$\begingroup$

It's also possible to create a new image containing the "UV Grid" or the "Color Grid" directly, without using the bpy.ops.image.new(...) operator:

import bpy

# Create the image
img = bpy.data.images.new(
        name='some name',
        width=1024,
        height=1024,
        # Further arguments are optional and this are the defaults:
        alpha=False,
        float_buffer=False,
        stereo3d=False,
        is_data=False,
        tiled=False
    )

# Setup the grid
img.source = 'GENERATED'  # optional, since it's done implicitly
img.generated_type = 'UV_GRID'  # alternatively use 'COLOR_GRID' here
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .