From 020bb5b02c97246723359ddd0a6a765e1cfd4d45 Mon Sep 17 00:00:00 2001 From: AlvaroHG Date: Thu, 22 Aug 2024 14:33:13 -0700 Subject: [PATCH 1/2] Release changes --- ai2thor/controller.py | 42 +- ai2thor/hooks/procedural_asset_hook.py | 73 ++-- tasks.py | 12 +- unity/Assets/Scripts/FpinAgentController.cs | 416 +++++++------------- unity/Assets/Scripts/ProceduralTools.cs | 3 +- 5 files changed, 220 insertions(+), 326 deletions(-) diff --git a/ai2thor/controller.py b/ai2thor/controller.py index 057dc44fd8..423c5e2282 100644 --- a/ai2thor/controller.py +++ b/ai2thor/controller.py @@ -399,8 +399,8 @@ def __init__( server_timeout: Optional[float] = 100.0, server_start_timeout: float = 300.0, # objaverse_asset_ids=[], TODO add and implement when objaverse.load_thor_objects is available - action_hook_runner=None, - metadata_hook: Optional[MetadataHook] = None, + before_action_callback=None, + metadata_callback: Optional[MetadataHook] = None, **unity_initialization_parameters, ): self.receptacle_nearest_pivot_points = {} @@ -443,18 +443,28 @@ def __init__( ) ) - self.action_hook_runner = action_hook_runner - self.action_hooks = ( + if "action_hook_runner" in unity_initialization_parameters: + raise ValueError( + f"Deprecated argument 'action_hook_runner'. Use 'before_action_callback' instead." + ) + + if "metadata_hook" in unity_initialization_parameters: + raise ValueError( + f"Deprecated argument 'metadata_hook'. Use 'metadata_callback' instead." + ) + + self.before_action_callback = before_action_callback + self.action_callbacks = ( { func - for func in dir(action_hook_runner) - if callable(getattr(action_hook_runner, func)) and not func.startswith("__") + for func in dir(before_action_callback) + if callable(getattr(before_action_callback, func)) and not func.startswith("__") } - if self.action_hook_runner is not None + if self.before_action_callback is not None else None ) - self.metadata_hook = metadata_hook + self.metadata_callback = metadata_callback if self.gpu_device is not None: # numbers.Integral works for numpy.int32/64 and Python int @@ -971,11 +981,11 @@ def multi_step_physics(self, action, timeStep=0.05, max_steps=20): return events - def run_action_hook(self, action): - if self.action_hooks is not None and action["action"] in self.action_hooks: + def run_before_action_callback(self, action): + if self.action_callbacks is not None and action["action"] in self.action_callbacks: try: # print(f"action hooks: {self.action_hooks}") - method = getattr(self.action_hook_runner, action["action"]) + method = getattr(self.before_action_callback, action["action"]) event = method(action, self) if isinstance(event, list): self.last_event = event[-1] @@ -984,18 +994,18 @@ def run_action_hook(self, action): except AttributeError: traceback.print_stack() raise NotImplementedError( - "Action Hook Runner `{}` does not implement method `{}`," + "Action Callback `{}` does not implement method `{}`," " actions hooks are meant to run before an action, make sure that `action_hook_runner`" " passed to the controller implements a method for the desired action.".format( - self.action_hook_runner.__class__.__name__, action["action"] + self.before_action_callback.__class__.__name__, action["action"] ) ) return True return False def run_metadata_hook(self, metadata: MetadataWrapper) -> bool: - if self.metadata_hook is not None: - out = self.metadata_hook(metadata=metadata, controller=self) + if self.metadata_callback is not None: + out = self.metadata_callback(metadata=metadata, controller=self) assert ( out is None ), "`metadata_hook` must return `None` and change the metadata in place." @@ -1043,7 +1053,7 @@ def step(self, action: Union[str, Dict[str, Any]] = None, **action_args): # not deleting to allow for older builds to continue to work # del action[old] - self.run_action_hook(action) + self.run_before_action_callback(action) self.server.send(action) try: diff --git a/ai2thor/hooks/procedural_asset_hook.py b/ai2thor/hooks/procedural_asset_hook.py index 171ce5173f..0d7b1cbfc6 100644 --- a/ai2thor/hooks/procedural_asset_hook.py +++ b/ai2thor/hooks/procedural_asset_hook.py @@ -22,6 +22,8 @@ load_existing_thor_asset_file, ) +from objathor.dataset import load_assets_path, DatasetSaveConfig + logger = logging.getLogger(__name__) EXTENSIONS_LOADABLE_IN_UNITY = { @@ -248,7 +250,7 @@ def create_assets_if_not_exist( # return evt -class ProceduralAssetHookRunner: +class ProceduralAssetActionCallback: def __init__( self, asset_directory, @@ -268,6 +270,7 @@ def __init__( self.target_dir = target_dir self.extension = extension self.verbose = verbose + self.last_asset_id_set = set() def Initialize(self, action, controller): if self.asset_limit > 0: @@ -278,6 +281,10 @@ def Initialize(self, action, controller): def CreateHouse(self, action, controller): house = action["house"] asset_ids = get_all_asset_ids_recursively(house["objects"], []) + asset_ids_set = set(asset_ids) + if not asset_ids_set.issubset(self.last_asset_id_set): + controller.step(action="DeleteLRUFromProceduralCache", assetLimit=0) + self.last_asset_id_set = set(asset_ids) return create_assets_if_not_exist( controller=controller, asset_ids=asset_ids, @@ -320,27 +327,49 @@ def GetHouseFromTemplate(self, action, controller): ) -class ObjaverseAssetHookRunner(object): - def __init__(self): - import objaverse - - self.objaverse_uid_set = set(objaverse.load_uids()) +class DownloadObjaverseActionCallback(object): + def __init__( + self, + asset_dataset_version, + asset_download_path, + target_dir="processed_models", + asset_symlink=True, + load_file_in_unity=False, + stop_if_fail=False, + asset_limit=-1, + extension=None, + verbose=True, + ): + self.asset_download_path = asset_download_path + self.asset_symlink = asset_symlink + self.stop_if_fail = stop_if_fail + self.asset_limit = asset_limit + self.load_file_in_unity = load_file_in_unity + self.target_dir = target_dir + self.extension = extension + self.verbose = verbose + self.last_asset_id_set = set() + dsc = DatasetSaveConfig( + VERSION=asset_dataset_version, + BASE_PATH=asset_download_path, + ) + self.asset_path = load_assets_path(dsc) def CreateHouse(self, action, controller): - raise NotImplemented("Not yet implemented.") - house = action["house"] - asset_ids = list(set(obj["assetId"] for obj in house["objects"])) - evt = controller.step(action="AssetsInDatabase", assetIds=asset_ids) - asset_in_db = evt.metadata["actionReturn"] - assets_not_created = [asset_id for (asset_id, in_db) in asset_in_db.items() if in_db] - not_created_set = set(assets_not_created) - not_objeverse_not_created = not_created_set.difference(self.objaverse_uid_set) - if len(not_created_set): - raise ValueError( - f"Invalid asset ids are not in THOR AssetDatabase or part of objeverse: {not_objeverse_not_created}" - ) - - # TODO when transformed assets are in objaverse download them and create them - # objaverse.load_thor_objects - # create_assets() + asset_ids = get_all_asset_ids_recursively(house["objects"], []) + asset_ids_set = set(asset_ids) + if not asset_ids_set.issubset(self.last_asset_id_set): + controller.step(action="DeleteLRUFromProceduralCache", assetLimit=0) + self.last_asset_id_set = set(asset_ids) + return create_assets_if_not_exist( + controller=controller, + asset_ids=asset_ids, + asset_directory=self.asset_path, + copy_to_dir=os.path.join(controller._build.base_dir, self.target_dir), + asset_symlink=self.asset_symlink, + stop_if_fail=self.stop_if_fail, + load_file_in_unity=self.load_file_in_unity, + extension=self.extension, + verbose=self.verbose, + ) diff --git a/tasks.py b/tasks.py index 145983154c..4321fbc4c2 100644 --- a/tasks.py +++ b/tasks.py @@ -4727,10 +4727,10 @@ def test_create_prefab(ctx, json_path): def procedural_asset_hook_test(ctx, asset_dir, house_path, asset_id=""): import json import ai2thor.controller - from ai2thor.hooks.procedural_asset_hook import ProceduralAssetHookRunner + from ai2thor.hooks.procedural_asset_hook import ProceduralAssetActionCallback from objathor.asset_conversion.util import view_asset_in_thor - hook_runner = ProceduralAssetHookRunner( + hook_runner = ProceduralAssetActionCallback( asset_directory=asset_dir, asset_symlink=True, verbose=True, @@ -4747,7 +4747,7 @@ def procedural_asset_hook_test(ctx, asset_dir, house_path, asset_id=""): height=300, server_class=ai2thor.fifo_server.FifoServer, visibilityScheme="Distance", - action_hook_runner=hook_runner, + before_action_callback=hook_runner, ) # TODO bug why skybox is not changing? from just procedural pipeline @@ -4817,9 +4817,9 @@ def procedural_asset_hook_test(ctx, asset_dir, house_path, asset_id=""): def procedural_asset_cache_test(ctx, asset_dir, house_path, asset_ids="", cache_limit=1): import json import ai2thor.controller - from ai2thor.hooks.procedural_asset_hook import ProceduralAssetHookRunner + from ai2thor.hooks.procedural_asset_hook import ProceduralAssetActionCallback - hook_runner = ProceduralAssetHookRunner( + hook_runner = ProceduralAssetActionCallback( asset_directory=asset_dir, asset_symlink=True, verbose=True, asset_limit=1 ) controller = ai2thor.controller.Controller( @@ -4834,7 +4834,7 @@ def procedural_asset_cache_test(ctx, asset_dir, house_path, asset_ids="", cache_ height=300, server_class=ai2thor.wsgi_server.WsgiServer, visibilityScheme="Distance", - action_hook_runner=hook_runner, + before_action_callback=hook_runner, ) asset_ids = asset_ids.split(",") with open(house_path, "r") as f: diff --git a/unity/Assets/Scripts/FpinAgentController.cs b/unity/Assets/Scripts/FpinAgentController.cs index b2e8feff46..c97ac47e5c 100644 --- a/unity/Assets/Scripts/FpinAgentController.cs +++ b/unity/Assets/Scripts/FpinAgentController.cs @@ -11,10 +11,8 @@ using UnityEngine.Rendering.PostProcessing; using UnityEngine.UIElements; -namespace UnityStandardAssets.Characters.FirstPerson -{ - public class BoxBounds - { +namespace UnityStandardAssets.Characters.FirstPerson { + public class BoxBounds { public Vector3 worldCenter; public Vector3 agentRelativeCenter; public Vector3 size; @@ -22,8 +20,7 @@ public class BoxBounds [Serializable] [MessagePackObject(keyAsPropertyName: true)] - public class LoadInUnityProceduralAsset - { + public class LoadInUnityProceduralAsset { public string id; public string dir; public string extension = ".msgpack.gz"; @@ -33,8 +30,7 @@ public class LoadInUnityProceduralAsset #nullable enable [Serializable] [MessagePackObject(keyAsPropertyName: true)] - public class BodyAsset - { + public class BodyAsset { public string? assetId = null; public LoadInUnityProceduralAsset? dynamicAsset = null; public ProceduralAsset? asset = null; @@ -44,8 +40,7 @@ public class BodyAsset [Serializable] [MessagePackObject(keyAsPropertyName: true)] - public class BackwardsCompatibleInitializeParams - { + public class BackwardsCompatibleInitializeParams { // Make what parameters it uses explicit public float maxUpwardLookAngle = 0.0f; public float maxDownwardLookAngle = 0.0f; @@ -69,8 +64,7 @@ public class BackwardsCompatibleInitializeParams public string visibilityScheme = VisibilityScheme.Collider.ToString(); } - public class FpinAgentController : PhysicsRemoteFPSAgentController - { + public class FpinAgentController : PhysicsRemoteFPSAgentController { private static readonly Vector3 agentSpawnOffset = new Vector3(100.0f, 100.0f, 100.0f); private FpinMovableContinuous fpinMovable; public BoxCollider spawnedBoxCollider = null; @@ -79,12 +73,9 @@ public class FpinAgentController : PhysicsRemoteFPSAgentController private Transform topMeshTransform = null; private Bounds? agentBounds = null; public BoxBounds boxBounds = null; - public BoxBounds BoxBounds - { - get - { - if (spawnedBoxCollider != null) - { + public BoxBounds BoxBounds { + get { + if (spawnedBoxCollider != null) { BoxBounds currentBounds = new BoxBounds(); currentBounds.worldCenter = spawnedBoxCollider.transform.TransformPoint( @@ -100,9 +91,7 @@ public BoxBounds BoxBounds // Debug.Log($"world center: {boxBounds.worldCenter}"); // Debug.Log($"size: {boxBounds.size}"); // Debug.Log($"agentRelativeCenter: {boxBounds.agentRelativeCenter}"); - } - else - { + } else { // Debug.Log("why is it nullll"); return null; } @@ -117,8 +106,7 @@ public BoxBounds BoxBounds public FpinAgentController(BaseAgentComponent baseAgentComponent, AgentManager agentManager) : base(baseAgentComponent, agentManager) { } - public void Start() - { + public void Start() { //put stuff we need here when we need it maybe } @@ -129,8 +117,7 @@ public override RaycastHit[] CastBodyTrayectory( float moveMagnitude, int layerMask, CapsuleData cachedCapsule - ) - { + ) { Vector3 startPositionBoxCenter = startPosition + this.transform.TransformDirection(this.boxBounds.agentRelativeCenter); @@ -148,16 +135,14 @@ CapsuleData cachedCapsule //since the size returned by the box collider only reflects its size in local space DISREGARDING ANY TRANSFORM SCALES //IN ITS PARENTS OR ITSELF here we go - public Vector3 GetTrueSizeOfBoxCollider(BoxCollider collider) - { + public Vector3 GetTrueSizeOfBoxCollider(BoxCollider collider) { Vector3 trueSize = collider.size; // get the transform of the collider itself Transform currentTransform = collider.transform; // Apply the scale from the collider's transform and all parent transforms - while (currentTransform != null) - { + while (currentTransform != null) { trueSize.x *= currentTransform.localScale.x; trueSize.y *= currentTransform.localScale.y; trueSize.z *= currentTransform.localScale.z; @@ -170,29 +155,24 @@ public Vector3 GetTrueSizeOfBoxCollider(BoxCollider collider) } //override so we can access to fpin specific stuff - public override MetadataWrapper generateMetadataWrapper() - { + public override MetadataWrapper generateMetadataWrapper() { //get all the usual stuff from base agent's implementation MetadataWrapper metaWrap = base.generateMetadataWrapper(); //here's the fpin specific stuff - if (boxBounds != null) - { + if (boxBounds != null) { //get from BoxBounds as box world center will update as agent moves so we can't cache it metaWrap.agent.fpinColliderSize = BoxBounds.size; metaWrap.agent.fpinColliderWorldCenter = BoxBounds.worldCenter; metaWrap.agent.fpinColliderAgentRelativeCenter = BoxBounds.agentRelativeCenter; - } - else - { + } else { metaWrap.agent.fpinColliderSize = new Vector3(0, 0, 0); } return metaWrap; } - public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) - { + public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) { float minX = agentManager.SceneBounds.min.x; float minZ = agentManager.SceneBounds.min.z; float maxX = agentManager.SceneBounds.max.x; @@ -203,10 +183,8 @@ public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) int n = (int)Mathf.Ceil(Mathf.Sqrt(sampleCount)); List initPoints = new List(); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { initPoints.Add( new Vector3( Mathf.Lerp(minX, maxX, (i + 0.5f) / n), @@ -219,17 +197,14 @@ public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) initPoints.Shuffle_(); List pointsOnMesh = new List(); - for (int i = 0; i < initPoints.Count; i++) - { - if (pointsOnMesh.Count >= sampleCount) - { + for (int i = 0; i < initPoints.Count; i++) { + if (pointsOnMesh.Count >= sampleCount) { break; } NavMeshHit hit; Vector3 randomPoint = initPoints[i]; - if (NavMesh.SamplePosition(randomPoint, out hit, maxDistance, NavMesh.AllAreas)) - { + if (NavMesh.SamplePosition(randomPoint, out hit, maxDistance, NavMesh.AllAreas)) { # if UNITY_EDITOR Debug.DrawLine( hit.position, @@ -247,17 +222,14 @@ public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) return pointsOnMesh; } - public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) - { + public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) { List pointsOnMesh = SamplePointsOnNavMesh(n, maxDistance: maxDistance); - if (pointsOnMesh.Count == 0) - { + if (pointsOnMesh.Count == 0) { throw new InvalidOperationException("No points on the navmesh"); } Bounds b = UtilityFunctions.CreateEmptyBounds(); - foreach (Collider c in GetComponentsInChildren()) - { + foreach (Collider c in GetComponentsInChildren()) { b.Encapsulate(c.bounds); } @@ -266,10 +238,8 @@ public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) //Debug.Log($"yOffset is: {yOffset}"); bool success = false; - foreach (Vector3 point in pointsOnMesh) - { - try - { + foreach (Vector3 point in pointsOnMesh) { + try { //Debug.Log($"what is the point we are trying from the pointsOnMesh? {point:F8}"); teleportFull( position: point + new Vector3(0, yOffset, 0), @@ -279,9 +249,7 @@ public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) ); success = true; break; - } - catch (InvalidOperationException) - { + } catch (InvalidOperationException) { continue; } } @@ -296,8 +264,7 @@ public void spawnAgentBoxCollider( Bounds agentBounds, bool useVisibleColliderBase = false, bool spawnCollidersWithoutMesh = false - ) - { + ) { //create colliders based on the agent bounds var col = new GameObject("fpinCollider", typeof(BoxCollider)); col.layer = LayerMask.NameToLayer("Agent"); @@ -332,27 +299,22 @@ public void spawnAgentBoxCollider( //helper function to remove the currently generated agent box collider //make sure to follow this up with a subsequent generation so BoxBounds isn't left null - public void destroyAgentBoxCollider() - { + public void destroyAgentBoxCollider() { GameObject visibleBox = GameObject.Find("VisibleBox"); - if (spawnedBoxCollider != null) - { + if (spawnedBoxCollider != null) { UnityEngine.Object.DestroyImmediate(spawnedBoxCollider.transform.gameObject); spawnedBoxCollider = null; } - if (spawnedTriggerBoxCollider != null) - { + if (spawnedTriggerBoxCollider != null) { UnityEngine.Object.DestroyImmediate(spawnedTriggerBoxCollider.transform.gameObject); spawnedTriggerBoxCollider = null; } - if (visibleBox != null) - { + if (visibleBox != null) { UnityEngine.Object.DestroyImmediate(visibleBox); } #if UNITY_EDITOR GameObject visualizedBoxCollider = GameObject.Find("VisualizedBoxCollider"); - if (visualizedBoxCollider != null) - { + if (visualizedBoxCollider != null) { UnityEngine.Object.DestroyImmediate(visualizedBoxCollider); } #endif @@ -368,36 +330,30 @@ private Transform CopyMeshChildrenRecursive( Transform sourceTransform, Transform targetTransform, bool isTopMost = true - ) - { + ) { Transform thisTransform = null; - foreach (Transform child in sourceTransform) - { + foreach (Transform child in sourceTransform) { GameObject copiedChild = null; // Check if the child has a MeshFilter component MeshFilter meshFilter = child.GetComponent(); - if (meshFilter != null) - { + if (meshFilter != null) { copiedChild = CopyMeshToTarget(child, targetTransform); } // Process children only if necessary (i.e., they contain MeshFilters) - if (HasMeshInChildrenOrSelf(child)) - { + if (HasMeshInChildrenOrSelf(child)) { Transform parentForChildren = (copiedChild != null) ? copiedChild.transform : CreateContainerForHierarchy(child, targetTransform).transform; CopyMeshChildrenRecursive(child, parentForChildren, false); - if (isTopMost) - { + if (isTopMost) { thisTransform = parentForChildren; } } } - if (isTopMost) - { + if (isTopMost) { // Set up intermediate object for scaling the mesh in agent-space (since the top-level mesh could be rotated, potentially introducing a complex matrix of scales) GameObject agentScaleObject = new GameObject("AgentScaleObject"); agentScaleObject.transform.position = thisTransform.position; @@ -409,8 +365,7 @@ private Transform CopyMeshChildrenRecursive( return null; } - private GameObject CopyMeshToTarget(Transform child, Transform targetParent) - { + private GameObject CopyMeshToTarget(Transform child, Transform targetParent) { // Create a new GameObject and copy components GameObject copiedChild = new GameObject(child.name); copiedChild.transform.SetParent(targetParent); @@ -420,8 +375,7 @@ private GameObject CopyMeshToTarget(Transform child, Transform targetParent) copiedMeshFilter.mesh = meshFilter.mesh; MeshRenderer sourceMeshRenderer = child.GetComponent(); - if (sourceMeshRenderer != null) - { + if (sourceMeshRenderer != null) { MeshRenderer copiedMeshRenderer = copiedChild.AddComponent(); copiedMeshRenderer.sharedMaterials = sourceMeshRenderer.sharedMaterials; } @@ -433,26 +387,21 @@ private GameObject CopyMeshToTarget(Transform child, Transform targetParent) return copiedChild; } - private bool HasMeshInChildrenOrSelf(Transform transform) - { - foreach (Transform child in transform) - { - if (child.GetComponent() != null || HasMeshInChildrenOrSelf(child)) - { + private bool HasMeshInChildrenOrSelf(Transform transform) { + foreach (Transform child in transform) { + if (child.GetComponent() != null || HasMeshInChildrenOrSelf(child)) { return true; } } - if (transform.GetComponent() != null) - { + if (transform.GetComponent() != null) { return true; } return false; } - private GameObject CreateContainerForHierarchy(Transform child, Transform targetParent) - { + private GameObject CreateContainerForHierarchy(Transform child, Transform targetParent) { GameObject container = new GameObject(child.name + "_Container"); container.transform.SetParent(targetParent); container.transform.localPosition = child.localPosition; @@ -464,26 +413,22 @@ private GameObject CreateContainerForHierarchy(Transform child, Transform target private HashSet TransformedMeshRendererVertices( MeshRenderer mr, bool returnFirstVertexOnly = false - ) - { + ) { MeshFilter mf = mr.gameObject.GetComponent(); Matrix4x4 localToWorld = mr.transform.localToWorldMatrix; HashSet vertices = new HashSet(mf.sharedMesh.vertices); HashSet transformedVertices = new HashSet(); - foreach (Vector3 vertex in vertices) - { + foreach (Vector3 vertex in vertices) { transformedVertices.Add(localToWorld.MultiplyPoint3x4(vertex)); } return transformedVertices; } - public ActionFinished GetBoxBounds() - { + public ActionFinished GetBoxBounds() { return new ActionFinished() { success = true, actionReturn = this.BoxBounds }; } - public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) - { + public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) { // Move the point to the pivot's origin Vector3 dir = point - pivot; // Rotate it @@ -495,31 +440,23 @@ public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angl public ActionFinished BackwardsCompatibleInitialize( BackwardsCompatibleInitializeParams args - ) - { + ) { Debug.Log("RUNNING BackCompatInitialize from FpinAgentController.cs"); // limit camera from looking too far down/up //default max are 30 up and 60 down, different agent types may overwrite this - if (Mathf.Approximately(args.maxUpwardLookAngle, 0.0f)) - { + if (Mathf.Approximately(args.maxUpwardLookAngle, 0.0f)) { this.maxUpwardLookAngle = 30f; - } - else - { + } else { this.maxUpwardLookAngle = args.maxUpwardLookAngle; } - if (Mathf.Approximately(args.maxDownwardLookAngle, 0.0f)) - { + if (Mathf.Approximately(args.maxDownwardLookAngle, 0.0f)) { this.maxDownwardLookAngle = 60f; - } - else - { + } else { this.maxDownwardLookAngle = args.maxDownwardLookAngle; } - if (args.antiAliasing != null) - { + if (args.antiAliasing != null) { agentManager.updateAntiAliasing( postProcessLayer: m_Camera.gameObject.GetComponentInChildren(), antiAliasing: args.antiAliasing @@ -527,77 +464,62 @@ BackwardsCompatibleInitializeParams args } // m_Camera.GetComponent().SwitchRenderersToHide(this.VisibilityCapsule); - if (args.gridSize == 0) - { + if (args.gridSize == 0) { args.gridSize = 0.25f; } // note: this overrides the default FOV values set in InitializeBody() - if (args.fieldOfView > 0 && args.fieldOfView < 180) - { + if (args.fieldOfView > 0 && args.fieldOfView < 180) { m_Camera.fieldOfView = args.fieldOfView; - } - else if (args.fieldOfView < 0 || args.fieldOfView >= 180) - { + } else if (args.fieldOfView < 0 || args.fieldOfView >= 180) { errorMessage = "fov must be set to (0, 180) noninclusive."; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.cameraNearPlane > 0) - { + if (args.cameraNearPlane > 0) { m_Camera.nearClipPlane = args.cameraNearPlane; } - if (args.cameraFarPlane > 0) - { + if (args.cameraFarPlane > 0) { m_Camera.farClipPlane = args.cameraFarPlane; } - if (args.timeScale > 0) - { - if (Time.timeScale != args.timeScale) - { + if (args.timeScale > 0) { + if (Time.timeScale != args.timeScale) { Time.timeScale = args.timeScale; } - } - else - { + } else { errorMessage = "Time scale must be > 0"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.rotateStepDegrees <= 0.0) - { + if (args.rotateStepDegrees <= 0.0) { errorMessage = "rotateStepDegrees must be a non-zero, non-negative float"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } // default is 90 defined in the ServerAction class, specify whatever you want the default to be - if (args.rotateStepDegrees > 0.0) - { + if (args.rotateStepDegrees > 0.0) { this.rotateStepDegrees = args.rotateStepDegrees; } - if (args.snapToGrid && !ValidRotateStepDegreesWithSnapToGrid(args.rotateStepDegrees)) - { + if (args.snapToGrid && !ValidRotateStepDegreesWithSnapToGrid(args.rotateStepDegrees)) { errorMessage = $"Invalid values 'rotateStepDegrees': ${args.rotateStepDegrees} and 'snapToGrid':${args.snapToGrid}. 'snapToGrid': 'True' is not supported when 'rotateStepDegrees' is different from grid rotation steps of 0, 90, 180, 270 or 360."; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.maxDownwardLookAngle < 0) - { + if (args.maxDownwardLookAngle < 0) { errorMessage = "maxDownwardLookAngle must be a non-negative float"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.maxUpwardLookAngle < 0) - { + if (args.maxUpwardLookAngle < 0) { errorMessage = "maxUpwardLookAngle must be a non-negative float"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); @@ -610,21 +532,18 @@ BackwardsCompatibleInitializeParams args || args.renderSemanticSegmentation || args.renderInstanceSegmentation || args.renderNormalsImage - ) - { + ) { this.updateImageSynthesis(true); } - if (args.visibilityDistance > 0.0f) - { + if (args.visibilityDistance > 0.0f) { this.maxVisibleDistance = args.visibilityDistance; } var navmeshAgent = this.GetComponentInChildren(); var collider = this.GetComponent(); - if (collider != null && navmeshAgent != null) - { + if (collider != null && navmeshAgent != null) { navmeshAgent.radius = collider.radius; navmeshAgent.height = collider.height; navmeshAgent.transform.localPosition = new Vector3( @@ -636,14 +555,11 @@ BackwardsCompatibleInitializeParams args // navmeshAgent.radius = - if (args.gridSize <= 0 || args.gridSize > 5) - { + if (args.gridSize <= 0 || args.gridSize > 5) { errorMessage = "grid size must be in the range (0,5]"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); - } - else - { + } else { gridSize = args.gridSize; // Don't know what this was for @@ -666,8 +582,7 @@ BackwardsCompatibleInitializeParams args return new ActionFinished( success: true, - actionReturn: new InitializeReturn - { + actionReturn: new InitializeReturn { cameraNearPlane = m_Camera.nearClipPlane, cameraFarPlane = m_Camera.farClipPlane } @@ -682,8 +597,7 @@ public ActionFinished Initialize( Vector3? colliderScaleRatio = null, bool useAbsoluteSize = false, bool useVisibleColliderBase = false - ) - { + ) { this.visibilityScheme = VisibilityScheme.Distance; var actionFinished = this.InitializeBody( bodyAsset: bodyAsset, @@ -705,16 +619,14 @@ public ActionFinished InitializeBody( Vector3? colliderScaleRatio = null, bool useAbsoluteSize = false, bool useVisibleColliderBase = false - ) - { + ) { // if using no source body mesh, we default to using absolute size via the colliderScaleRatio // since a non absolute size doesn't make sense if we have no default mesh size to base the scale // ratio on Vector3 meshScaleRatio = colliderScaleRatio.GetValueOrDefault(Vector3.one); bool noMesh = false; - if (bodyAsset == null) - { + if (bodyAsset == null) { useAbsoluteSize = true; noMesh = true; } @@ -729,21 +641,18 @@ public ActionFinished InitializeBody( //remove any old copied meshes or generated colliders from previous fpin agent now destroyAgentBoxCollider(); - if (fpinVisibilityCapsule != null) - { + if (fpinVisibilityCapsule != null) { UnityEngine.Object.DestroyImmediate(fpinVisibilityCapsule); } var spawnAssetActionFinished = new ActionFinished(); Bounds meshBoundsWorld = new Bounds(this.transform.position, Vector3.zero); - if (bodyAsset != null) - { + if (bodyAsset != null) { //spawn in a default mesh in an out-of-the-way location (currently 200,200,200) to base the new bounds on spawnAssetActionFinished = spawnBodyAsset(bodyAsset, out GameObject spawnedMesh); // Return early if spawn failed - if (!spawnAssetActionFinished.success) - { + if (!spawnAssetActionFinished.success) { return spawnAssetActionFinished; } @@ -759,18 +668,13 @@ public ActionFinished InitializeBody( // so we'll take the bounds-center of the first meshRenderer MeshRenderer[] meshRenderers = topMeshTransform.gameObject.GetComponentsInChildren(); - foreach (MeshRenderer mr in meshRenderers) - { + foreach (MeshRenderer mr in meshRenderers) { // No need to run TransformedMeshRendererVertices if the meshRenderer's GameObject isn't rotated - if (mr.transform.eulerAngles.magnitude < 1e-4f) - { + if (mr.transform.eulerAngles.magnitude < 1e-4f) { meshBoundsWorld.Encapsulate(mr.bounds); - } - else - { + } else { HashSet vertices = TransformedMeshRendererVertices(mr); - foreach (Vector3 vertex in vertices) - { + foreach (Vector3 vertex in vertices) { meshBoundsWorld.Encapsulate(vertex); } } @@ -781,17 +685,14 @@ public ActionFinished InitializeBody( topMeshTransform.position = meshBoundsWorld.center; topMeshTransform.GetChild(0).position = currentTopMeshTransformChildPos; - if (useAbsoluteSize) - { + if (useAbsoluteSize) { topMeshTransform.localScale = new Vector3( meshScaleRatio.x / meshBoundsWorld.size.x, meshScaleRatio.y / meshBoundsWorld.size.y, meshScaleRatio.z / meshBoundsWorld.size.z ); meshBoundsWorld.size = meshScaleRatio; - } - else - { + } else { topMeshTransform.localScale = meshScaleRatio; meshBoundsWorld.size = new Vector3( meshScaleRatio.x * meshBoundsWorld.size.x, @@ -810,18 +711,14 @@ public ActionFinished InitializeBody( this.transform.position + Vector3.up * meshBoundsWorld.extents.y; // remove the spawned mesh cause we are done with it - foreach (var sop in spawnedMesh.GetComponentsInChildren()) - { + foreach (var sop in spawnedMesh.GetComponentsInChildren()) { agentManager.physicsSceneManager.RemoveFromObjectsInScene(sop); } - if (spawnedMesh.activeInHierarchy) - { + if (spawnedMesh.activeInHierarchy) { UnityEngine.Object.DestroyImmediate(spawnedMesh); } - } - else - { + } else { meshBoundsWorld = new Bounds( this.transform.position + (Vector3.up * meshScaleRatio.y / 2), meshScaleRatio @@ -836,8 +733,7 @@ public ActionFinished InitializeBody( // set reference to the meshes so the base agent and fpin agent are happy VisibilityCapsule = fpinVisibilityCapsule = viscap; - if (topMeshTransform != null) - { + if (topMeshTransform != null) { topMeshTransform.SetParent(viscap.transform); } @@ -853,8 +749,7 @@ public ActionFinished InitializeBody( ); // spawn the visible collider base if we need to - if (useVisibleColliderBase) - { + if (useVisibleColliderBase) { GameObject visibleBase = GameObject.CreatePrimitive(PrimitiveType.Cube); visibleBase.name = "visibleBase"; visibleBase.GetComponent().enabled = false; @@ -966,8 +861,7 @@ public ActionFinished InitializeBody( originalRotation, checkBoxLayerMask ) - ) - { + ) { this.transform.position = originalPosition; this.transform.rotation = originalRotation; string error = @@ -989,10 +883,8 @@ public ActionFinished InitializeBody( fpinMovable = new FpinMovableContinuous(this.GetComponentInParent()); // we had a body asset used, so actionFinished returns info related to that - if (bodyAsset != null) - { - return new ActionFinished(spawnAssetActionFinished) - { + if (bodyAsset != null) { + return new ActionFinished(spawnAssetActionFinished) { success = true, // TODO: change to a proper class once metadata return is defined actionReturn = new Dictionary() @@ -1007,9 +899,7 @@ spawnAssetActionFinished.actionReturn as ObjectSphereBounds { "cameraFarPlane", m_Camera.farClipPlane } } }; - } - else - { + } else { return new ActionFinished( success: true, // TODO: change to a proper class once metadata return is defined @@ -1023,18 +913,14 @@ spawnAssetActionFinished.actionReturn as ObjectSphereBounds } } - private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawnedMesh) - { - if (bodyAsset == null) - { + private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawnedMesh) { + if (bodyAsset == null) { throw new ArgumentNullException("bodyAsset is null"); - } - else if ( - bodyAsset.assetId == null - && bodyAsset.dynamicAsset == null - && bodyAsset.asset == null - ) - { + } else if ( + bodyAsset.assetId == null + && bodyAsset.dynamicAsset == null + && bodyAsset.asset == null + ) { throw new ArgumentNullException( "`bodyAsset.assetId`, `bodyAsset.dynamicAsset` or `bodyAsset.asset` must be provided all are null." ); @@ -1048,8 +934,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne if ( (bodyAsset.dynamicAsset != null || bodyAsset.asset != null) && bodyAsset.assetId == null - ) - { + ) { var id = bodyAsset.dynamicAsset != null ? bodyAsset.dynamicAsset.id @@ -1057,24 +942,20 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne var assetMap = ProceduralTools.getAssetMap(); // Check if asset is in AssetDatabase already - if (assetMap.ContainsKey(id)) - { + if (assetMap.ContainsKey(id)) { Debug.Log("------- Already contains key"); bodyAsset.assetId = id; } } - if (bodyAsset.assetId != null) - { + if (bodyAsset.assetId != null) { actionFinished = SpawnAsset( bodyAsset.assetId, "agentMesh", new Vector3(200f, 200f, 200f) ); spawnedMesh = GameObject.Find("agentMesh"); - } - else if (bodyAsset.dynamicAsset != null) - { + } else if (bodyAsset.dynamicAsset != null) { actionFinished = this.CreateRuntimeAsset( id: bodyAsset.dynamicAsset.id, dir: bodyAsset.dynamicAsset.dir, @@ -1083,9 +964,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne serializable: true ); spawnedMesh = GameObject.Find("mesh"); - } - else if (bodyAsset.asset != null) - { + } else if (bodyAsset.asset != null) { bodyAsset.asset.serializable = true; actionFinished = this.CreateRuntimeAsset(asset: bodyAsset.asset); } @@ -1093,8 +972,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne if ( bodyAsset.assetId == null && (bodyAsset.dynamicAsset != null || bodyAsset.asset != null) - ) - { + ) { var id = bodyAsset.dynamicAsset != null ? bodyAsset.dynamicAsset.id @@ -1102,8 +980,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne Debug.Log( $"-- checks {bodyAsset.assetId == null} {bodyAsset.dynamicAsset != null} {bodyAsset.asset != null} " ); - if (!actionFinished.success || actionFinished.actionReturn == null) - { + if (!actionFinished.success || actionFinished.actionReturn == null) { return new ActionFinished( success: false, errorMessage: $"Could not create asset `{bodyAsset.dynamicAsset}` error: {actionFinished.errorMessage}" @@ -1116,8 +993,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne return actionFinished; } - protected override LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisible = false) - { + protected override LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisible = false) { // No agent because camera can be in the path of colliders string[] layers = new string[] { @@ -1127,8 +1003,7 @@ protected override LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisi "Procedural3", "Procedural0" //, "Agent" }; - if (withSimObjInvisible) - { + if (withSimObjInvisible) { layers = layers.Append("SimObjInvisible").ToArray(); } return LayerMask.GetMask(layers); @@ -1141,8 +1016,7 @@ public override void TeleportFull( float? horizon = null, bool? standing = null, bool forceAction = false - ) - { + ) { teleportFull( position: position, rotation: rotation, @@ -1156,8 +1030,7 @@ protected override void teleportFull( Vector3? rotation, float? horizon, bool forceAction - ) - { + ) { //Debug.Log($"what even is the position passed in at the start? {position:F8}"); if ( rotation.HasValue @@ -1165,8 +1038,7 @@ bool forceAction !Mathf.Approximately(rotation.Value.x, 0f) || !Mathf.Approximately(rotation.Value.z, 0f) ) - ) - { + ) { throw new ArgumentOutOfRangeException( "No agents currently can change in pitch or roll. So, you must set rotation(x=0, y=yaw, z=0)." + $" You gave {rotation.Value.ToString("F6")}." @@ -1178,8 +1050,7 @@ bool forceAction !forceAction && horizon.HasValue && (horizon.Value > maxDownwardLookAngle || horizon.Value < -maxUpwardLookAngle) - ) - { + ) { throw new ArgumentOutOfRangeException( $"Each horizon must be in [{-maxUpwardLookAngle}:{maxDownwardLookAngle}]. You gave {horizon}." ); @@ -1189,15 +1060,13 @@ bool forceAction !forceAction && position.HasValue && !agentManager.SceneBounds.Contains(position.Value) - ) - { + ) { throw new ArgumentOutOfRangeException( $"Teleport position {position.Value.ToString("F6")} out of scene bounds! Ignore this by setting forceAction=true." ); } - if (!forceAction && position.HasValue && !isPositionOnGrid(position.Value)) - { + if (!forceAction && position.HasValue && !isPositionOnGrid(position.Value)) { throw new ArgumentOutOfRangeException( $"Teleport position {position.Value.ToString("F6")} is not on the grid of size {gridSize}." ); @@ -1226,15 +1095,13 @@ bool forceAction targetPosition: position.GetValueOrDefault(transform.position) ); - if (!forceAction) - { + if (!forceAction) { if ( isAgentCapsuleColliding( collidersToIgnore: collidersToIgnoreDuringMovement, includeErrorMessage: true ) - ) - { + ) { transform.position = oldPosition; transform.rotation = oldRotation; autoSyncTransforms(); @@ -1248,8 +1115,7 @@ bool forceAction collidersToIgnore: collidersToIgnoreDuringMovement, includeErrorMessage: true ) - ) - { + ) { transform.position = oldPosition; transform.rotation = oldRotation; autoSyncTransforms(); @@ -1261,11 +1127,9 @@ bool forceAction actionFinished(success: true); } - protected override void assertTeleportedNearGround(Vector3? targetPosition) - { + protected override void assertTeleportedNearGround(Vector3? targetPosition) { // position should not change if it's null. - if (targetPosition == null) - { + if (targetPosition == null) { return; } @@ -1280,8 +1144,7 @@ protected override void assertTeleportedNearGround(Vector3? targetPosition) autoSyncTransforms(); // perhaps like y=2 was specified, with an agent's standing height of 0.9 - if (Mathf.Abs(transform.position.y - pos.y) > 1.0f) - { + if (Mathf.Abs(transform.position.y - pos.y) > 1.0f) { throw new InvalidOperationException( "After teleporting and adjusting agent position to floor, there was too large a change." + " This may be due to the target teleport coordinates causing the agent to fall through the floor." @@ -1295,10 +1158,8 @@ public IEnumerator MoveAgent( float ahead = 0, float right = 0, float speed = 1 - ) - { - if (ahead == 0 && right == 0) - { + ) { + if (ahead == 0 && right == 0) { throw new ArgumentException("Must specify ahead or right!"); } Vector3 direction = new Vector3(x: right, y: 0, z: ahead); @@ -1326,8 +1187,7 @@ public IEnumerator MoveAhead( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( ahead: moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1339,8 +1199,7 @@ public IEnumerator MoveBack( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( ahead: -moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1352,8 +1211,7 @@ public IEnumerator MoveRight( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( right: moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1365,8 +1223,7 @@ public IEnumerator MoveLeft( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( right: -moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1378,8 +1235,7 @@ public IEnumerator RotateRight( float? degrees = null, float speed = 1.0f, bool returnToStart = true - ) - { + ) { return RotateAgent( degrees: degrees.GetValueOrDefault(rotateStepDegrees), speed: speed, @@ -1391,8 +1247,7 @@ public IEnumerator RotateLeft( float? degrees = null, float speed = 1.0f, bool returnToStart = true - ) - { + ) { return RotateAgent( degrees: -degrees.GetValueOrDefault(rotateStepDegrees), speed: speed, @@ -1404,8 +1259,7 @@ public virtual IEnumerator RotateAgent( float degrees, float speed = 1.0f, bool returnToStart = true - ) - { + ) { CollisionListener collisionListener = fpinMovable.collisionListener; collisionListener.Reset(); diff --git a/unity/Assets/Scripts/ProceduralTools.cs b/unity/Assets/Scripts/ProceduralTools.cs index 95eb72b1af..9d779f1f86 100644 --- a/unity/Assets/Scripts/ProceduralTools.cs +++ b/unity/Assets/Scripts/ProceduralTools.cs @@ -1386,7 +1386,8 @@ public static GameObject CreateHouse( if (!validateHouseObjects(getAssetMap(), house.objects, missingIds)) { throw new ArgumentException( $"Object ids '{string.Join(", ", missingIds)}' not present in asset database." - + $" If it is a procedural asset make sure you call 'CreateAsset' before 'CreateHouse'" + + $" If it is a procedural asset make sure you call 'CreateAsset' before 'CreateHouse'." + + $"See https://ai2thor.allenai.org/ithor/documentation/objects/procedural-assets" ); } From 803cbba8c6daca15e7459f254b9432a0c1b7f7d2 Mon Sep 17 00:00:00 2001 From: AlvaroHG Date: Thu, 3 Oct 2024 14:01:04 -0700 Subject: [PATCH 2/2] Deprecate ChangeFOV --- .../PhysicsRemoteFPSAgentController.cs | 44 ++----------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/unity/Assets/Scripts/PhysicsRemoteFPSAgentController.cs b/unity/Assets/Scripts/PhysicsRemoteFPSAgentController.cs index a827d8200a..b37962eecf 100644 --- a/unity/Assets/Scripts/PhysicsRemoteFPSAgentController.cs +++ b/unity/Assets/Scripts/PhysicsRemoteFPSAgentController.cs @@ -6113,47 +6113,9 @@ public void RotateUniverseAroundAgent(ServerAction action) { } public void ChangeFOV(float fieldOfView, string camera = "") { - if (fieldOfView > 0 && fieldOfView < 180) { - if (string.IsNullOrEmpty(camera)) { - m_Camera.fieldOfView = fieldOfView; - actionFinished(true); - } else { - var cameraTuples = new List<(Camera camera, bool isThirdPartyCamera, int id)>() - { - (camera: m_Camera, isThirdPartyCamera: false, id: -1) - }.Concat( - this.agentManager.thirdPartyCameras.Select( - (c, i) => (camera: c, isThirdPartyCamera: true, id: i) - ) - ); - var matches = cameraTuples; - if (camera != "*") { - matches = cameraTuples.Where(t => t.camera.name == camera); - } - // Debug.Log($"Camera matches: {matches.Count()} {string.Join(", ", matches.Select(m => m.camera.name))}"); - if (matches.Count() == 0) { - errorMessage = - $"Camera '{camera}' is not present in the agent, make sure the agent was initialized correctly or camera was added via 'AddThirdPartyCamera'."; - actionFinished(false); - } else { - foreach (var tuple in matches) { - if (tuple.isThirdPartyCamera) { - agentManager.UpdateThirdPartyCamera( - tuple.id, - fieldOfView: fieldOfView - ); - } else { - tuple.camera.fieldOfView = fieldOfView; - } - } - actionFinished(true); - } - } - } else { - errorMessage = "fov must be in (0, 180) noninclusive."; - Debug.Log(errorMessage); - actionFinished(false); - } + throw new InvalidOperationException( + "This action is deprecated. Use `UpdateMainCamera` or `UpdateThirdPartyCamera` instead." + ); } // in case you want to change the timescale