1
$\begingroup$

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

$\endgroup$
2
$\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 '20 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.