• Hi Guest!

    This is a notice regarding recent upgrades to the Hub. Over the last month, we have added several new features to improve your experience.
    You can check out the details in our official announcement!

Plugin prototyping: Easy Pose Biomechanics Rig

MaryJane

Member
Messages
11
Reactions
32
Points
13
Hi,
trying to create my first plugin for VaM and I am getting a NullReferenceException, but I don't know why.
I think the person body parts controller not exist until I selected every one manually.

https://hub.virtamate.com/resources...l-and-superathlete-with-simple-physics.37148/

Code:
!> Exception caught: System.NullReferenceException: Object reference not set to an instance of an object
  at MaryJane.VAMPluginDev.setHipController () [0x00000] in <filename unknown>:0
  at MaryJane.VAMPluginDev.setControllersList () [0x00000] in <filename unknown>:0

C#:
using System;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.UI;
using Battlehub.UIControls;
using UnityEngine.Tizen;
using UnityEngine.UI.Extensions;

namespace MaryJane
{
    public class VAMPluginDev : MVRScript
    {
        protected bool debug = true;
        protected string _personAtomName;
        protected string _chestControllerName;
        protected string _neckControllerName;

        public override void Init()
        {
            try
            {
                if (containingAtom.type != "Person")
                {
                    SuperController.LogError("Please attach to a Person Atom");
                }
                else
                {
                    // put code in here
                    _personAtomName = containingAtom.name;
                    _chestControllerName = "chestController";
                    _neckControllerName = "neckController";
                    setControllersList();
                }
            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }
        void Start()
        {
            try
            {
                // put code in here               
            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }
        void Update()
        {
            try
            {
                // put code in here
            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }
        void FixedUpdate()
        {
            try
            {
                // put code in here               
            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }
        void OnDestroy() { }

        protected void setControllersList()
        {
            try
            {
                SuperController.LogMessage("ControllerList");
                foreach (FreeControllerV3 _controller in containingAtom.freeControllers)
                {
                    SuperController.LogMessage(_controller.ToString());
                }

                SuperController.LogMessage("");
                SuperController.LogMessage("SetUp Controller");

                // Neck (the master atom) - Position On, Rotation On

                setNeckController();

                // Head - Position Comply, Rotation On
                setHeadController();

                // Chest - Position Comply, Rotation Comply. Parent Link to Neck
                setChestController();

                // Hip - Position Comply, Rotation Comply, Parent Link to Chest
                setHipController();

                // R and L Elbow - Position On, Rotation Off, Parent Link to Neck
                setRightElbowController();
                setLeftElbowController();

                // R and L Hand - Position Off, Rotation Comply
                setRightHandController();
                setLeftHandController();


                // R and L Knee -Position On, Rotation Off, Parent Link to Neckf
                setRightKneeController();
                setLeftKneeController();

                // R and L Foot - Position Comply, Rotation On
                setRightFootController();
                setLeftFootController();


                // R and L Toe -Position Off, Rotation Comply
                setRightToeController();
                setLeftToeController();


            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }

        private void setNeckController()
        {
            FreeControllerV3 _neckControl = containingAtom.GetStorableByID("neckControl") as FreeControllerV3;
            _neckControl.currentPositionState = FreeControllerV3.PositionState.On;
            _neckControl.currentRotationState = FreeControllerV3.RotationState.On;
            SuperController.LogMessage("Neck (the master atom) - Position On, Rotation On");
        }

        private void setHeadController()
        {
            FreeControllerV3 _headControl = containingAtom.GetStorableByID("headControl") as FreeControllerV3;
            _headControl.currentPositionState = FreeControllerV3.PositionState.Comply;
            _headControl.currentRotationState = FreeControllerV3.RotationState.On;
            SuperController.LogMessage("Head - Position Comply, Rotation On");
        }

        private void setChestController()
        {
            FreeControllerV3 _chestControl = containingAtom.GetStorableByID("chestControl") as FreeControllerV3;
            _chestControl.currentPositionState = FreeControllerV3.PositionState.Comply;
            _chestControl.currentRotationState = FreeControllerV3.RotationState.Comply;
            _chestControl.linkToAtomSelectionPopup.currentValue = _personAtomName;
            _chestControl.linkToSelectionPopup.currentValue = _neckControllerName;
            SuperController.LogMessage("Chest - Position Comply, Rotation Comply. Parent Link to Neck");
        }

        private void setHipController()
        {
            FreeControllerV3 _hipControl = containingAtom.GetStorableByID("hipControl") as FreeControllerV3;
            _hipControl.currentPositionState = FreeControllerV3.PositionState.Comply;
            _hipControl.currentRotationState = FreeControllerV3.RotationState.Comply;
            _hipControl.linkToAtomSelectionPopup.currentValue = _personAtomName; ;
            _hipControl.linkToSelectionPopup.currentValue = _chestControllerName;
            SuperController.LogMessage("Hip - Position Comply, Rotation Comply, Parent Link to Chest");
        }

        private void setRightElbowController()
        {
            FreeControllerV3 _rElbowControl = containingAtom.GetStorableByID("rElbowControl") as FreeControllerV3;
            _rElbowControl.currentPositionState = FreeControllerV3.PositionState.On;
            _rElbowControl.currentRotationState = FreeControllerV3.RotationState.Off;
            _rElbowControl.linkToAtomSelectionPopup.currentValue = _personAtomName;
            _rElbowControl.linkToSelectionPopup.currentValue = _neckControllerName;
            SuperController.LogMessage("R Elbow - Position On, Rotation Off, Parent Link to Neck");
        }

        private void setLeftElbowController()
        {
            FreeControllerV3 _lElbowControl = containingAtom.GetStorableByID("lElbowControl") as FreeControllerV3;
            _lElbowControl.currentPositionState = FreeControllerV3.PositionState.On;
            _lElbowControl.currentRotationState = FreeControllerV3.RotationState.Off;
            _lElbowControl.linkToAtomSelectionPopup.currentValue = _personAtomName;
            _lElbowControl.linkToSelectionPopup.currentValue = _neckControllerName;
            SuperController.LogMessage("L Elbow - Position On, Rotation Off, Parent Link to Neck");
        }

        private void setRightHandController()
        {
            FreeControllerV3 _rHandControl = containingAtom.GetStorableByID("rHandControl") as FreeControllerV3;
            _rHandControl.currentPositionState = FreeControllerV3.PositionState.Off;
            _rHandControl.currentRotationState = FreeControllerV3.RotationState.Comply;
            SuperController.LogMessage("R Hand - Position Off, Rotation Comply");
        }

        private void setLeftHandController()
        {
            FreeControllerV3 _lHandControl = containingAtom.GetStorableByID("lHandControl") as FreeControllerV3;
            _lHandControl.currentPositionState = FreeControllerV3.PositionState.Off;
            _lHandControl.currentRotationState = FreeControllerV3.RotationState.Comply;
            SuperController.LogMessage("L Hand - Position Off, Rotation Comply");
        }

        private void setRightKneeController()
        {
            FreeControllerV3 _rKneeControl = containingAtom.GetStorableByID("rKneeControl") as FreeControllerV3;
            _rKneeControl.currentPositionState = FreeControllerV3.PositionState.On;
            _rKneeControl.currentRotationState = FreeControllerV3.RotationState.Off;
            _rKneeControl.linkToAtomSelectionPopup.currentValue = _personAtomName;
            _rKneeControl.linkToSelectionPopup.currentValue = _neckControllerName;
            SuperController.LogMessage("R Knee -Position On, Rotation Off, Parent Link to Neck");
        }

        private void setLeftKneeController()
        {
            FreeControllerV3 _lKneeControl = containingAtom.GetStorableByID("lKneeControl") as FreeControllerV3;
            _lKneeControl.currentPositionState = FreeControllerV3.PositionState.On;
            _lKneeControl.currentRotationState = FreeControllerV3.RotationState.Off;
            _lKneeControl.linkToAtomSelectionPopup.currentValue = _personAtomName;
            _lKneeControl.linkToSelectionPopup.currentValue = _neckControllerName;
            SuperController.LogMessage("L Knee -Position On, Rotation Off, Parent Link to Neck");
        }

        protected void setRightFootController()
        {
            FreeControllerV3 _rFootControl = containingAtom.GetStorableByID("rFootControl") as FreeControllerV3;
            _rFootControl.currentPositionState = FreeControllerV3.PositionState.Comply;
            _rFootControl.currentRotationState = FreeControllerV3.RotationState.On;
            SuperController.LogMessage("R Foot - Position Comply, Rotation On");
        }

        protected void setLeftFootController()
        {
            FreeControllerV3 _lFootControl = containingAtom.GetStorableByID("lFootControl") as FreeControllerV3;
            _lFootControl.currentPositionState = FreeControllerV3.PositionState.Comply;
            _lFootControl.currentRotationState = FreeControllerV3.RotationState.On;
            SuperController.LogMessage("L Foot - Position Comply, Rotation On");
        }

        protected void setRightToeController()
        {
            FreeControllerV3 _rToeControl = containingAtom.GetStorableByID("rToeControl") as FreeControllerV3;
            _rToeControl.currentPositionState = FreeControllerV3.PositionState.Off;
            _rToeControl.currentRotationState = FreeControllerV3.RotationState.Comply;
            SuperController.LogMessage("R Toe -Position Off, Rotation Comply");
        }

        protected void setLeftToeController()
        {
            FreeControllerV3 _lToeControl = containingAtom.GetStorableByID("lToeControl") as FreeControllerV3;
            _lToeControl.currentPositionState = FreeControllerV3.PositionState.Off;
            _lToeControl.currentRotationState = FreeControllerV3.RotationState.Comply;
            SuperController.LogMessage("L Toe -Position Off, Rotation Comply");
        }

    }
}
 
I think the person body parts controller not exist until I selected every one manually.
The hipControl does exist just fine.
However you try to access the UI, which doesn't exist until you opened it:
_hipControl.linkToAtomSelectionPopup

Possibly you would want to try calling one or more of those methods on FreeControllerV3 instead of going through the UI:
SetLinkToAtom(string atomUID) SetLinkToRigidbody(string rigidbodyName) SetLinkToRigidbodyObject(string objectName)

Have not used these, so can't help. You may need to experiment a bit and/or look at their implementation in ILSpy.
 
The hipControl does exist just fine.
However you try to access the UI, which doesn't exist until you opened it:
_hipControl.linkToAtomSelectionPopup
Thanks, now that makes sense to me.

Possibly you would want to try calling one or more of those methods on FreeControllerV3 instead of going through the UI:
SetLinkToAtom(string atomUID) SetLinkToRigidbody(string rigidbodyName) SetLinkToRigidbodyObject(string objectName)
Tried this first but the UI is not updating.
Maybe I miss some UI Update magic?

C#:
            _hipControl.SetLinkToAtom(_personAtomName);
            _hipControl.SetLinkToRigidbody(_chestControllerName);
 
Gave it a quick try....doing both seems to work?

C#:
hipControl.SetLinkToAtom(personAtomName);
hipControl.SetLinkToRigidbodyObject(chestControllerName);
if (hipControl.linkToAtomSelectionPopup != null)
{
    hipControl.linkToAtomSelectionPopup.currentValue = personAtomName;
    hipControl.linkToSelectionPopup.currentValue = chestControllerName;
}
 
It's working, but need to press [Select] in the UI to update the links.
Thank you very much.


C#:
using System;

namespace MaryJane
{
    public class Rig : MVRScript
    {
        protected bool debug = false;
        protected string personAtomName;
        protected string chestControllerName;
        protected string neckControllerName;

        public override void Init()
        {
            try
            {
                if (containingAtom.type != "Person")
                {
                    SuperController.LogError("Please attach to a Person Atom");
                }
                else
                {
                    // put code in here
                    personAtomName = containingAtom.name;
                    chestControllerName = "chestControl";
                    neckControllerName = "neckControl";
                    SetControllers();
                }
            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }

        protected void DebugMessage(string message)
        {
            if (debug)
            {
                SuperController.LogMessage(message);
            }
        }

        protected void SetControllers()
        {
            try
            {
                DebugMessage("ControllerList");
                foreach (FreeControllerV3 _controller in containingAtom.freeControllers)
                {
                    DebugMessage(_controller.ToString());
                }

                DebugMessage("");
                DebugMessage("SetUp Controller");

                setNeckController(containingAtom.GetStorableByID("neckControl") as FreeControllerV3);
                setHeadController(containingAtom.GetStorableByID("headControl") as FreeControllerV3);
                setChestController(containingAtom.GetStorableByID("chestControl") as FreeControllerV3);
                setHipController(containingAtom.GetStorableByID("hipControl") as FreeControllerV3);
                setRightElbowController(containingAtom.GetStorableByID("rElbowControl") as FreeControllerV3);
                setLeftElbowController(containingAtom.GetStorableByID("lElbowControl") as FreeControllerV3);
                setRightHandController(containingAtom.GetStorableByID("rHandControl") as FreeControllerV3);
                setLeftHandController(containingAtom.GetStorableByID("lHandControl") as FreeControllerV3);
                setRightKneeController(containingAtom.GetStorableByID("rKneeControl") as FreeControllerV3);
                setLeftKneeController(containingAtom.GetStorableByID("lKneeControl") as FreeControllerV3);
                setRightFootController(containingAtom.GetStorableByID("rFootControl") as FreeControllerV3);
                setLeftFootController(containingAtom.GetStorableByID("lFootControl") as FreeControllerV3);
                setRightToeController(containingAtom.GetStorableByID("rToeControl") as FreeControllerV3);
                setLeftToeController(containingAtom.GetStorableByID("lToeControl") as FreeControllerV3);


            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }

        protected void setController(FreeControllerV3 controller, FreeControllerV3.PositionState positionState, FreeControllerV3.RotationState rotationState)
        {
            controller.currentPositionState = positionState;
            controller.currentRotationState = rotationState;
        }

        protected void setController(FreeControllerV3 controller, FreeControllerV3.PositionState positionState, FreeControllerV3.RotationState rotationState, string linkToAtom, string linkToRigidbody)
        {
            controller.currentPositionState = positionState;
            controller.currentRotationState = rotationState;

            controller.SetLinkToAtom(linkToAtom);
            controller.SetLinkToRigidbodyObject(linkToRigidbody);
            if (controller.linkToAtomSelectionPopup != null)
            {
                controller.linkToAtomSelectionPopup.currentValue = linkToAtom;
                controller.linkToSelectionPopup.currentValue = linkToRigidbody;
            }
        }

        protected void setNeckController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.On, FreeControllerV3.RotationState.On);
            DebugMessage("Neck (the master atom) - Position On, Rotation On");
        }

        protected void setHeadController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Comply, FreeControllerV3.RotationState.On);
            DebugMessage("Head - Position Comply, Rotation On");
        }

        protected void setChestController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Comply, FreeControllerV3.RotationState.Comply, personAtomName, neckControllerName);
            DebugMessage("Chest - Position Comply, Rotation Comply. Parent Link to Neck");
        }

        protected void setHipController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Comply, FreeControllerV3.RotationState.Comply, personAtomName, chestControllerName);
            DebugMessage("Hip - Position Comply, Rotation Comply, Parent Link to Chest");
        }

