From d6bb836aa31defca20b2b4dd102b586ba012e7d0 Mon Sep 17 00:00:00 2001 From: Dragorn421 Date: Sat, 14 Jun 2025 15:33:33 +0200 Subject: [PATCH] Cleanup: setOrigin() (#537) --- fast64_internal/oot/oot_utility.py | 2 +- fast64_internal/oot/skeleton/utility.py | 2 +- fast64_internal/oot/tools/operators.py | 2 +- fast64_internal/sm64/sm64_geolayout_writer.py | 2 +- fast64_internal/utility.py | 24 ++++++++++++------- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/fast64_internal/oot/oot_utility.py b/fast64_internal/oot/oot_utility.py index 1f8bc4a..89c54fe 100644 --- a/fast64_internal/oot/oot_utility.py +++ b/fast64_internal/oot/oot_utility.py @@ -364,7 +364,7 @@ def ootDuplicateHierarchy(obj, ignoreAttr, includeEmpties, objectCategorizer) -> for modifier in selectedObj.modifiers: attemptModifierApply(modifier) for selectedObj in meshObjs: - setOrigin(obj, selectedObj) + setOrigin(selectedObj, obj.location) if ignoreAttr is not None: for selectedObj in meshObjs: if getattr(selectedObj, ignoreAttr): diff --git a/fast64_internal/oot/skeleton/utility.py b/fast64_internal/oot/skeleton/utility.py index 0a565c3..d76b837 100644 --- a/fast64_internal/oot/skeleton/utility.py +++ b/fast64_internal/oot/skeleton/utility.py @@ -220,7 +220,7 @@ def ootDuplicateArmatureAndRemoveRotations(originalArmatureObj: bpy.types.Object try: for obj in meshObjs: - setOrigin(armatureObj, obj) + setOrigin(obj, armatureObj.location) bpy.ops.object.select_all(action="DESELECT") armatureObj.select_set(True) diff --git a/fast64_internal/oot/tools/operators.py b/fast64_internal/oot/tools/operators.py index 1e834fc..a5db2cd 100644 --- a/fast64_internal/oot/tools/operators.py +++ b/fast64_internal/oot/tools/operators.py @@ -59,7 +59,7 @@ class OOT_AddDoor(Operator): parentObject(cubeObj, emptyObj) - setOrigin(emptyObj, cubeObj) + setOrigin(cubeObj, emptyObj.location) return {"FINISHED"} diff --git a/fast64_internal/sm64/sm64_geolayout_writer.py b/fast64_internal/sm64/sm64_geolayout_writer.py index 5c039e9..a0cd41b 100644 --- a/fast64_internal/sm64/sm64_geolayout_writer.py +++ b/fast64_internal/sm64/sm64_geolayout_writer.py @@ -298,7 +298,7 @@ def replaceDLReferenceInGeo(geoPath, pattern, replacement): def prepareGeolayoutExport(armatureObj, obj): # Make object and armature space the same. - setOrigin(armatureObj, obj) + setOrigin(obj, armatureObj.location) # Apply armature scale. bpy.ops.object.select_all(action="DESELECT") diff --git a/fast64_internal/utility.py b/fast64_internal/utility.py index e709391..955c0e3 100644 --- a/fast64_internal/utility.py +++ b/fast64_internal/utility.py @@ -685,14 +685,20 @@ def checkIdentityRotation(obj, rotation, allowYaw): ) -def setOrigin(target, obj): - bpy.ops.object.select_all(action="DESELECT") - obj.select_set(True) - bpy.context.view_layer.objects.active = obj - bpy.ops.object.transform_apply() - bpy.context.scene.cursor.location = target.location - bpy.ops.object.origin_set(type="ORIGIN_CURSOR") - bpy.ops.object.select_all(action="DESELECT") +def setOrigin(obj: bpy.types.Object, target_loc: mathutils.Vector): + if not target_loc.is_frozen: + target_loc = target_loc.copy() + with bpy.context.temp_override( + selected_objects=[obj], + active_object=obj, + ): + obj.location += -target_loc + # Applying location puts the object origin at world origin + # (It is only needed to apply location to set the origin, + # but historically this function has applied all transforms + # so just keep doing that to not break anything) + bpy.ops.object.transform_apply() + obj.location = target_loc def checkIfPathExists(filePath): @@ -1081,7 +1087,7 @@ def combineObjects(obj, includeChildren, ignoreAttr, areaIndex): joinedObj.select_set(True) meshList.remove(joinedObj.data) bpy.ops.object.join() - setOrigin(obj, joinedObj) + setOrigin(joinedObj, obj.location) bpy.ops.object.select_all(action="DESELECT") bpy.context.view_layer.objects.active = joinedObj