www.xbdev.net
xbdev - software development
Wednesday June 25, 2025
Home | Contact | Support | Blender (.py) Scripts... Automating Blender ..
     
 

Blender (.py) Scripts...

Automating Blender ..

 

Create Texture Procedurally


We'll create a texture on-the-fly in the Python script - and map it onto the surface of the shape.

The texture will be a blank texture with some random circles drawn on it.


Generate a texture in Python and use it in Blender - texture mapping to cover the sphere. We can link with other Python librari...
Generate a texture in Python and use it in Blender - texture mapping to cover the sphere. We can link with other Python libraries and tools and use them with Blender (e.g., Pillow/PIL, OpenCV, ChatGPT text libraries, ...).


import bpy
import random
from mathutils import Vector
from math import radians
from PIL import ImageImageDraw
import numpy 
as np
import os

# -----------------------
# 1. Clear the scene
# -----------------------
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

for 
block in bpy.data.meshes:
    
bpy.data.meshes.remove(block)
for 
block in bpy.data.materials:
    
bpy.data.materials.remove(block)
for 
block in bpy.data.images:
    
bpy.data.images.remove(block)

# -----------------------
# 2. Create blank texture and draw random circles
# -----------------------
widthheight 10241024
img 
Image.new("RGBA", (widthheight), (000255))
draw ImageDraw.Draw(img)

for 
_ in range(100):  # number of circles
    
radius random.randint(10100)
    
random.randint(0width)
    
random.randint(0height)
    
color tuple(random.randint(0255) for _ in range(3)) + (255,)
    
draw.ellipse((radiusradiusradiusradius), fill=color)

image_path bpy.app.tempdir "random_circles.png"
img.save(image_path)

# Load image into Blender
image bpy.data.images.load(image_path)

# -----------------------
# 3. Create material with texture
# -----------------------
material bpy.data.materials.new(name="SphereMaterial")
material.use_nodes True
bsdf 
material.node_tree.nodes.get("Principled BSDF")

# Add image texture node
tex_image material.node_tree.nodes.new("ShaderNodeTexImage")
tex_image.image image
material
.node_tree.links.new(bsdf.inputs['Base Color'], tex_image.outputs['Color'])

# -----------------------
# 4. Create sphere and assign material
# -----------------------
bpy.ops.mesh.primitive_uv_sphere_add(radius=1location=(000))
sphere bpy.context.object
sphere
.data.materials.append(material)

# -----------------------
# 5. Add camera
# -----------------------
bpy.ops.object.camera_add(location=(0, -40), rotation=(radians(90), 00))
camera bpy.context.object
bpy
.context.scene.camera camera

# -----------------------
# 6. Add light
# -----------------------
bpy.ops.object.light_add(type='POINT'location=(0, -33))
light bpy.context.object
light
.data.energy 1000

print("Scene setup complete.")






 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.