Log in or register to post comments

Spawning ModelTargetBehaviour from code

October 9, 2019 - 7:22am #1

Hi,

 

I have a simple question.

I would like to spawn a ModelTarget (MT360) from C# code. I can simply create a GameObject and add the component:

 

    GameObject go = new GameObject("ModelTarget");

    ModelTargetBehaviour res = go.AddComponent<ModelTargetBehaviour>();

 

The resulting ModelTargetBehaviour is, naturally, not set up. The "Database" and "Model Target" in the inspector are --EMPTY--. And the physical LxWxH are all 0. Fair enough.

But is there any way to "load" a database into this ModelTargetBehaviour after creating it? All the databases I need are in the VuforiaConfiguration, since I imported them from the ModelTargetGenerator.

I need to set up multiple of these, enable one at a time and switch between them when I need. I can do this manually successfully, but now I would like to automate this...

 

Thanks!

Spawning ModelTargetBehaviour from code

October 17, 2019 - 2:04am #3

Well... I think the reason I had no ObjectTracker when I had no ModelTargetBehaviour in the scene is that I forgot to add the VuforiaBehaviour to the camera... I am using HoloLens, with MRTK, and I recently switched to a clean MRTK cam rig during a refactor. And I forgot to add the VuforiaBehaviour. Now that I added it, all seems good. I also added an extra safety, but it seems I don't even need it:

if(objectTracker == null)
{
    ((TrackerManager)TrackerManager.Instance).InitTracker<ObjectTracker>();
}

So I was being a bit silly. But maybe it helps others in the future.

Spawning ModelTargetBehaviour from code

October 16, 2019 - 12:23am #2

I found out...

 

Apparently, when you load a DataSet (which has already been explained on the forum), it automatically creates the ModelTargetBehaviour (if it's a ModelTarget of course). It does not seem to create the child mesh, but for me that is not a problem. It might even be better without the automatic model so I can (re)use my own.

The only thing I found is that, there is no ObjectTracker if there is no ModelTargetBehaviour in the scene at all. Right now I just have a single ModelTargetBehaviour to automatically have an ObjectTracker available to load the other datasets into. But I want to look into starting up an ObjectTracker from code, so the scene is "clean" (no ModelTargetBehaviours) when it starts.

Log in or register to post comments