Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Set Cloth Properties with script
#1
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...-2405.html 
but i cant make it work.

So, how do i manage that?
thanks beforehand!
Reply
#2
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!
Reply
#3
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 >;
}
Reply
#4
(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/tutor...ctors.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.
Reply
#5
(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
Reply
#6
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,
Reply
#7
(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 ?
Reply
#8
(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...Asset.html
https://docs.unity3d.com/ScriptReference...ssets.html
Reply
#9
(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...Asset.html
https://docs.unity3d.com/ScriptReference...ssets.html
i think i made it work! thank you again so much!
Reply
#10
You're welcome Sonrisa . Don't hesitate to ask if you need help.
Reply