Dynamic cloth under stress



  • @englishbob
    @F_Verbaas
    @trekkiegrrrl
    I'm glad you all like it.



  • @amethystpendant said in Dynamic cloth under stress:

    @adp That is brilliant! I did for a while script out the vertex groups I would use in Poser (from Blender) then manually edit the PZ2 file pasting them in, I had no idea you could load vert groups like this. Can this be done for polygroups as well, as this would mean I could create this file on the fly in python and then load it.

    Sorry, I overlooked your post (the site is not so much tablet friendly).

    What polygroups do you mean? Mesh parts?

    You can anything define in a Poser filefragment what originally is written from Poser into such a file. That is what Poser understands. Not more. Poser reads what it understands and left the rest alone (the reason why Poser 9 can read P11 files).

    Poser accepts partly written sections. As long as the format is correct (curly brackets) Poser reads in and accepts what it can identify.

    Exception is mesh. Poser includes a complete OBJ file in his own files and wants to read meshes as a whole. So writing only vt entries (uv-map) in a file will result in an error.



  • @adp
    I have tried this script now. It works pretty fast to create a pose file with a load of vertices. A vertex group is formed, but it is empty.
    As far as I can see the file is formatted correctly, except that there are three curly closing brackets at the end where nett effect of the file opening is two levels. Is one opening bracket missing?

    For others playing with it: if the object processsed in the script is a prop, make sure there is at least one figure in the scene before or the pose file can not be loaded. The figure is not affected, but it needs to be there to have some checks pass successfully.



  • @F_Verbaas Sooo sorry! I didn't realize that I changed something in the meantime. I really thought it worked and posted it untested.

    This version IS tested – and it works perfect!

    from __future__ import print_function
    
    import os
    
    try:
        import poser
    
        SCENE = poser.Scene()
    except ImportError:
        raise Exception("This script needs Poser")
    
    from PIL import Image as IMG
    
    try:
        START_PATH
    except NameError:
        START_PATH = poser.ContentRootLocation()
    
    PZ2_HEADER = """{
    \tversion 
    \t{
    \t\tnumber 9
    \t}
    
    """
    
    
    ##############################################################################
    
    def is_red(R, G, B):
        return R > 160 and G < 40 and B < 40
    
    
    ##############################################################################
    
    def generate_Vertexlist_from_UVmap(imgname, actorname=None, groupname="uv_generated", pz2_file=None):
        actor = image = None
    
        if actorname is None:
            actor = SCENE.CurrentActor()
        elif isinstance(actorname, (str, unicode)):
            actor = SCENE.Actor(actorname)
    
        if not isinstance(actor, poser.ActorType):
            raise ValueError("Actor not found or none given")
    
        # make sure a prop or bodypart of a figure is selected
        if not (actor.IsProp() or actor.IsBodyPart()):
            raise Exception("Select a Prop or Bodypart first!")
    
        geom = actor.Geometry()
        if geom is None:
            raise ValueError("Actor '%s' has no geometry" % actor.Name())
    
        sets = geom.Sets()
    
        # open image
        if not IMG.isImageType(imgname):
            try:
                image = IMG.open(imgname)
            except:
                raise ValueError("Couldn't open image '%s'" % imgname)
    
        image = image.convert("RGB")
        # width and high to compute UV-coordinates
        w, h = image.size[0] - 1, image.size[1] - 1
    
        seen = set()  # already seen vertices
        ar = list()  # list of vertex indices
    
        for tp in geom.TexPolygons():
            idx = tp.Start()
            for iv, tv in enumerate(tp.TexVertices()):
                ux, uy = tv.U(), tv.V()
                set_idx = sets[idx + iv]
                if not set_idx in seen:
                    seen.add(set_idx)
                    x = round(w * ux, 0) % w
                    y = (h - round(h * uy, 0)) % h
                    c = image.getpixel((x, y))
                    if is_red(*c):
                        # red-part must be 128 or greater
                        ar.append(set_idx)
    
        if pz2_file is None:
            pz2_file = imgname.rsplit(".")[0] + ".pz2"
    
        if groupname is not None:
            groupname = os.path.basename(imgname).rsplit(".")[0] + "_vt"
    
        try:
            fh = open(pz2_file, "w")
        except Exception as err:
            print("can't open weightmapfile '%s'" % pz2_file)
        else:
            print(PZ2_HEADER, file=fh)
            tab = lambda n: "\t" * n
            t = 1
            print("%sactor $CURRENT" % (tab(t)), file=fh)
            print("%s{" % tab(t), file=fh)
            print("%svertsGroup %s" % (tab(t + 1), groupname), file=fh)
            print("%s{" % tab(t + 1), file=fh)
            for vidx in sorted(ar):
                print("%sv %s" % (tab(t + 2), vidx), file=fh)
    
            print("%s}" % tab(t + 1), file=fh)
            print("%s}" % tab(t), file=fh)
    
            print("}", file=fh)
            fh.close()
    
            return pz2_file
    
    
    ##############################################################################
    
    if __name__ == "__main__":
        dialog = poser.DialogFileChooser(poser.kDialogFileChooserOpen,
                                         None,
                                         "Select an UV-mapped Image",
                                         START_PATH)
        if dialog.Show():
            if os.path.isfile(dialog.Path()):
                START_PATH = os.path.dirname(dialog.Path())
                pz2_file = generate_Vertexlist_from_UVmap(imgname=dialog.Path())
                SCENE.LoadLibraryPose(pz2_file)
                SCENE.DrawAll()
                print("PZ2-file created:", pz2_file)
            else:
                print("Not an usable file:", dialog.Path())
    
    
    


  • @adp what filename do you save the script as?



  • @anomalaus said in Dynamic cloth under stress:

    @adp what filename do you save the script as?

    ??? as you like...

    I'm busy working on it. Few extensions, more flexible, usable as lib (I often thought: Would be nice to have a script to mask out some morphed areas based on UV; now I have it).

    The script gets a final filename later (any ideas?)



  • @adp said in Dynamic cloth under stress:

    Another thing one can make with UV-maps is creating morph. May come in handy if one has to move some vertices you simply cant reach from within Poser.

    You're right, this answers the question "which vertex is that?" which I can't easily discover with my current set-up. This script will be even more useful than I thought!

    @adp said in Dynamic cloth under stress:

    The script gets a final filename later (any ideas?)

    I named mine UV2vertex.py, if that helps. :)



  • Maybe we should open another thread for Python scripts.



  • @adp Yeah, drifting off topic a little. :)


  • Poser Ambassadors

    @adp Maybe a whole new Topic forum outside the Poser forum that is only aimed at python scripting.



  • @Ghostman said in Dynamic cloth under stress:

    @adp Maybe a whole new Topic forum outside the Poser forum that is only aimed at python scripting.

    I believe Rendo has a Python forum.



  • @adp
    Never mind. Happens to me all the time.
    Tried it again now. It works like a charm.
    It still needs a figure in the scene before it can load a pose file on a prop, but hey if that were the biggest problem with PoserPython life would be completely different.
    Thank you again.



  • @Ghostman
    Yes, guess some platform for PoserPython would be extremely useful. There is one on Rendo but it does not have much traffic. Good thing is that some of the old heroes still are linked there.
    Since the site redesign (Please do not start a discussion on that here) access to and searching the history of that forum is difficult and not many users will find their way in that very rich resource.



  • @adp said in Dynamic cloth under stress:

    @amethystpendant said in Dynamic cloth under stress:

    @englishbob said in Dynamic cloth under stress:

    @adp said in Dynamic cloth under stress:

    I made a script for my modeler able to output vertices into a file that Poser can read. This is a big plus, because it's practically impossible to select lots of vertices in Poser.
    (More later)

    Interesting. In the past I've defined special material zones to make it easier to select the dynamic zones I wanted, but of course the minumum selection size is then a facet. You can't define a vertex or a line of vertices this way. You're also left with additional materials in your cloth which may not be useful.

    I'll be interested to see how that works.

    The way I would (have) do(ne) this is to have a material group for the edge one facet deep, another after that, either for the hem or just the cloth. Go into grouping and assign the 1 face depth material as soft decorated. Then go to dynamic group and add the adjacent material, that will effectively remove the second layer of verts from the soft decorated group leaving you with just the one row of verts on the edge

    With Poser 11 I can define a Soft Decorated group and another Dynamic Group with the same vertices. I made this with the waistborder from the trousers. And Poser shows me the vertices in both groups.

    I know it was like you say in previous versions.

    Not sure how that is supposed to work, as I believe the simulation is run against verts, so how can the same vert be treated as both dynamic and soft decorated, or I am assuming constrained, rigid or choreographed.



  • @amethystpendant wouldn't that be because the dynamics calculations are always dealing with pairs of vertices and the forces between them, so one vertex could be a member of a facet in a dynamic group and also a member of a facet in a soft decorated group?



  • @amethystpendant said in Dynamic cloth under stress:

    @adp said in Dynamic cloth under stress:

    @amethystpendant said in Dynamic cloth under stress:

    @englishbob said in Dynamic cloth under stress:

    @adp said in Dynamic cloth under stress:

    I made a script for my modeler able to output vertices into a file that Poser can read. This is a big plus, because it's practically impossible to select lots of vertices in Poser.
    (More later)

    Interesting. In the past I've defined special material zones to make it easier to select the dynamic zones I wanted, but of course the minumum selection size is then a facet. You can't define a vertex or a line of vertices this way. You're also left with additional materials in your cloth which may not be useful.

    I'll be interested to see how that works.

    The way I would (have) do(ne) this is to have a material group for the edge one facet deep, another after that, either for the hem or just the cloth. Go into grouping and assign the 1 face depth material as soft decorated. Then go to dynamic group and add the adjacent material, that will effectively remove the second layer of verts from the soft decorated group leaving you with just the one row of verts on the edge

    With Poser 11 I can define a Soft Decorated group and another Dynamic Group with the same vertices. I made this with the waistborder from the trousers. And Poser shows me the vertices in both groups.

    I know it was like you say in previous versions.

    Not sure how that is supposed to work, as I believe the simulation is run against verts, so how can the same vert be treated as both dynamic and soft decorated, or I am assuming constrained, rigid or choreographed.

    A vertex can only be assigned to ONE Cloth group at a time. Once assigned, it will be treated like every other vertex assigned to that cloth group.



  • @eclark1849 said in Dynamic cloth under stress:

    @amethystpendant said in Dynamic cloth under stress:

    @adp said in Dynamic cloth under stress:

    @amethystpendant said in Dynamic cloth under stress:

    @englishbob said in Dynamic cloth under stress:

    @adp said in Dynamic cloth under stress:

    I made a script for my modeler able to output vertices into a file that Poser can read. This is a big plus, because it's practically impossible to select lots of vertices in Poser.
    (More later)

    Interesting. In the past I've defined special material zones to make it easier to select the dynamic zones I wanted, but of course the minumum selection size is then a facet. You can't define a vertex or a line of vertices this way. You're also left with additional materials in your cloth which may not be useful.

    I'll be interested to see how that works.

    The way I would (have) do(ne) this is to have a material group for the edge one facet deep, another after that, either for the hem or just the cloth. Go into grouping and assign the 1 face depth material as soft decorated. Then go to dynamic group and add the adjacent material, that will effectively remove the second layer of verts from the soft decorated group leaving you with just the one row of verts on the edge

    With Poser 11 I can define a Soft Decorated group and another Dynamic Group with the same vertices. I made this with the waistborder from the trousers. And Poser shows me the vertices in both groups.

    I know it was like you say in previous versions.

    Not sure how that is supposed to work, as I believe the simulation is run against verts, so how can the same vert be treated as both dynamic and soft decorated, or I am assuming constrained, rigid or choreographed.

    A vertex can only be assigned to ONE Cloth group at a time. Once assigned, it will be treated like every other vertex assigned to that cloth group.

    That is exactly what @adp was NOT saying, he was saying he had the same verts in multiple groups

    @anomalaus said in Dynamic cloth under stress:

    @amethystpendant wouldn't that be because the dynamics calculations are always dealing with pairs of vertices and the forces between them, so one vertex could be a member of a facet in a dynamic group and also a member of a facet in a soft decorated group?

    No that's the point, dynamics works on verts NOT facets that's why you can have a ring of just verts in a cloth group or even a single vert, as I've done with choreographed group before now



  • @amethystpendant I understand that the grouping selection is based on vertices, rather than facets, but the dynamics simulations couldn't work at all (apart from gravity) if you had a (ridiculous) cloth consisting of only a single vertex. There would be nothing to calculate strain and forces between. You need at least a pair of vertices, and they don't need to be facet edge pairs, you can calculate forces across a facet diagonal, as well.



  • @anomalaus said in Dynamic cloth under stress:

    @amethystpendant I understand that the grouping selection is based on vertices, rather than facets, but the dynamics simulations couldn't work at all (apart from gravity) if you had a (ridiculous) cloth consisting of only a single vertex. There would be nothing to calculate strain and forces between. You need at least a pair of vertices, and they don't need to be facet edge pairs, you can calculate forces across a facet diagonal, as well.

    Agreed, but you can have a group of just one vert, irrespective of how many edges/facets that vert belongs to.



  • @amethystpendant sure. I think I see now that we are perhaps arguing about different things. I suppose it is confusing as to how the simulation would determine which parameters to use for it's calculations on a vertex which is a member of multiple dynamics/decoration groups.