Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Help by scripting cloth modifier

Help by scripting cloth modifier

cerakecera
Advocate Advocate
1,194 Views
11 Replies
Message 1 of 12

Help by scripting cloth modifier

cerakecera
Advocate
Advocate

Hello everyone.

First of all, i´d like you to know that im a beginner to MaxScript and scripting in general.
Started learning it by studying the MaxScript Help/Documentary/Reference pages and watching the 101 Tutorials by John Wainwright etc.
I can pretty much say, that wasn´t enough to write my first script or give me a clue on how get started. Especially regarding the matter i started learning MaxScript in the first place, which is the cloth modifier.
Working with it for animations alot, can say im pretty much experienced. As powerful as it is, there are situations, where it messes up the mesh completely, due to different poly sizes and forms or even velocity of the mesh. Solving this issue with just the cloth group parameters didn´t work. Tried that for a long time and saw alot people struggle with this, which i could only offer a workaround...

In a nutshell: I want to write scripts, which automaticly changes values for each vert of the mesh in respect of poly size, form, bend, velocity and so on.

To begin with i´d like to ask your help in formulating a code that can change the cloth density, when a poly is stretched above limit while simulation is running. All i have so far is this:

If -- poly reaches/stretches a certain size/length
Then <Cloth>.<Group>.density: float -- decrease value
Else -- keep default value

Appreciate every help/suggestion you can come up with and if there is something unclear with my matter (i bet there is) please ask me.
Thank you in advance!

0 Likes
1,195 Views
11 Replies
Replies (11)
Message 2 of 12

ads_royje
Alumni
Alumni

Hi @cerakecera ,

 

How do you define what is the condition, how/when is it verified ?

If -- poly reaches/stretches a certain size/length
0 Likes
Message 3 of 12

cerakecera
Advocate
Advocate

How do you define what is the condition, how/when is it verified ?

Hello ads_royje, thank you for helping!

Hope i understand the question correct.

When cloth stretches a poly during simulation too much, the script should verify this in the next frame and change values until the poly is back to default size.

e.g. a high density value stretches polys when gravitation is on or when accelerating the mesh.

I should add, all this is for animating purposes.

If i couldn´t make things clear, i can make a video, showcase.

Thank you again for helping out, much appreciated!

0 Likes
Message 4 of 12

ads_royje
Alumni
Alumni

Hi again,

 

yes, you got the question right! 🙂

 

I am trying to understand how would that work...

Is the sim running, if so, then the check function should run every frame.

But what it the function checking ? how do you validate that there is a stretch, in script I mean ? Visually it is clear of course.
There is no output from cloth to tell the state of the cloth, so I am wondering how does this gets verified ?
Is the check on the bounding box, passed a certain size ? Or does the function iterate through vertices and validate distances between them ?
The problem I cannot visualize is how does the function run every frame while the simulation is ran.
Cloth does not have a frame per frame simulation that can be done in script...
What I mean, is that every frame, the function has to validate the stretching before the simulation is ran so it can update the Density accordingly. Not sure 100% that it would actually update but I got hope it should because the property can be animated.

Currently, the only idea I get that 'may' work would be to run the simulation, then run the function on every frame to analyse the mesh until it finds an unwanted 'stretching'. From there the script could 'truncate' the sim (or erase it), key the Density accordingly and run the sim again. (I am thinking out loud...)


0 Likes
Message 5 of 12

cerakecera
Advocate
Advocate

Yes the simulation is running and the checks should validate vertices every frame, e.g:

frame n: if poly is overstretching then...

next frame: change density/stretch/shear etc. value until...

1 to couple frames later: poly stretch is back to desired size then...

next frame: change density/stretch/shear etc. value to default

 

So the frame the overstretching occurs, the script registers this and gives the order to change values in the next frame accordingly. I do this right now by manually keying, but with cloth groups, inaccurate values and bad timing and the result is, well anything but good. And there will always be issues when grouping vertices, because of the mesh topology. Their shape isn´t uniformly, so is their reaction to the groups settings. Giving all of them unique settings is the aim of the script.

But if i understand you right, scripts can´t read poly/vert status from cloth modifier in general? Then how about throwing a poly select or edit poly on top of cloth, which should do the job by providing the necessary information for the script, which updates with cloth every frame. Or maybe i didn´t get the point.

 

