Clean Up Snapping Does Not Work When Scale Is Not Uniform

by ADMIN 58 views

Clean Up Snapping Issues with Non-Uniform Scale and Origin

Introduction

When working with the Clean Up operator in Blender, snapping vertices to a surface can be a crucial step in the retopology process. However, issues can arise when the retopology object has a non-uniform scale or its origin is not at the center of the world. In this article, we will delve into the problems that occur in these situations and explore possible solutions.

Understanding the Issue

The nearest_point_valid_sources function from common\raycast.py is a widely used tool in Blender for snapping vertices to a surface. However, as mentioned by @vxlcoder, this function can break in two specific situations:

  1. Non-Uniform Scale: When the retopology object has a non-uniform scale, the snapping process can become unreliable. This is because the function relies on the object's scale to calculate the nearest point on the surface.
  2. Origin Not at World Center: When the retopology object's origin is not at the center of the world, the snapping process can also become inaccurate. This is because the function uses the object's origin to calculate the point's local coordinates.

Examining the Code

To better understand the issue, let's take a closer look at the code. The nearest_point_valid_sources function uses the following formula to calculate the point's local coordinates:

point_local = point_world - active_object.matrix_world.translation

As @vxlcoder pointed out, the active_object.matrix_world needs to be taken into account before calculating the point's local coordinates. However, simply multiplying the point's world coordinates by the object's matrix does not yield the correct result.

Solution 1: Uniform Scale

To resolve the issue with non-uniform scale, we can apply a uniform scale to the retopology object before running the Clean Up operator. This can be done using the following code:

import bpy

# Get the active object
active_object = bpy.context.active_object

# Apply a uniform scale to the object
active_object.scale = (1, 1, 1)

However, this solution may not be ideal, as it can alter the object's original scale.

Solution 2: Origin at World Center

To resolve the issue with the origin not being at the world center, we can set the world argument to False when calling the nearest_point_valid_sources function. This will prevent the function from using the object's origin to calculate the point's local coordinates.

import bpy

# Get the active object
active_object = bpy.context.active_object

# Set the world argument to False
nearest_point_valid_sources(world=False)

However, as @vxlcoder pointed out, running the Clean Up operator multiple times in a row can still cause issues, as the function will keep looking for the nearest surface point and find a different one even though it is already on the surface.

Solution 3: Matrix Multiplication

To resolve the issue with the active_object.matrix_world not being taken into account, we can multiply the point's world coordinates by the object's matrix before calculating the point's local coordinates. This can be done using the following code:

import bpy
import mathutils

# Get the active object
active_object = bpy.context.active_object

# Get the point's world coordinates
point_world = mathutils.Vector((0, 0, 0))  # Replace with the actual point's world coordinates

# Multiply the point's world coordinates by the object's matrix
point_local = active_object.matrix_world @ point_world

# Calculate the point's local coordinates
point_local = point_local - active_object.matrix_world.translation

However, as @vxlcoder pointed out, this solution may not be correct, and further investigation is needed to resolve the issue.

Conclusion

In conclusion, the nearest_point_valid_sources function can break in two specific situations: when the retopology object has a non-uniform scale and when its origin is not at the world center. While there are possible solutions to these issues, further investigation is needed to resolve the problem with the active_object.matrix_world not being taken into account. By understanding the code and exploring different solutions, we can improve the reliability of the Clean Up operator and make it more suitable for retopology tasks.
Clean Up Snapping Issues with Non-Uniform Scale and Origin: Q&A

Introduction

In our previous article, we explored the issues that can arise when using the Clean Up operator in Blender, specifically when the retopology object has a non-uniform scale or its origin is not at the world center. We also examined possible solutions to these problems. In this article, we will answer some frequently asked questions (FAQs) related to these issues.

Q: What is the nearest_point_valid_sources function, and why is it used in the Clean Up operator?

A: The nearest_point_valid_sources function is a widely used tool in Blender for snapping vertices to a surface. It is used in the Clean Up operator to ensure that vertices are snapped to the correct surface, even when the object has a non-uniform scale or its origin is not at the world center.

Q: Why does the nearest_point_valid_sources function break when the retopology object has a non-uniform scale?

A: The nearest_point_valid_sources function relies on the object's scale to calculate the nearest point on the surface. When the object has a non-uniform scale, the function can become unreliable, leading to snapping issues.

Q: Why does the nearest_point_valid_sources function break when the retopology object's origin is not at the world center?

A: The nearest_point_valid_sources function uses the object's origin to calculate the point's local coordinates. When the object's origin is not at the world center, the function can become inaccurate, leading to snapping issues.

Q: How can I resolve the issue with non-uniform scale?

A: There are several possible solutions to this issue. One approach is to apply a uniform scale to the retopology object before running the Clean Up operator. Another approach is to modify the nearest_point_valid_sources function to take into account the object's matrix.

Q: How can I resolve the issue with the origin not being at the world center?

A: One possible solution is to set the world argument to False when calling the nearest_point_valid_sources function. This will prevent the function from using the object's origin to calculate the point's local coordinates.

Q: What is the difference between world and local coordinates in Blender?

A: In Blender, world coordinates refer to the object's coordinates in 3D space, relative to the world origin. Local coordinates, on the other hand, refer to the object's coordinates relative to its own origin.

Q: How can I convert world coordinates to local coordinates in Blender?

A: To convert world coordinates to local coordinates in Blender, you can use the following formula:

local_coordinates = world_coordinates - object_matrix_world.translation

Q: What is the matrix_world attribute in Blender, and how is it used?

A: The matrix_world attribute in Blender represents the object's transformation matrix in 3D space. It is used to convert world coordinates to local coordinates and vice versa.

Q: How can I access the matrix_world attribute in Blender using Python?

A: To access the matrix_world attribute in Blender using Python, you can use the following code:

import bpy

# Get the active object
active_object = bpy.context.active_object

# Get the matrix_world attribute
matrix_world = active_object.matrix_world

Conclusion

In this article, we answered some frequently asked questions related to the issues that can arise when using the Clean Up operator in Blender, specifically when the retopology object has a non-uniform scale or its origin is not at the world center. We also provided some possible solutions to these problems and explored the concepts of world and local coordinates in Blender.