Beispiel #1
0
        /// <summary>
        /// Destroy the shared memory space and temporary scene objects
        /// </summary>
        public void Teardown()
        {
            if (!IsRunning)
            {
                return;
            }

            IsRunning = false;

            AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeAssemblyReload;
            EditorApplication.update        -= OnEditorUpdate;
            SceneManager.activeSceneChanged -= OnSceneUnloaded;
            EditorSceneManager.activeSceneChangedInEditMode -= OnSceneUnloaded;

            // Notify Blender if we're shutting down from the Unity side
            if (IsConnected)
            {
                messages.WriteDisconnect();
                OnDisconnectFromBlender();
            }
            else
            {
                // Still clear if we're not connected - we might've torn down
                // the connection uncleanly and have some persisted data to clean up.
                Clear();
            }

            // Dispose shared memory
            messages?.Dispose();
            messages = null;

            pixelsProducer?.Dispose();
            pixelsProducer = null;
        }
Beispiel #2
0
        /// <summary>
        /// Destroy the shared memory space and temporary scene objects
        /// </summary>
        public void Teardown()
        {
            AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeAssemblyReload;
            EditorApplication.update -= OnEditorUpdate;

            // Notify Blender if we're shutting down from the Unity side
            if (IsConnected)
            {
                messages.WriteDisconnect();
                OnDisconnectFromBlender();
            }
            else
            {
                // Still clear if we're not connected - we might've torn down
                // the connection uncleanly and have some persisted data to clean up.
                Clear();
            }

            IsSetup = false;

            Debug.Log("Tearing down shared memory space");

            // Dispose shared memory
            messages?.Dispose();
            messages = null;

            pixelsProducer?.Dispose();
            pixelsProducer = null;
        }
Beispiel #3
0
        /// <summary>
        /// Create a shared memory space for Blender to connect to.
        /// </summary>
        public bool Setup()
        {
            gameObject.transform.parent = null;

            if (IsRunning)
            {
                return(true);
            }

            IsRunning = true;

            var settings = CoherenceSettings.Instance;
            var name     = settings.bufferName;

            try
            {
                messages = new InteropMessenger();

                messages.ConnectAsMaster(
                    name + BLENDER_MESSAGES_BUFFER,
                    name + UNITY_MESSAGES_BUFFER,
                    settings.nodeCount,
                    settings.nodeSize
                    );
            }
            catch (Exception e)
            {
                // TODO: This is an IOException - which isn't as useful as
                // a FileNotFoundException that we'd get from Blender's side of things.
                // We could parse out the string to customize the error (e.g. instructions
                // on turning off Blender's side of things) but... feels hacky?
                Debug.LogError($"Failed to setup messaging: {e}");
                throw;
            }

            try
            {
                pixelsProducer = new CircularBuffer(
                    name + VIEWPORT_IMAGE_BUFFER,
                    settings.pixelsNodeCount,
                    settings.PixelsNodeSizeBytes
                    );
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to setup pixels producer: {e}");
                throw;
            }

            AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
            EditorApplication.update        += OnEditorUpdate;
            SceneManager.activeSceneChanged += OnSceneUnloaded;
            EditorSceneManager.activeSceneChangedInEditMode += OnSceneUnloaded;

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Dispose shared memory and shutdown communication to Unity.
        ///
        /// Anything from Unity's side would get cleaned up.
        /// </summary>
        public void Disconnect()
        {
            messages?.WriteDisconnect();

            IsConnectedToUnity        = false;
            IsConnectedToSharedMemory = false;

            messages?.Dispose();
            messages = null;

            pixelsConsumer?.Dispose();
            pixelsConsumer = null;
        }
Beispiel #5
0
        /// <summary>
        /// Connect to a shared memory space hosted by Unity and sync scene data
        /// </summary>
        /// <param name="connectionName">Common name for the shared memory space between Blender and Unity</param>
        public bool Connect(string connectionName, string versionInfo)
        {
            blenderState = new InteropBlenderState
            {
                version = versionInfo
            };

            try
            {
                InteropLogger.Debug($"Connecting to `{connectionName + VIEWPORT_IMAGE_BUFFER}`");

                // Buffer for render data coming from Unity (consume-only)
                pixelsConsumer = new CircularBuffer(connectionName + VIEWPORT_IMAGE_BUFFER);

                InteropLogger.Debug($"Connecting to `{connectionName + UNITY_MESSAGES_BUFFER}` and `{connectionName + BLENDER_MESSAGES_BUFFER}`");

                // Two-way channel between Blender and Unity
                messages = new InteropMessenger();
                messages.ConnectAsSlave(
                    connectionName + UNITY_MESSAGES_BUFFER,
                    connectionName + BLENDER_MESSAGES_BUFFER
                    );
            }
            catch (System.IO.FileNotFoundException)
            {
                // Shared memory space is not valid - Unity may not have started it.
                // This is an error that should be gracefully handled by the UI.
                IsConnectedToSharedMemory = false;
                return(false);
            }

            IsConnectedToSharedMemory = true;

            // Send an initial connect message to let Unity know we're in
            messages.Queue(RpcRequest.Connect, versionInfo, ref blenderState);

            return(true);
        }