How to get morph target weights in c# ?

This widget could not be displayed.

How to get morph target weights in c# ?

Anonymous
Not applicable

Hi,

 

How to retreive the weight of each morph target listed inside the 'morpher' modifier when no animation is defined?

 

Currently i'm only able to get the animation values of such attribute.

 

A screenshot of what i want:

 

MaxMorphTargetWeight.jpg

 

 

Here is the code i have using 3ds Max 2017:

 

 

IIGameNode meshNode = ... // initialized previously

// Retreive modifiers with morpher flag List<IIGameModifier> modifiers = new List<IIGameModifier>(); for (int i = 0; i < meshNode.IGameObject.NumModifiers; i++) { IIGameModifier modifier = meshNode.IGameObject.GetIGameModifier(i); if (modifier.ModifierType == Autodesk.Max.IGameModifier.ModType.Morpher) { modifiers.Add(modifier); } } // Cast modifiers to morphers List<IIGameMorpher> morphers = new List<IIGameMorpher>(); modifiers.ForEach(modifier => { Assembly wrappersAssembly = Assembly.Load("Autodesk.Max.Wrappers, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"); Type type = wrappersAssembly.GetType("Autodesk.Max.Wrappers.IGameMorpher"); ConstructorInfo constructor = type.GetConstructors()[0]; unsafe { void* voidPtr = modifier.GetNativeHandle().ToPointer(); IIGameMorpher morpher = (IIGameMorpher)constructor.Invoke(new object[] { modifier.GetNativeHandle(), false }); morphers.Add(morpher); } }); // Retreive morph target weights morphers.ForEach(morpher => { for (int i = 0; i < morpher.NumberOfMorphTargets; i++) { IIGameControl morphWeight = morpher.GetMorphWeight(i); // Retreive weight animation values ITab<IIGameKey> gameKeyTab = GlobalInterface.Instance.Tab.Create<IIGameKey>(); morphWeight.GetQuickSampledKeys(gameKeyTab, IGameControlType.Float); for (int indexKey = 0; indexKey < gameKeyTab.Count; indexKey++) { IIGameKey gameKey = gameKeyTab[indexKey]; // Animation value int time = gameKey.T; float weight = gameKey.SampleKey.Fval / 100.0f; } // TODO - HOW TO GET MORPH TARGET WEIGHT? } });

 

 

 

I was expecting IIGameMorpher.GetMorphWeight to return a float and have another method like IIGameMorpher.GetMorphWeightControl to get the animation control...

 

 

 

Noalak

0 Likes
Reply
863 Views
4 Replies
Replies (4)

kevinvandecar
Community Manager
Community Manager

Hi Noalak,

 

When I review that GetMorhWeight API in the Autodesk.Max.dll assembly (I am using ilspy to have a quick look). I can see that the API is returning another object of type IIGameControl. The signature is like this: IIGameControl GetMorphWeight(int index); This type holds a lot of data, and includes IsAnimated. I also checked the C++ SDK header, where there is some documentation. Here it says that the returned IGameControl object is used to get: "The actual value of the keys provides the weight value - this value can not be consider normalised." Going back to your code, I think you are using it correctly, but I agree it is not clear how to obtain the weight with no animation. I see in the IGameControl class there is also a GetConstraintWeight that returns float, would that be something?

 

If that does not help, and if no one else has a strong knowledge of these APIs, it would be great if you provided a small scene with the setup, and a small sample C# program to make it easier for us to investigate deeper. (note the C# version is a simple wrapper of the C++ version, so behavior should be

identical).

 

Hope that helps,

kevin

 


Kevin Vandecar
Developer Technical Services
Autodesk Developer Network



0 Likes

Anonymous
Not applicable

Hi,

 

Thanks for your answer. Was busy lately couldn't reply sooner.

 

About your suggestion with GetConstraintWeight, for me this method is not inside IGameControl class but inside IIGameConstraint.

Such a object can be retreive through IGameControl.GetConstraint method.

However it requires a IGameControlType as parameter and none of the available choice returns a non null value...

 

// Retreive morph target weights
morphers.ForEach(morpher =>
{
    for (int i = 0; i < morpher.NumberOfMorphTargets; i++)
    {
        IIGameControl morphWeight = morpher.GetMorphWeight(i);

        // For each control type (float, vec3...)
        var controlTypes = Enum.GetValues(typeof(IGameControlType)).Cast<IGameControlType>();
        foreach (var controlType in controlTypes)
        {
            IIGameConstraint constraint = morphWeight.GetConstraint(controlType);
            if (constraint != null)
            {
                // Never reach here
                
                // float weight = constraint.GetConstraintWeight(0);
            }
        }
    }
});

Dunno what to do...

 

Anonymous
Not applicable

I have exactly the same problem.


 morphWeight.GetConstraint (controlType) always null even with my try

 

Did you ever get it out?
Please write a short msg !

Regards
Peter


0 Likes

Anonymous
Not applicable

No 😞

0 Likes