Obi Official Forum
Help Set Cloth Properties with script - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Cloth (https://obi.virtualmethodstudio.com/forum/forum-2.html)
+--- Thread: Help Set Cloth Properties with script (/thread-2853.html)



Set Cloth Properties with script - orestissar - 08-04-2021

Hello,

Im currently trying to create and assign a skinned cloth blueprint to some cloth parts i have in my scene. 

I have succesfully managed to do this but im having some issues setting the cloth properties values with the script (skin backstop, backstopradius, radius).
Code:
  blueprint.inputMesh = mesh;
  ObiSkinnedCloth obsc = childobject.AddComponent<ObiSkinnedCloth>() as ObiSkinnedCloth;
  obsc.skinnedClothBlueprint = blueprint;             
  ObiSkinnedClothRenderer obscr = childobject.AddComponent<ObiSkinnedClothRenderer>() as ObiSkinnedClothRenderer;
The above code does what i explained before, but i need couple code lines to add the values i want for each property.

I found this relevant topic: http://obi.virtualmethodstudio.com/forum/thread-2405.html 
but i cant make it work.

So, how do i manage that?
thanks beforehand!


RE: Set Cloth Properties with script - josemendez - 09-04-2021

Hi!

Only change needed for newer versions is that the first batch is accessed like this:

Code:
var skinBatch = skinConstraints.batches[0];

instead of

Code:
var skinBatch = skinConstraints[0];

Other than that, everything is exactly the same, it should just work. Keep in mind that you have to iterate trough all constraints, if you wish to set the values for *all* of them:

Code:
for (int i = 0; i < skinBatch.constraintCount; ++i)
{
       
        // the skinRadiiBackstop array contains 3 floats per constraint: the skin radius, the collision radius and the collision backstop distance:
        skinBatch.skinRadiiBackstop[i * 3] = < skin radius >;
        skinBatch.skinRadiiBackstop[i * 3 + 1] = < collision radius >;
        skinBatch.skinRadiiBackstop[i * 3 + 2] = < collision backstop >;
}

Let me know if you have trouble with it. cheers!


RE: Set Cloth Properties with script - orestissar - 09-04-2021

Hey again, and thanks for the reply.

The code below gives me this error in console:

NullReferenceException: Object reference not set to an instance of an object
CreateObiBP.Start () (at Assets/Shirt and Pants/Shirt/CreateObiBP.cs:36)

Code:
var skinBatch = skinConstraints.batches[0];

Also, im not sure i understand the exactly logic behind the iteration below.

Code:
for (int i = 0; i < skinBatch.constraintCount; ++i)
{
       
        // the skinRadiiBackstop array contains 3 floats per constraint: the skin radius, the collision radius and the collision backstop distance:
        skinBatch.skinRadiiBackstop[i * 3] = < skin radius >;
        skinBatch.skinRadiiBackstop[i * 3 + 1] = < collision radius >;
        skinBatch.skinRadiiBackstop[i * 3 + 2] = < collision backstop >;
}



RE: Set Cloth Properties with script - josemendez - 09-04-2021

(09-04-2021, 11:07 AM)orestissar Wrote: Hey again, and thanks for the reply.

The code below gives me this error in console:

NullReferenceException: Object reference not set to an instance of an object
CreateObiBP.Start () (at Assets/Shirt and Pants/Shirt/CreateObiBP.cs:36)

Code:
var skinBatch = skinConstraints.batches[0];

If line 36 in your script is that one, it means skinConstraints is null. This can only be null if:
a) your blueprint hasn't been generated yet. Not initialized --> no constraints.
b) your blueprint is not a ObiSkinnedClothBlueprint.

Check the manual for info on how to create and generate a blueprint at runtime:
http://obi.virtualmethodstudio.com/tutorials/scriptingactors.html

(09-04-2021, 11:07 AM)orestissar Wrote: Also, im not sure i understand the exactly logic behind the iteration below.

Cloth is made of particles, generally one per vertex. Each particle in the cloth has one skin constraint, that restricts its range of movement relative to the skinned mesh. If you don't iterate trough them all, you're setting values just for one of them.


RE: Set Cloth Properties with script - orestissar - 09-04-2021

