Cleanup: deselectAllObjects() and selectSingleObject() (#538)

This commit is contained in:
Dragorn421
2025-06-14 15:33:51 +02:00
committed by GitHub
parent d6bb836aa3
commit c4376de2e9
15 changed files with 75 additions and 99 deletions
+2 -5
View File
@@ -91,8 +91,7 @@ def F3DtoBlenderObject(romfile, startAddress, scene, newname, transformMatrix, s
mesh.update()
if shadeSmooth:
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
selectSingleObject(obj)
bpy.ops.object.shade_smooth()
return obj
@@ -1885,9 +1884,7 @@ class F3DContext:
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
selectSingleObject(obj)
for material in self.materials:
obj.data.materials.append(material)
+1 -1
View File
@@ -292,7 +292,7 @@ class CreateLargeTextureMesh(bpy.types.Operator):
bl_options = {"REGISTER", "UNDO", "PRESET"}
def execute(self, context):
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
prop = context.scene.opLargeTextureProperty
assert prop.mat is not None
name = prop.mat.name + "Mesh"
@@ -1,7 +1,7 @@
import bpy
import mathutils
from ....utility import PluginError
from ....utility import PluginError, selectSingleObject
from ...oot_utility import convertIntTo2sComplement
from .classes import OOTCollisionVertex, OOTCollisionPolygon, getPolygonType
@@ -83,8 +83,7 @@ def addCollisionTriangles(obj, collisionDict, includeChildren, transformMatrix,
# water boxes handled by level writer
def exportCollisionCommon(collision, obj, transformMatrix, includeChildren, name):
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
selectSingleObject(obj)
# dict of collisionType : faces
collisionDict = {}
@@ -4,7 +4,7 @@ import re
from dataclasses import dataclass
from typing import Optional, TYPE_CHECKING
from bpy.types import Object, Armature
from ....utility import PluginError
from ....utility import PluginError, deselectAllObjects
from ..motion.utility import setupCutscene, getBlenderPosition, getInteger
if TYPE_CHECKING:
@@ -556,7 +556,7 @@ class CutsceneImport(CutsceneObjectFactory):
# Init camera + preview objects and setup the scene
setupCutscene(csObj)
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
print("Success!")
# ``csNumber`` makes sure there's no duplicates
+7 -10
View File
@@ -21,6 +21,8 @@ from ..utility import (
cleanupDuplicatedObjects,
hexOrDecInt,
binOps,
deselectAllObjects,
selectSingleObject,
)
if TYPE_CHECKING:
@@ -340,7 +342,7 @@ class OOTObjectCategorizer:
# This also sets all origins relative to the scene object.
def ootDuplicateHierarchy(obj, ignoreAttr, includeEmpties, objectCategorizer) -> tuple[Object, list[Object]]:
# Duplicate objects to apply scale / modifiers / linked data
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
ootSelectMeshChildrenOnly(obj, includeEmpties)
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
@@ -352,15 +354,13 @@ def ootDuplicateHierarchy(obj, ignoreAttr, includeEmpties, objectCategorizer) ->
objectCategorizer.sortObjects(allObjs)
meshObjs = objectCategorizer.meshes
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
for selectedObj in meshObjs:
selectedObj.select_set(True)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True, properties=False)
for selectedObj in meshObjs:
bpy.ops.object.select_all(action="DESELECT")
selectedObj.select_set(True)
bpy.context.view_layer.objects.active = selectedObj
selectSingleObject(selectedObj)
for modifier in selectedObj.modifiers:
attemptModifierApply(modifier)
for selectedObj in meshObjs:
@@ -369,8 +369,7 @@ def ootDuplicateHierarchy(obj, ignoreAttr, includeEmpties, objectCategorizer) ->
for selectedObj in meshObjs:
if getattr(selectedObj, ignoreAttr):
for child in selectedObj.children:
bpy.ops.object.select_all(action="DESELECT")
child.select_set(True)
selectSingleObject(child)
bpy.ops.object.parent_clear(type="CLEAR_KEEP_TRANSFORM")
selectedObj.parent.select_set(True)
bpy.ops.object.parent_set(keep_transform=True)
@@ -410,9 +409,7 @@ def ootDuplicateHierarchy(obj, ignoreAttr, includeEmpties, objectCategorizer) ->
# This is a relative transform we care about so the 90 degrees
# doesn't matter (since they're both right-handed).
print("Applying transform")
bpy.ops.object.select_all(action="DESELECT")
tempObj.select_set(True)
bpy.context.view_layer.objects.active = tempObj
selectSingleObject(tempObj)
bpy.ops.object.transform_apply()
return tempObj, allObjs
@@ -3,7 +3,7 @@ from typing import List
import mathutils, bpy, math
from ....f3d.f3d_gbi import F3D, get_F3D_GBI
from ....f3d.f3d_parser import getImportData, parseF3D
from ....utility import hexOrDecInt, applyRotation, PluginError
from ....utility import hexOrDecInt, applyRotation, PluginError, deselectAllObjects, selectSingleObject
from ...oot_f3d_writer import ootReadActorScale
from ...oot_model_classes import OOTF3DContext, ootGetIncludedAssetData
from ...oot_utility import OOTEnum, ootGetObjectPath, getOOTScale, ootGetObjectHeaderPath, ootGetEnums, ootStripComments
@@ -22,8 +22,7 @@ class OOTDLEntry:
def ootAddBone(armatureObj, boneName, parentBoneName, currentTransform, loadDL):
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
bpy.context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
bpy.ops.object.mode_set(mode="EDIT")
bone = armatureObj.data.edit_bones.new(boneName)
bone.use_connect = False
@@ -213,9 +212,7 @@ def ootBuildSkeleton(
armatureObj.location = bpy.context.scene.cursor.location
# Set bone rotation mode.
bpy.ops.object.select_all(action="DESELECT")
armatureObj.select_set(True)
bpy.context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
bpy.ops.object.mode_set(mode="POSE")
for bone in armatureObj.pose.bones:
bone.rotation_mode = "XYZ"
@@ -223,7 +220,7 @@ def ootBuildSkeleton(
# Apply mesh to armature.
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
obj.select_set(True)
armatureObj.select_set(True)
bpy.context.view_layer.objects.active = armatureObj
+8 -12
View File
@@ -14,6 +14,8 @@ from ...utility import (
attemptModifierApply,
cleanupDuplicatedObjects,
yUpToZUp,
deselectAllObjects,
selectSingleObject,
)
@@ -204,7 +206,7 @@ def ootRemoveRotationsFromArmature(armatureObj: bpy.types.Object) -> None:
def ootDuplicateArmatureAndRemoveRotations(originalArmatureObj: bpy.types.Object):
# Duplicate objects to apply scale / modifiers / linked data
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
for originalMeshObj in [obj for obj in originalArmatureObj.children if obj.type == "MESH"]:
originalMeshObj.select_set(True)
@@ -222,15 +224,13 @@ def ootDuplicateArmatureAndRemoveRotations(originalArmatureObj: bpy.types.Object
for obj in meshObjs:
setOrigin(obj, armatureObj.location)
bpy.ops.object.select_all(action="DESELECT")
armatureObj.select_set(True)
bpy.context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True, properties=False)
ootRemoveRotationsFromArmature(armatureObj)
# Apply modifiers/data to mesh objs
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
for obj in meshObjs:
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
@@ -238,16 +238,13 @@ def ootDuplicateArmatureAndRemoveRotations(originalArmatureObj: bpy.types.Object
bpy.ops.object.make_single_user(obdata=True)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True, properties=False)
for selectedObj in meshObjs:
bpy.ops.object.select_all(action="DESELECT")
selectedObj.select_set(True)
bpy.context.view_layer.objects.active = selectedObj
selectSingleObject(selectedObj)
for modifier in selectedObj.modifiers:
attemptModifierApply(modifier)
# Apply new armature rest pose
bpy.ops.object.select_all(action="DESELECT")
bpy.context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
bpy.ops.object.mode_set(mode="POSE")
bpy.ops.pose.armature_apply()
bpy.ops.object.mode_set(mode="OBJECT")
@@ -263,8 +260,7 @@ def ootDuplicateArmatureAndRemoveRotations(originalArmatureObj: bpy.types.Object
def applySkeletonRestPose(boneData: list[tuple[float, float, float]], armatureObj: bpy.types.Object):
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
armatureObj.select_set(True)
selectSingleObject(armatureObj)
bpy.ops.object.mode_set(mode="POSE")
+1 -1
View File
@@ -68,7 +68,7 @@ class AddWaterBox(OperatorBase):
return None
def execute_operator(self, context):
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
location = mathutils.Vector(bpy.context.scene.cursor.location)
bpy.ops.mesh.primitive_plane_add(size=2 * self.scale, enter_editmode=False, align="WORLD", location=location[:])
+2 -2
View File
@@ -39,6 +39,7 @@ from ..utility import (
makeWriteInfoBox,
writeBoxExportType,
enumExportHeaderType,
selectSingleObject,
)
@@ -377,8 +378,7 @@ def exportCollisionInsertableBinary(obj, transformMatrix, filepath, includeSpeci
def exportCollisionCommon(obj, transformMatrix, includeSpecials, includeChildren, name, areaIndex):
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
selectSingleObject(obj)
# dict of collisionType : faces
collisionDict = {}
+9 -11
View File
@@ -22,6 +22,8 @@ from ..utility import (
prop_split,
sm64BoneUp,
geoNodeRotateOrder,
selectSingleObject,
deselectAllObjects,
)
from .sm64_geolayout_utility import (
@@ -131,8 +133,7 @@ def parseGeoLayout(
if shadeSmooth:
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
listObj.select_set(True)
selectSingleObject(listObj)
bpy.ops.object.shade_smooth()
# Dont remove doubles here, as importing geolayout all at once results
@@ -150,7 +151,7 @@ def parseGeoLayout(
# Apply mesh to armature.
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
obj.select_set(True)
switchArmatureObj.select_set(True)
bpy.context.view_layer.objects.active = switchArmatureObj
@@ -703,8 +704,7 @@ def createConnectBone(armatureObj, childName, parentName):
def createBone(armatureObj, parentBoneName, boneName, currentTransform, boneGroup, loadDL):
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
bpy.context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
bpy.ops.object.mode_set(mode="EDIT")
bone = armatureObj.data.edit_bones.new(boneName)
bone.use_connect = False
@@ -741,7 +741,7 @@ def createSwitchOption(
armatureObj, switchBoneName, boneName, currentTransform, nextParentTransform, switchLevel, switchCount
):
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
# bpy.context.view_layer.objects.active = armatureObj
# bpy.ops.object.mode_set(mode="EDIT")
# bone = armatureObj.data.edit_bones.new(boneName)
@@ -1559,21 +1559,19 @@ class SM64_ImportGeolayout(bpy.types.Operator):
)
romfileSrc.close()
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
if armatureObj is not None:
for armatureMeshGroup in armatureMeshGroups:
armatureMeshGroup[0].select_set(True)
doRotation(math.radians(-90), "X")
for armatureMeshGroup in armatureMeshGroups:
bpy.ops.object.select_all(action="DESELECT")
armatureMeshGroup[0].select_set(True)
bpy.context.view_layer.objects.active = armatureMeshGroup[0]
selectSingleObject(armatureMeshGroup[0])
bpy.ops.object.make_single_user(obdata=True)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False, properties=False)
else:
doRotation(math.radians(-90), "X")
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
# objs[-1].select_set(True)
self.report({"INFO"}, "Generic import succeeded.")
+6 -10
View File
@@ -58,6 +58,8 @@ from ..utility import (
writeBoxExportType,
enumExportHeaderType,
geoNodeRotateOrder,
deselectAllObjects,
selectSingleObject,
)
from ..f3d.f3d_bleed import (
@@ -301,9 +303,7 @@ def prepareGeolayoutExport(armatureObj, obj):
setOrigin(obj, armatureObj.location)
# Apply armature scale.
bpy.ops.object.select_all(action="DESELECT")
armatureObj.select_set(True)
bpy.context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True, properties=False)
@@ -2950,9 +2950,7 @@ class SM64_ExportGeolayoutObject(ObjectDataExporter):
)
romfileOutput.close()
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
context.view_layer.objects.active = obj
selectSingleObject(obj)
if os.path.exists(bpy.path.abspath(context.scene.fast64.sm64.output_rom)):
os.remove(bpy.path.abspath(context.scene.fast64.sm64.output_rom))
@@ -3060,7 +3058,7 @@ class SM64_ExportGeolayoutArmature(bpy.types.Operator):
applyRotation([armatureObj] + linkedArmatures, math.radians(90), "X")
# You must ALSO apply object rotation after armature rotation.
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
for linkedArmature, linkedMesh in linkedArmatureDict.items():
linkedMesh.select_set(True)
obj.select_set(True)
@@ -3154,9 +3152,7 @@ class SM64_ExportGeolayoutArmature(bpy.types.Operator):
)
romfileOutput.close()
bpy.ops.object.select_all(action="DESELECT")
armatureObj.select_set(True)
context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
if os.path.exists(bpy.path.abspath(context.scene.fast64.sm64.output_rom)):
os.remove(bpy.path.abspath(context.scene.fast64.sm64.output_rom))
+2 -2
View File
@@ -23,6 +23,7 @@ from ..utility import (
multilineLabel,
raisePluginError,
enumExportHeaderType,
selectSingleObject,
)
from ..f3d.f3d_gbi import (
@@ -726,8 +727,7 @@ class PuppycamVolume:
def exportAreaCommon(areaObj, transformMatrix, geolayout, collision, name):
bpy.ops.object.select_all(action="DESELECT")
areaObj.select_set(True)
selectSingleObject(areaObj)
if not areaObj.noMusic:
if areaObj.musicSeqEnum != "Custom":
+2 -4
View File
@@ -6,7 +6,7 @@ from bpy.props import EnumProperty, BoolProperty, IntProperty, FloatProperty, St
from bpy.path import abspath
from ...operators import OperatorBase, AddWaterBox
from ...utility import PluginError, decodeSegmentedAddr, encodeSegmentedAddr
from ...utility import PluginError, decodeSegmentedAddr, encodeSegmentedAddr, selectSingleObject
from ...f3d.f3d_material import getDefaultMaterialPreset, createF3DMat, add_f3d_mat_to_obj
from ...utility import parentObject, intToHex, bytesToHex
@@ -245,9 +245,7 @@ class SM64_CreateSimpleLevel(OperatorBase):
warp_game_object.bparam2 = "0x0A"
warp_game_object.bparams = "0x000A0000"
bpy.ops.object.select_all(action="DESELECT")
level_object.select_set(True)
bpy.context.view_layer.objects.active = level_object
selectSingleObject(level_object)
class SM64_AddWaterBox(AddWaterBox):
+23 -25
View File
@@ -180,18 +180,26 @@ def checkObjectReference(obj, title):
)
def selectSingleObject(obj: bpy.types.Object):
bpy.ops.object.select_all(action="DESELECT")
def setActiveObject(obj: bpy.types.Object):
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
def deselectAllObjects():
for obj in bpy.data.objects:
obj.select_set(False)
def selectSingleObject(obj: bpy.types.Object):
deselectAllObjects()
setActiveObject(obj)
def parentObject(parent, child):
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
child.select_set(True)
parent.select_set(True)
bpy.context.view_layer.objects.active = parent
setActiveObject(parent)
bpy.ops.object.parent_set(type="OBJECT", keep_transform=True)
@@ -655,11 +663,9 @@ def highlightWeightErrors(obj, elements, elementType):
return # Doesn't work currently
if bpy.context.mode != "OBJECT":
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
selectSingleObject(obj)
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="DESELECT")
deselectAllObjects()
bpy.ops.mesh.select_mode(type=elementType)
bpy.ops.object.mode_set(mode="OBJECT")
print(elements)
@@ -924,25 +930,21 @@ def get_obj_temp_mesh(obj):
def apply_objects_modifiers_and_transformations(allObjs: Iterable[bpy.types.Object]):
# first apply modifiers so that any objects that affect each other are taken into consideration
for selectedObj in allObjs:
bpy.ops.object.select_all(action="DESELECT")
selectedObj.select_set(True)
bpy.context.view_layer.objects.active = selectedObj
selectSingleObject(selectedObj)
for modifier in selectedObj.modifiers:
attemptModifierApply(modifier)
# apply transformations now that world space changes are applied
for selectedObj in allObjs:
bpy.ops.object.select_all(action="DESELECT")
selectedObj.select_set(True)
bpy.context.view_layer.objects.active = selectedObj
selectSingleObject(selectedObj)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True, properties=False)
def duplicateHierarchy(obj, ignoreAttr, includeEmpties, areaIndex):
# Duplicate objects to apply scale / modifiers / linked data
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
selectMeshChildrenOnly(obj, None, includeEmpties, areaIndex)
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
@@ -958,9 +960,7 @@ def duplicateHierarchy(obj, ignoreAttr, includeEmpties, areaIndex):
for selectedObj in allObjs:
if ignoreAttr is not None and getattr(selectedObj, ignoreAttr):
for child in selectedObj.children:
bpy.ops.object.select_all(action="DESELECT")
child.select_set(True)
bpy.context.view_layer.objects.active = child
selectSingleObject(child)
bpy.ops.object.parent_clear(type="CLEAR_KEEP_TRANSFORM")
selectedObj.parent.select_set(True)
bpy.context.view_layer.objects.active = selectedObj.parent
@@ -1058,7 +1058,7 @@ def combineObjects(obj, includeChildren, ignoreAttr, areaIndex):
obj.original_name = obj.name
# Duplicate objects to apply scale / modifiers / linked data
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
if includeChildren:
selectMeshChildrenOnly(obj, ignoreAttr, False, areaIndex)
else:
@@ -1074,7 +1074,7 @@ def combineObjects(obj, includeChildren, ignoreAttr, areaIndex):
apply_objects_modifiers_and_transformations(allObjs)
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
# Joining causes orphan data, so we remove it manually.
meshList = []
@@ -1089,9 +1089,7 @@ def combineObjects(obj, includeChildren, ignoreAttr, areaIndex):
bpy.ops.object.join()
setOrigin(joinedObj, obj.location)
bpy.ops.object.select_all(action="DESELECT")
bpy.context.view_layer.objects.active = joinedObj
joinedObj.select_set(True)
selectSingleObject(joinedObj)
# Need to clear parent transform in order to correctly apply transform.
bpy.ops.object.parent_clear(type="CLEAR_KEEP_TRANSFORM")
@@ -1173,7 +1171,7 @@ def applyRotation(objList, angle, axis):
bpy.context.scene.tool_settings.use_transform_pivot_point_align = False
bpy.context.scene.tool_settings.use_transform_skip_children = False
bpy.ops.object.select_all(action="DESELECT")
deselectAllObjects()
for obj in objList:
obj.select_set(True)
bpy.context.view_layer.objects.active = objList[0]
+4 -4
View File
@@ -71,6 +71,8 @@ def attemptModifierApply(modifier):
def armatureApplyWithMesh(armatureObj: bpy.types.Object, context: bpy.types.Context):
from .utility import selectSingleObject
for child in armatureObj.children:
if child.type != "MESH":
continue
@@ -81,14 +83,12 @@ def armatureApplyWithMesh(armatureObj: bpy.types.Object, context: bpy.types.Cont
if armatureModifier is None:
continue
bpy.ops.object.select_all(action="DESELECT")
context.view_layer.objects.active = child
selectSingleObject(child)
bpy.ops.object.modifier_copy(modifier=armatureModifier.name)
print(len(child.modifiers))
attemptModifierApply(armatureModifier)
bpy.ops.object.select_all(action="DESELECT")
context.view_layer.objects.active = armatureObj
selectSingleObject(armatureObj)
bpy.ops.object.mode_set(mode="POSE")
bpy.ops.pose.armature_apply()
if context.mode != "OBJECT":