Skip to content

Commit

Permalink
Merge pull request #99 from specklesystems/jrm/unity/speckle-properti…
Browse files Browse the repository at this point in the history
…es-example

Added manual speckle properties example
  • Loading branch information
JR-Morgan authored Sep 12, 2023
2 parents 106d4c8 + 5dd889c commit 0bb1591
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 12 deletions.
62 changes: 62 additions & 0 deletions Assets/Extra/AttachSpecklePropertiesExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections;
using System.Collections.Generic;
using Objects.Converter.Unity;
using Speckle.ConnectorUnity.Utils;
using Speckle.ConnectorUnity.Wrappers;
using Speckle.Core.Api;
using Speckle.Core.Credentials;
using Speckle.Core.Models;
using Speckle.Core.Transports;
using UnityEngine;

/// <summary>
/// Example script for grabbing speckle properties for a specific object "on-the-fly"
/// </summary>
/// <remarks>
/// see discussion https://speckle.community/t/reloading-assemblies-takes-too-long/6708
/// </remarks>
[AddComponentMenu("Speckle/Extras/" + nameof(AttachSpecklePropertiesExample))]
public class AttachSpecklePropertiesExample : MonoBehaviour
{
public string streamId;
public string objectId;

public virtual void Start()
{
Client speckleClient = new(AccountManager.GetDefaultAccount());

StartCoroutine(AttachSpeckleProperties(speckleClient, streamId, objectId));
}

public IEnumerator AttachSpeckleProperties(
Client speckleClient,
string streamId,
string objectId
)
{
//Fetch the object from Speckle
ServerTransport remoteTransport = new(speckleClient.Account, streamId);
Utils.WaitForTask<Base> operation =
new(async () => await Operations.Receive(objectId, remoteTransport));

//yield until task completes
yield return operation;
Base speckleObject = operation.Result;

//Do something with the properties. e.g. attach SpeckleProperties component
DoSomething(speckleObject);
}

protected virtual void DoSomething(Base speckleObject)
{
//GetProperties will filter "useful" properties
Dictionary<string, object> properties = ConverterUnity.GetProperties(
speckleObject,
typeof(SpeckleObject)
);

var sd = this.gameObject.AddComponent<SpeckleProperties>();
sd.Data = properties;
sd.SpeckleType = speckleObject.GetType();
}
}
11 changes: 11 additions & 0 deletions Assets/Extra/AttachSpecklePropertiesExample.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions Assets/Extra/Speckle.Extra.asmdef
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
{
"name": "Speckle.Extra",
"references":[ "GUID:eed1b8b83e2c0074d9e5de2348e3ff72", "GUID:e6adfdc4e436206479f48eafc82f32b5", "GUID:d274441ecc3eb3f43b093eec1503d681", "GUID:50d889142fdf9de4b8501c6eaa4b3225" ]
}
"name": "Speckle.Extra",
"rootNamespace": "",
"references": [
"GUID:eed1b8b83e2c0074d9e5de2348e3ff72",
"GUID:e6adfdc4e436206479f48eafc82f32b5",
"GUID:d274441ecc3eb3f43b093eec1503d681",
"GUID:50d889142fdf9de4b8501c6eaa4b3225",
"GUID:7383cd71541a2aa48a7baf23f74b4d5f"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Speckle.ConnectorUnity
/// that handles conversions and subscriptions for you
/// </summary>
[RequireComponent(typeof(RecursiveConverter))]
[Obsolete]
[Obsolete("See " + nameof(SpeckleReceiver))]
public class Receiver : MonoBehaviour
{
public string StreamId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Speckle.ConnectorUnity
/// that handles conversions for you
/// </summary>
[RequireComponent(typeof(RecursiveConverter)), ExecuteAlways]
[Obsolete]
[Obsolete("See " + nameof(SpeckleSender))]
public class Sender : MonoBehaviour
{
private ServerTransport transport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"GUID:eed1b8b83e2c0074d9e5de2348e3ff72",
"GUID:13aec21e8e96f864bafd00df49f225fc",
"GUID:d274441ecc3eb3f43b093eec1503d681",
"GUID:50d889142fdf9de4b8501c6eaa4b3225"
"GUID:50d889142fdf9de4b8501c6eaa4b3225",
"GUID:7383cd71541a2aa48a7baf23f74b4d5f"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ public async Task<Base> ReceiveAsync(CancellationToken cancellationToken)
return result;
}

/// <summary>
/// Gets the current selection
/// </summary>
/// <param name="client">The selected Account's Client</param>
/// <param name="stream">The selected <see cref="Stream"/></param>
/// <param name="commit">The selected <see cref="Commit"/></param>
/// <exception cref="InvalidOperationException">Selection was not complete or invalid</exception>
public void ValidateSelection(out Client client, out Stream stream, out Commit commit)
{
Client? selectedClient = Account.Client;
Expand Down Expand Up @@ -237,7 +244,7 @@ protected internal void FinishOperation()
/// <param name="onTotalChildrenCountKnown"></param>
/// <param name="cancellationToken"></param>
/// <exception cref="Exception">Throws various types of exceptions to indicate faliure</exception>
/// <returns></returns>
/// <returns>The requested Speckle object</returns>
public static async Task<Base> ReceiveAsync(
Client client,
string streamId,
Expand Down Expand Up @@ -301,7 +308,7 @@ public static async Task<Base> ReceiveAsync(
{ "hostPlatform", Application.platform.ToString() },
{
"isMultiplayer",
commit != null && commit.authorId != client.Account.userInfo.id
commit?.authorId != null && commit?.authorId != client.Account?.userInfo?.id
},
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,24 @@ public virtual Point PointToSpeckle(Vector3 p)
return go;
}

public Dictionary<string, object?> GetProperties(Base o) => GetProperties(o, typeof(Base));
/// <summary>Gets all properties of <paramref name="o"/> except ignored ones. <br/>
/// Ignored properties include properties of <see cref="Base"/>
/// And some hardcoded ones that are likely to be converted (such as material, elements, and name)
/// </summary>
/// <param name="o">The speckle object to grab properties from</param>
/// <returns>The properties</returns>
/// <remarks>If you don't want to filter any properties, simply use <see cref="Base.GetMembers"/></remarks>
public static Dictionary<string, object?> GetProperties(Base o) =>
GetProperties(o, typeof(Base));

public Dictionary<string, object?> GetProperties(Base o, Type excludeType)
/// <summary>
/// Gets all properties of <paramref name="o"/> except ignored ones.<br/>
/// Ignored properties include properties of <paramref name="excludeType"/>
/// And some hardcoded ones that are likely to be converted (such as material, elements, and name)
/// </summary>
/// <inheritdoc cref="GetProperties(Base)"/>
/// <param name="excludeType">A <see cref="Type"/> whose properties should be ignored</param>
public static Dictionary<string, object?> GetProperties(Base o, Type excludeType)
{
var excludeProps = new HashSet<string>(
excludeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public Base ConvertGameObjectToSpeckle(GameObject go)
}
}

public IList<string> DisplayValuePropertyAliases { get; set; } =
public static IList<string> DisplayValuePropertyAliases { get; set; } =
new[] { "displayValue", "@displayValue", "displayMesh", "@displayMesh" };

public GameObject? DisplayValueToNative(Base @object)
Expand Down
2 changes: 1 addition & 1 deletion Packages/systems.speckle.speckle-unity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "systems.speckle.speckle-unity",
"version": "2.15.2",
"version": "2.15.3",
"displayName": "Speckle Unity Connector",
"description": "AEC Interoperability for Unity through Speckle",
"unity": "2021.1",
Expand Down

0 comments on commit 0bb1591

Please sign in to comment.