Your other suggestion is absolutly worth trying, too!

 

0 Likes
Message 6 of 12

ads_royje
Alumni
Alumni

Cloth modifier does not tell us what is the state of its mesh.

Yes, you can get the vertices from Cloth end results using either polyop or meshop depending on the base class of the mesh (Editable Poly or Editable Mesh).
What I meant was that Cloth won't return the mesh state as being stretch or not. It is for the script to define what that means and how to validate that.
That is the 'secret sauce' that the function will have to do, and for you do define. 🙂

The problem I was seeing is that when Cloth simulation is ran, it is ran for the whole time line. There is no script function to simulate frame by frame, so I could not see (yet) how to execute a validation function for each frame...

But after thinking (better) into it, Cloth does have a Start and End frame value that can be set... that may work.
Cloth has a value for Start and End frame that can be set in script, I was not thinking about that earlier... This could do the trick.

So let's say you have a function that tests vertices on the mesh using meshop or polyop.
The fn would update as you wish the Density parameter (and/or any other you need to) if the defined condition(s) is(are) meet.

This what I would try, in a for loop based on the timeline

for fr = animationRange.start to animationRange.end do (

-- set current frame
slidertime = fr
-- on cloth, set the end frame for the simulation to be the current frame
-- so as the loop goes forward, we are accumulating simulated frames <obj>.modifiers[#cloth].endframe = fr  -- execute the simulation, this simulate from the last simulated frame to the current frame -- as we are going forward in timeline, we update the end frame of the simulation, this is how we can control the advancement of the simulation frame by frame  $Plane001.modifiers[#Cloth].simulate true -- once simulation is ran on current frame, we can now do the mesh validation test -- this is the magic function that you define to validate if the mesh is stretching out of bound or not and update the the Cloth parameters that you'd need to update. myValidationFunction()   )


Let me know if this makes any sense or not

 

0 Likes
Message 7 of 12

cerakecera
Advocate
Advocate

It absolutly does make sense 😀

I wonder how this will behave. Will it start-stop-start and so on, frame by frame the simulation automaticly through the active timeline?

Regarding the validation function, im not sure what is possible and not. Should it validate polys or verts including adjacent to check for overstretching?

And will it be possible to exclude polys/verts from the checks in specific cases?

 

0 Likes
Message 8 of 12

ads_royje
Alumni
Alumni

Hi again!

the behavior would look at bit like in this example.
https://autode.sk/36uBQUw

In this example, it does not 'correct' the simulation.

This is a first try to validate 'how' it could work.

 

The video shows how to run the cloth sim per frame and how to set it up so it can be modified while simulating.


That is a first step.

 

The next step, indeed, relates to your questions.

 

This is where I can help the least, but can help get on 'a path' to some results.
You'll need to figure out what it the best approach for your objects and simulations.
What 'stretches too much' in the simulation, how you can catch it and how to fix it.

What I don't know is what you would consider to be a stretch problem with your meshes and simulation.

The approach I would take from there, is use a concrete example that you have, a simulation that you'd like your scripted tool to catch and correct, and most important, how would you correct it! As you'd need to be able to do it manually so it can be scripted.
Using the concrete example, I would try to seek for where does the stretch occur, what is the best method for you to catch it, it is vertex distance from each other or would it be the faces' surface area growing, or is it another way ?

Depending on the base mesh type, Editable Mesh (meshop) or Editable Poly (Polyop)
Here is a few key commands that can be used, as example I am using Polyop, for Editable Poly mesh.

To get number of vertices of an Editable Poly
polyop.getNumVerts <object>

To get number of faces of an Editable Poly
polyop.getNumFaces <object>

Using either (or both), we can get the amount of vertices or faces. With that information we can check them

Iterating through vertices to check distances could be a long process, as we would need to check every vertex against all of them. That is doable, requires a good logic.

Could also iterate through all faces and check for it area.
For instance, for a given Editable Poly, iterate through all faces and print out its surface area

for i = 1 to (polyop.getNumFaces $) do (
    print (polyop.getFaceArea $ i) -- get the Face Surface
)


If needed be, for any given faces, we can also get the vertices

for i = 1 to (polyop.getNumFaces $) do (
    print (polyop.getFaceArea $ i) -- get the Face Surface 
    print (polyop.getFaceVerts $ i) -- get the same Face Vertices
)


To get all of the possible polyop functions, in the listener execute

show polyop -- shows all polyop functions. Use on Editable Poly
show meshop -- shows all meshop functions. Use on Editable Mesh


Other notes that can be of use:
. The script can use Selection Sets for Vertex or Face Selection
. The script can use modifier stack Vertex or Face Selection
. The Cloth modifier can be set to have many Vertex Groups with different settings, all that can be altered while simulating.

The script could also have options to go back in (simulation) time and correct values so stretching 'does not' occur.
For instance, if on a given frame, lets say 52, the function finds a face that stretches above the point you defined, then the script could go back a given amount of frame (either predefined, like 5 frames back) or go back to a frame where the area was still in an acceptable size. Then truncate the simulation, update some cloth values, and start to simulate again.

At that stage, this is the 'secret sauce' you'd have to figure out...

A long one, but hope it can help! 🙂
Let me know if this goes in the direction you'd like!

Regards,

Message 9 of 12

cerakecera
Advocate
Advocate

This is just amazing and is more then i have expected or imagined!

Really stunned how much effort you put in to help! Your explanation video gave me a better understanding on how the script will work now, thank you.

 

Made an example scene, which i hope shows the overstretch problem. The issue is not due to meshes overstretching to gravity, its because of the inertia of mass. With increasing density the inertia tend to stretch the meshes infinitely when moving the mesh, like in the scene. But in the end you want that inertia effect, because it gives the cloth object its nice motion.

So you´ll find 3 stripes in the save file and all have the same values, escept the density:

-Red is the desired form, with a high density. It should have the motion, but not the overstretch.

-Blue has the desired non-stretch form, but lacks the desired motion, its just too stiff.

-And black is the approach to keep the desired form with the desired motion. Tried to change density accordingly to prevent overstretching, but wasn´t succesful.

 

After some thoughts, i´d say getting the information from the edges. When edges exceed a defined length, like length limit 110%, script should give the 2 linked verts to decrease their density, but with transition. Also we can work with Editable Poly mesh, since i do that, too. Then i can find the right "edge length to density ratio".

 

Im really curious, how the features you mentioned will look in action. This should be implemented into the software with the next update, it really improves working with the cloth mod alot  👍

 

Message 10 of 12

ads_royje
Alumni
Alumni

Hi @cerakecera !

 

Thanks for your file!! 🙂

 

Great to see what you are looking for!

 

I played a bit with the file, if you have time to check this one out:
https://autode.sk/38J5ZRE

Let me know if you can access the video or not and if that can be of any help at all, or not!

Message 11 of 12

cerakecera
Advocate
Advocate

Hi

 

Please don´t hesitate to make longer videos! It´s a pleasure to see someone else having so much fun with cloth physics 😊

Indeed it is possible to reach acceptable results with the stretch value, or shear. In my experience both are similar. But you also witnessed what happens when velocity of the node changes. I tried alot of property combinations to solve the problem, but without success.

There is another issue when trying to solve overstretching with stretch/shear value. When applying too much of it on cloth objects like a balloon, ball or purse then their volumes starting to shrink. Here is another scene that shows the problem. You can change the values from shear to stretch with similar results.

This example is more like my current project, which are meshes with volumes.

That is why i really think that scripting the right density at the right time is the way to go. It doesn´t have any "side effects".

Well i hope it won´t have any 😅

 
Message 12 of 12

denisT.MaxDoctor
Advisor
Advisor

@cerakecera wrote:

 

-Red is the desired form, with a high density. It should have the motion, but not the overstretch.

-Blue has the desired non-stretch form, but lacks the desired motion, its just too stiff.

-And black is the approach to keep the desired form with the desired motion. Tried to change density accordingly to prevent overstretching, but wasn´t succesful.

What kind of material do you want to simulate? What are desired size and mass of your ribbon (in world units)? 

 

The Cloth modifier tries to use physics parameters for simulation. So they are not abstract, but they are hard to be intuitively set.

What I see in your example is just the wrong settings. If you have a video reference of desired motion please give it to us. 

For me it's interesting to bind 'mesh stretching factor' and other simulation parameters (which technically can be animated and scripted). But as I said, my guess that the task just needs the right settings.

0 Likes