This commit is contained in:
2025-09-11 13:13:21 +03:00
parent 80a71c1dc1
commit c1ac5297f2
3 changed files with 280 additions and 60 deletions

View File

@@ -47,8 +47,16 @@ class TEXT_TEXTURE_OT_generate(Operator):
self.report({'ERROR'}, "Failed to generate diffuse image")
return {'CANCELLED'}
# Create diffuse texture
img_name = f"TextTexture_{props.text[:20].replace(' ', '_')}"
# Create diffuse texture with proper sanitization
import re
# Remove non-ASCII and non-alphanumeric characters, keep spaces temporarily
sanitized_text = re.sub(r'[^\w\s-]', '', props.text[:20])
# Replace spaces and multiple underscores/dashes with single underscores
sanitized_text = re.sub(r'[\s_-]+', '_', sanitized_text).strip('_')
# Fallback if text becomes empty
if not sanitized_text:
sanitized_text = "Texture"
img_name = f"TextTexture_{sanitized_text}"
# Remove existing diffuse texture
if img_name in bpy.data.images:
@@ -57,20 +65,15 @@ class TEXT_TEXTURE_OT_generate(Operator):
# Create new diffuse image with USER SPECIFIED dimensions
blender_img = bpy.data.images.new(img_name, final_width, final_height, alpha=True)
# Convert PIL to Blender for diffuse texture
pixels = []
for y in range(final_height):
for x in range(final_width):
r, g, b, a = img.getpixel((x, final_height - 1 - y))
pixels.extend([r/255.0, g/255.0, b/255.0, a/255.0])
blender_img.pixels = pixels
# Directly copy pixel data from generated image to Blender image
# The generated image is already in the correct format, just copy it
blender_img.pixels[:] = img.pixels[:]
blender_img.pack() # PACK FINAL IMAGE
# Create normal map texture if enabled and generated
normal_map_blender_img = None
if normal_map_img and props.generate_normal_map:
normal_map_name = f"TextTexture_Normal_{props.text[:20].replace(' ', '_')}"
normal_map_name = f"TextTexture_Normal_{sanitized_text}"
# Remove existing normal map texture
if normal_map_name in bpy.data.images:
@@ -80,14 +83,12 @@ class TEXT_TEXTURE_OT_generate(Operator):
normal_map_blender_img = bpy.data.images.new(normal_map_name, final_width, final_height, alpha=False)
normal_map_blender_img.colorspace_settings.name = 'Non-Color' # Important for normal maps
# Convert PIL to Blender for normal map
normal_pixels = []
for y in range(final_height):
for x in range(final_width):
r, g, b = normal_map_img.getpixel((x, final_height - 1 - y))
normal_pixels.extend([r/255.0, g/255.0, b/255.0, 1.0]) # Alpha always 1.0 for normal maps
normal_map_blender_img.pixels = normal_pixels
# Directly copy normal map pixel data
# Copy RGB channels and set alpha to 1.0
temp_pixels = list(normal_map_img.pixels[:])
for i in range(3, len(temp_pixels), 4):
temp_pixels[i] = 1.0 # Set alpha to 1.0 for normal maps
normal_map_blender_img.pixels[:] = temp_pixels
normal_map_blender_img.pack() # PACK NORMAL MAP
print(f"[Normal Map] Created and packed normal map texture: {normal_map_name}")
@@ -134,82 +135,110 @@ class TEXT_TEXTURE_OT_generate_shader(Operator):
def execute(self, context):
try:
print(f"[TTG SHADER DEBUG] === SHADER GENERATION STARTED ===")
props = context.scene.text_texture_props
# Generate texture first
final_width = props.texture_width
final_height = props.texture_height
print(f"Generating shader with texture at {final_width}x{final_height}px")
print(f"[TTG SHADER DEBUG] Step 1: Starting texture generation at {final_width}x{final_height}px")
result = generate_texture_image(props, final_width, final_height)
print(f"[TTG SHADER DEBUG] Step 2: Texture generation returned: {type(result)}")
if not result:
self.report({'ERROR'}, "Failed to generate texture image")
return {'CANCELLED'}
print(f"[TTG SHADER DEBUG] Step 3: Processing result...")
# Extract diffuse and normal map images
if isinstance(result, tuple):
img, normal_map_img = result
print(f"[TTG SHADER DEBUG] Step 3a: Got tuple result - img: {type(img)}, normal: {type(normal_map_img)}")
else:
# Backward compatibility
img = result
normal_map_img = None
print(f"[TTG SHADER DEBUG] Step 3b: Got single result - img: {type(img)}")
if not img:
self.report({'ERROR'}, "Failed to generate diffuse image")
return {'CANCELLED'}
# Create diffuse texture name
img_name = f"TextTexture_{props.text[:20].replace(' ', '_')}"
print(f"[TTG SHADER DEBUG] Step 4: Image validation passed, proceeding with shader creation...")
print(f"[TTG SHADER DEBUG] Step 5: Creating diffuse texture...")
# Create diffuse texture name with proper sanitization
import re
# Remove non-ASCII and non-alphanumeric characters, keep spaces temporarily
sanitized_text = re.sub(r'[^\w\s-]', '', props.text[:20])
# Replace spaces and multiple underscores/dashes with single underscores
sanitized_text = re.sub(r'[\s_-]+', '_', sanitized_text).strip('_')
# Fallback if text becomes empty
if not sanitized_text:
sanitized_text = "Texture"
img_name = f"TextTexture_{sanitized_text}"
print(f"[TTG SHADER DEBUG] Step 5a: Sanitized texture name: {img_name}")
# Remove existing diffuse texture if it exists
if img_name in bpy.data.images:
print(f"[TTG SHADER DEBUG] Step 5b: Removing existing texture")
bpy.data.images.remove(bpy.data.images[img_name])
print(f"[TTG SHADER DEBUG] Step 5c: Creating new Blender image...")
# Create new diffuse texture
blender_img = bpy.data.images.new(img_name, final_width, final_height, alpha=True)
print(f"[TTG SHADER DEBUG] Step 5d: New image created, copying pixel data...")
# Convert PIL to Blender for diffuse texture
pixels = []
for y in range(final_height):
for x in range(final_width):
r, g, b, a = img.getpixel((x, final_height - 1 - y))
pixels.extend([r/255.0, g/255.0, b/255.0, a/255.0])
blender_img.pixels = pixels
# Directly copy pixel data from generated image to Blender image
# The generated image is already in the correct format, just copy it
blender_img.pixels[:] = img.pixels[:]
print(f"[TTG SHADER DEBUG] Step 5e: Pixel data copied, packing image...")
blender_img.pack()
print(f"[TTG SHADER DEBUG] Step 5f: Image packed successfully")
print(f"[TTG SHADER DEBUG] Step 6: Processing normal maps...")
# Create normal map texture if enabled and generated
normal_map_blender_img = None
if normal_map_img and props.generate_normal_map:
normal_map_name = f"TextTexture_Normal_{props.text[:20].replace(' ', '_')}"
print(f"[TTG SHADER DEBUG] Step 6a: Normal map enabled and generated, processing...")
# Use the same sanitized text for consistency
normal_map_name = f"TextTexture_Normal_{sanitized_text}"
print(f"[TTG SHADER DEBUG] Step 6b: Sanitized normal map name: {normal_map_name}")
# Remove existing normal map texture
if normal_map_name in bpy.data.images:
print(f"[TTG SHADER DEBUG] Step 6c: Removing existing normal map")
bpy.data.images.remove(bpy.data.images[normal_map_name])
print(f"[TTG SHADER DEBUG] Step 6d: Creating normal map image...")
# Create new normal map image
normal_map_blender_img = bpy.data.images.new(normal_map_name, final_width, final_height, alpha=False)
normal_map_blender_img.colorspace_settings.name = 'Non-Color' # Important for normal maps
print(f"[TTG SHADER DEBUG] Step 6e: Normal map image created, copying pixels...")
# Convert PIL to Blender for normal map
normal_pixels = []
for y in range(final_height):
for x in range(final_width):
r, g, b = normal_map_img.getpixel((x, final_height - 1 - y))
normal_pixels.extend([r/255.0, g/255.0, b/255.0, 1.0]) # Alpha always 1.0 for normal maps
normal_map_blender_img.pixels = normal_pixels
# Directly copy normal map pixel data
# Copy RGB channels and set alpha to 1.0
temp_pixels = list(normal_map_img.pixels[:])
for i in range(3, len(temp_pixels), 4):
temp_pixels[i] = 1.0 # Set alpha to 1.0 for normal maps
normal_map_blender_img.pixels[:] = temp_pixels
print(f"[TTG SHADER DEBUG] Step 6f: Normal map pixels copied, packing...")
normal_map_blender_img.pack()
print(f"[Normal Map] Created normal map texture for shader: {normal_map_name}")
print(f"[TTG SHADER DEBUG] Step 6g: Normal map packed successfully")
else:
print(f"[TTG SHADER DEBUG] Step 6: Normal map generation skipped")
print(f"[TTG SHADER DEBUG] Step 7: Creating material...")
# Create or get material using custom name or fallback
if props.custom_material_name.strip():
material_name = props.custom_material_name.strip()
print(f"[TTG SHADER DEBUG] Step 7a: Using custom material name: {material_name}")
else:
# Fallback to text-based naming
clean_text = props.text[:15].replace(' ', '_').replace('\n', '_')
material_name = f"TextTexture_Mat_{clean_text}"
# Use the same sanitized text for consistency
material_name = f"TextTexture_Mat_{sanitized_text}"
print(f"[TTG SHADER DEBUG] Step 7b: Using sanitized material name: {material_name}")
if material_name in bpy.data.materials:
mat = bpy.data.materials[material_name]