        protected void setRightElbowController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.On, FreeControllerV3.RotationState.Off, personAtomName, neckControllerName);
            DebugMessage("R Elbow - Position On, Rotation Off, Parent Link to Neck");
        }

        protected void setLeftElbowController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.On, FreeControllerV3.RotationState.Off, personAtomName, neckControllerName);
            DebugMessage("L Elbow - Position On, Rotation Off, Parent Link to Neck");
        }

        protected void setRightHandController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Off, FreeControllerV3.RotationState.Comply);
            DebugMessage("R Hand - Position Off, Rotation Comply");
        }

        protected void setLeftHandController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Off, FreeControllerV3.RotationState.Comply);
            DebugMessage("L Hand - Position Off, Rotation Comply");
        }

        protected void setRightKneeController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.On, FreeControllerV3.RotationState.Off, personAtomName, neckControllerName);
            DebugMessage("R Knee -Position On, Rotation Off, Parent Link to Neck");
        }

        protected void setLeftKneeController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.On, FreeControllerV3.RotationState.Off, personAtomName, neckControllerName);
            DebugMessage("L Knee -Position On, Rotation Off, Parent Link to Neck");
        }

        protected void setRightFootController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Comply, FreeControllerV3.RotationState.On);
            DebugMessage("R Foot - Position Comply, Rotation On");
        }

        protected void setLeftFootController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Comply, FreeControllerV3.RotationState.On);
            DebugMessage("L Foot - Position Comply, Rotation On");
        }

        protected void setRightToeController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Off, FreeControllerV3.RotationState.Comply);
            DebugMessage("R Toe -Position Off, Rotation Comply");
        }

        protected void setLeftToeController(FreeControllerV3 controller)
        {
            setController(controller, FreeControllerV3.PositionState.Off, FreeControllerV3.RotationState.Comply);
            DebugMessage("L Toe -Position Off, Rotation Comply");
        }

    }
}
 
Back
Top Bottom