This project is read-only.

Issue with model attachments.

Sep 12, 2012 at 5:55 AM

So I add an attachment model to my model via model.Attachments.Add().  Later I go to remove this attachment via model.Attachments.Clear() and I hit an object reference not set to an instance of an object error.  It appears that when SetParent has null passed in...

        protected override void RemoveItem(int index)
        {
            this[index].SetParent(null);
            base.RemoveItem(index);
        }

the set parent function tries to access members of this null parameter.

        internal void SetParent(Model model)
        {
            if (this.model != model)
            {
                if (this.model != null && model != null)
                    throw new InvalidOperationException("Cannot attach to multiple models");

                Validate(model, transformable);

                if (transformable != null)
                {
                    if (model != null)
                    {
                        model.NotifyAdded(transformable);                        
                        model.children.Add(transformable);
                    }
                    else
                    {
                        model.NotifyRemoved(transformable);
                        model.children.Remove(transformable);
                    }
                }

                this.model = model;
            }
        }

Am I perhaps using this in a way that was not intended?

Sep 12, 2012 at 8:41 AM

Oops, this is definitely a bug, thanks for pointing out :)

Try change the SetParent method to

                    else
                    {
                        this.model.NotifyRemoved(transformable);
                        this.model.children.Remove(transformable);
                    }
Sep 12, 2012 at 3:18 PM
Edited Sep 12, 2012 at 3:27 PM

That was actually the first thing I tried, unfortunately it didn't solve the problem.  

this.model.NotifyRemoved(transformable);
this.model.children.Remove(transformable);

This code will execute fine, but the issue is that "transformable"'s parent is not removed.  After removing an attachment, when you try to add a different attachment back onto the model, the engine believes the transformable is already in the scene its parent is set, and throws an exception. (There is a condition when adding an attachment that checks if the parent is null, and if it is not null, throws that exception)  I might not be explaining this the best way, I'd say just insert that fix, and then try adding, removing, and then adding an attachment.

I'm guessing that the CORE of the issue is... if you debug into

this.model.NotifyRemoved(transformable);

this event, you will notice the event is null, and therefor nothing happens.  The issue might be that some event is not getting set/fired properly?

Sep 12, 2012 at 3:23 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 12, 2012 at 3:28 PM

Thank you!