Should I use Python or Blender for clothing simulation?

Well, it looks like I’m going to spend some time writing a clothing simulator. My question for you gurus is this:

Cloth simulation is calculation intensive. For each step (frame) up to ten equations can be calulated for each [edit]point[/edit] multiple times. Will python scripting be quick enough to do this in a reasonable amount of time (less than two or three minutes per frame) or should I look into modifying blender sources directly?

I balk at hacking the blender sources. My time is extremely limited and I don’t frankly have any real desire to rummage through tons of blender code.

Having said that, I expect this project to take some time to complete, so if I have to, I will.

Er, to rephrase, is it worth taking the time to learn blender’s internal structure to do this?

Start with Python and test it until it works well then try and include it into Blender. I’m not sure if it works with Blender but you can compile Python code and it’ll run faster because it’s not interpreting on the fly. I’m sure RipSting’s excellent fiber used compiled Python code at one time. One of the POVRay exporters does but I haven’t used it.

well i agree with osxrules, the best method of found for writing code for Blender is to start with Python and write a working script, then work towards internal code.

on the matter of speed it depends…are you use a voxel based clothing simulator or mesh based? I don’t believe a voxel based sim would run quick enough from Python however a mesh based sim probably would depending of course on the number of verts in the mesh.

the simple way to find out would be time all ten equations with dummy data which would be your worst case, the scale up for the number of times these could be called.

hope it works out, Blender could use cloth.

tim

Thanks for the quick responses. After some further discussions I’ve had it seems that in this particular case it is better to go straight to the 2.37 source and build on top of softbodies.

SO, I’m preparing to install everything necessary to work on a blender source tree on my system. The very first thing I am instructed to do is install python (reasonable).

Python wants to install Tcl/Tk (which, come to think of it, is also reasonable).

Now, I’ve already got two tcl/tk installations on my system, carefully crufted so as not to conflict. Active Tcl (for clicking on stuff to make it run) and tclkit (because I am doing some cross-platform development and want to bundle everything into a friendly executable starkit when I’m done).

What exactly is the python installation going to do to my current tcl/tk installation? Should I just uninstall ActiveTcl and go for it? I must have at least 8.4.

I hope this isn’t a silly Q. It’s late and my mind is getting more sluggish by the second…

Another approach could be to write it in C and use python to bridge the gap between the ‘library’ and blender, to keep it ‘independant’ from blender(i.e easier to maintain imho).

I would highly recommend this approach (I’m a python programmer, although I’m brand new to Blender).

However, there are good reasons to steer clear of diving into writing C source for such a thing (mainly that you will waste a lot of time — C is probably too low level for what you really want to do). Premature optimization is usually a bad idea. I used to program almost exclusively in C, and I didn’t get that much done. I use Python almost to the exclusion of C now, and I’m a lot more productive.

What I most want to call your attention to, however, is that there are already very good optimized math modules for Python (which are written in native C, so they run very fast, I mean):

Numeric – Old standby for doing array mathematics and image processing.

Numarry – rewrite of Numeric which is gaining popularity for a better (clearer) internal design, but is poorer than Numeric for SMALL arrays (it gains on larger arrays faster than Numeric does, IIRC).

Psyco – Optimizing Just-In-Time compiler for Python. Maybe python’s interpreter speed won’t be an issue.

Pyrex – Python/C bridge language which is essentially a compiled subset of Python (all it omits is some very advanced dynamic techniques which you probably shouldn’t use anyway — like defining classes and functions within functions), and of course speaks C as well. If you are okay with writing C code, you may find this superior to C-library-automatic-wrapper solutions like SWIG, but of course, those exist too.

(all of the above are free-licensed, GPL or Python License, IIRC).

There are other 3rd party tools like SciPy and so on, but I don’t think you’d be needing those for your project (I confess to knowing nothing about how clothing simulation is done, but I think it must be pretty math intensive and presumeably highly parallel).

That’s what I actually was initially considering.

The only real difficulty using C introduces is data structure, which environments like Python simplify for you. Hence in Python you can consider more what it is that you are doing, and less how you organize the encumbant data.

The main reasons for going straight to blender source are:

  • data structure is already provided (so I can think less about it and more about what I’m doing)
  • softbody calculations may provide most of the routines needed (so I don’t have to build a new wheel)
  • direct data manipulation
  • no porting into blender needed when done

