I'm creating some ways to 'retopologize' CAD assemblies with low-poly objects. I came up with a function that takes all the selected objects and replaces them with the active object, while retaining each original object's dimensions, rotation & location.
However I have a glitch. I can't seem to figure out why half the time one or two of the newly created objects default to (0,0,0) as their location. It doesn't make any sense to me. It only seems to happen on some occasions (not all the time). Any ideas why / how to prevent this?
import bpy
def replaceObjects():
import bpy
objects = []
active_object = bpy.context.active_object
for obj in bpy.context.selected_objects:
# Pass if the object is active object, or is not a mesh (e.g. Camera & Light)
if(obj.type != 'MESH' or obj == active_object):
pass
else:
#This is an important step since objects can be offset from their origin
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY',center='BOUNDS')
world_matrix = obj.matrix_world
global_location = world_matrix.translation
rotation = world_matrix.to_3x3().to_euler('XYZ')
data = obj.data
# Save the object's name, dimensions, location to array
objects.append({"name": obj.name,
"dimensions": obj.dimensions,
"location": global_location,
"rotation": rotation,
"data": data})
# Delete this object
bpy.data.objects.remove(obj)
tag = '.c'
newID = 0
for item in objects:
newID += 1
new = str(newID)
n = bpy.data.objects.new(new, active_object.data)
# when you create a new object manually this way it's not part of any collection, add it to the active collection so you can actually see it in the viewport
bpy.context.collection.objects.link(n)
# Name the object
n.name = item["name"] + tag
# Size the object
n.dimensions = item["dimensions"]
# Position the object
n.location = item["location"]
# Rotate the object
n.rotation_euler = item["rotation"]
replaceObjects()
objects
and then delete them during the second for loop. That fixed it. $\endgroup$