(09-04-2021, 11:14 AM)josemendez Wrote: Cloth is made of particles, generally one per vertex. Each particle in the cloth has one skin constraint, that restricts its range of movement relative to the skinned mesh. If you don't iterate trough them all, you're setting values just for one of them.
ok i understand now, tnx. 


regarding the other thing:

my code is this:
Code:
public class CreateObiBP : MonoBehaviour
{   
    void Start()
    {
        GameObject CreateObi = this.gameObject;
        Debug.Log("Loaded");

        ObiSolver obs = CreateObi.AddComponent<ObiSolver>();
        ObiLateFixedUpdater olfu = CreateObi.AddComponent<ObiLateFixedUpdater>();
        olfu.solvers.Add(obs);
      
        int garmentID = 0;

        foreach (Transform child in CreateObi.transform)
        {
            GameObject childobject = child.gameObject;

            var blueprint = ScriptableObject.CreateInstance<ObiSkinnedClothBlueprint>();
            var bd = ScriptableObject.CreateInstance<ObiSkinnedClothBlueprint>();

            SkinnedMeshRenderer skin = childobject.GetComponent(typeof(SkinnedMeshRenderer)) as SkinnedMeshRenderer;
            var mesh = skin.sharedMesh;

            blueprint.name = childobject.name;
           
            blueprint.inputMesh = mesh;
            blueprint.Generate();

            ObiSkinnedCloth obsc = childobject.AddComponent<ObiSkinnedCloth>() as ObiSkinnedCloth;
            obsc.skinnedClothBlueprint = blueprint;             
            ObiSkinnedClothRenderer obscr = childobject.AddComponent<ObiSkinnedClothRenderer>() as ObiSkinnedClothRenderer;
         
        }

    }
everything in the scene seems to generate, however i still get the error


RE: Set Cloth Properties with script - josemendez - 09-04-2021

Code:
blueprint.Generate();

This is a coroutine (as evidenced by its IEnumerator return type). Like all coroutines, you need to use StartCoroutine() and wait for it to finish. Calling it directly like in your code does nothing. From the code samples found in the manual page I linked to in the previous message:

Code:
yield return StartCoroutine(blueprint.Generate());

Also, see:
https://docs.unity3d.com/Manual/Coroutines.html

cheers,


RE: Set Cloth Properties with script - orestissar - 09-04-2021

(09-04-2021, 11:39 AM)josemendez Wrote:
Code:
blueprint.Generate();

This is a coroutine (as evidenced by its IEnumerator return type). Like all coroutines, you need to use StartCoroutine() and wait for it to finish. Calling it directly like in your code does nothing. From the code samples found in the manual page I linked to in the previous message:

Code:
yield return StartCoroutine(blueprint.Generate());

Also, see:
https://docs.unity3d.com/Manual/Coroutines.html

cheers,
oooh! thanks! i think i fixed it ! thank you so much! im kind of new to unity so i do this kind of mistakes.

Is there a way to actually save the generated blueprint ?


RE: Set Cloth Properties with script - josemendez - 09-04-2021

(09-04-2021, 11:54 AM)orestissar Wrote: oooh! thanks! i think i fixed it ! thank you so much! im kind of new to unity so i do this kind of mistakes.

No worries! Keep in mind that Obi is quite complex, so it might take you a while to get used to it.

(09-04-2021, 11:54 AM)orestissar Wrote: Is there a way to actually save the generated blueprint ?

Yes, blueprints are just assets (ScriptableObjects) so you can save the usual way it's done in Unity. See:

https://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html
https://docs.unity3d.com/ScriptReference/AssetDatabase.SaveAssets.html


RE: Set Cloth Properties with script - orestissar - 09-04-2021

(09-04-2021, 11:58 AM)josemendez Wrote: No worries! Keep in mind that Obi is quite complex, so it might take you a while to get used to it.


Yes, blueprints are just assets (ScriptableObjects) so you can save the usual way it's done in Unity. See:

https://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html
https://docs.unity3d.com/ScriptReference/AssetDatabase.SaveAssets.html
i think i made it work! thank you again so much!


RE: Set Cloth Properties with script - josemendez - 09-04-2021

You're welcome Sonrisa . Don't hesitate to ask if you need help.