What the python intermediary would be spending a lot of time and structure doing would just be sending mesh data back and forth between the executable and blender. This would involve a lot of string to/from float conversions, which would cripple any possible speed gained from compiled calculations. If I can directly link into blender via python that would be very convenient (i.e. does python provide shared memory for two separate processes?)

However, I haven’t yet had time to examine the blender source very carefully yet, so the final decision has yet to be firmly made. I’ll look into those modules you suggested. Thanks.

As for optimizations, bit-twaddling will not make so great a difference here as algorithm implementation. The thing that slows down math on modern systems isn’t the calculations but what is done between each one. For example, in Tcl, each expr is preceed by lexing and parsing and succeeded by printing to strings. Even high-level languages like C can get you behind the scenes as each float can be converted to a double before sending through some function or to the FPU (though this isn’t a problem in blender since it uses double internally).

Clothing simulation is actually fairly straight-forward. It is parallel only in that a piece of cloth is considered as a single system; each point is updated based only on its nearest neighbors’ attributes --but this can be done without regard to order. The main difficulty comes from the size of the step you can take as each one plus failures require multiple integrations.

The data to be stored between ‘steps’ is:

  • initial mesh (used for detangling and for obtaining natural spring lengths)
  • current mesh
  • previous mesh (used to get the current mesh’s velocity)

Each mesh involved (more than one object can be a mesh, and they can all be set to mutually interact) will also have various vertex groups attached allowing specific values to be overridden such as friction and mass.

What I would like to have is an ability to write, in python, functions which look something like:

gravity( frame_number, mesh_name, position )

which will provide the greatest user control over these things. But again, this introduces a layer between the value and its use for each point and each integration.

Well, I’m still learning how I want to do all this… This weekend probably I’ll spend some time getting my system set up to compile blender and start looking through the softbodies.

Oh yeah, will the Python install stomp all over my ActiveTcl?

howsabout implementing www.freecloth.enigmati.ca into blender?

edit: fixed the link.

It’s possible the implicit integrator and adaptive stepper from freecloth will be implemented, but I can’t figure out where it forces meshes to be regularly-rectangular discoids.

I’m not satisfied with simple cloth simulation like Topix and freecloth. I want full-fledged clothing, and without the artifacts introduced by ‘stitching’ together flat sheets.

What I’ll use is a simple modification of the standard mass-spring system but apply it to any valid mesh. AFAIK softbodies already implement the basics of this… What this means is that you can model cloths directly and get proper deformations.

Despite assertations to the contrary freecloth is fairly poorly documented, and I really don’t want to spend a lot of time analyzing and decoding the inner workings of an algorithm with such a major limitation.

A couple of months from now I’ll have a comprehensive work-up for you all to approve. Be patient :wink:

Er, that is, I know where it does it, but when I modify the code that does it, freecloth crashes. Meaning there’s an implicit assumption about the shape of the mesh in the algorithm.

Maybe just port this to python 2.3 / blender 2.3x and improve it. :wink:
http://www.ne.jp/asahi/viz/max/topix/Topix.html

This dose use c + python.

No, Topix is definitely not under consideration. Not only is it limited as I mentioned previously, the legal status of its source is questionable. Comparable, if not superior, algorithms exist.

I want full-fledged clothing, and without the artifacts introduced by ‘stitching’ together flat sheets.

Why don’t you take a look at Sweater then?
http://www-ui.is.s.u-tokyo.ac.jp/~takeo/research/cloth/index.html
I think I posted this link already somewhere…

Thanks for the input. Looks interesting. It is, however, an add-on process for pre-simulation setup. Hence I’ll consider it only after I have simulation done… Sorry.

There are a couple of papers out there that looks very interesting with fast routines and buckling. I will post it later then :slight_smile:

EDIT:
http://graphics.stanford.edu/papers/cloth-sig02/
http://graphics.snu.ac.kr/~kjchoi/cloth.htm
http://www.pixar.com/companyinfo/research/deb/index.html

Heh heh… You rock! :smiley:

Yeah, those are the very papers I was looking at. I’ve got a lot of homework to do… :frowning:

Bjornmose (softbodies coder) is making progress on cloth as well…

http://mitglied.lycos.de/mosebjorn/hidden/bmhomework1.avi

LetterRip