Hey MarcAndre,

this should do the trick ...


""""
from win32com.client.dynamic import Dispatch


POLYMESH_TYPE = "polymsh"
POLYCLS_TYPE = "poly"


def fracture_by_clusters(inobjs):
  """
  Fracture a mesh considering its clusters. Should keep materials, uvs and
whatever, since
  objects are duplicated.
  """
  Application.SetUserPref("SCR_CMDLOG_ENABLED", False)

  # list ? pass : list
  if type(inobjs) not in (list, tuple):
    inobjs = (inobjs, )

  # loop over objects
  for inobj in inobjs:
    # if not polymesh -> out
    if inobj.Type != POLYMESH_TYPE:
      continue

    geo = inobj.ActivePrimitive.Geometry
    # we skip on empty clusters
    clusters = [c for c in geo.Clusters if c.Elements.Count > 0]
    if len(clusters) > 0:
      # we get all indices to make ops
      indices = frozenset(geo.Polygons.IndexArray)
      # we will calculate the "non-exist" cluster
      rest = indices.copy()
      # we duplicate the mesh to get the same number of "existing" clusters
      dups = tuple(Application.SIDuplicate(inobj, len(clusters)))

      # we loop over cluster/mesh
      for cluster, dup in zip(clusters, dups):
        subc = cluster.CreateSubComponent()
        polys = subc.ComponentCollection
        if subc.ClusterType != POLYCLS_TYPE:
          # we turn cluster elements into polygons
          polys = polys.NeighborPolygons()
          # dispatch troubles
        elements = frozenset(Dispatch(polys).IndexArray)
        others = list(indices - elements)
        op = Application.ApplyTopoOp("DeleteComponent",
"{0}.poly{1}".format(dup.FullName, str(others)))
        Application.FreezeObj(op)
        # set the name
        dup.Name = cluster.Name
        # get the rest
        rest = rest - elements

      # rest / last cluster
      if len(rest) > 0:
        others = list(indices - rest)
        # nothing to remove
        if others != list():
          dup = Application.Duplicate(inobj, 1)(0)
          op = Application.ApplyTopoOp("DeleteComponent",
"{0}.poly{1}".format(dup.FullName, str(others)))
          Application.FreezeObj(op)
          dup.Name = inobj.Name + "_Rest"

      Application.DeleteObj(inobj)

  return None


""""

jo







2012/4/25 Martin <[email protected]>

> Hi, a few weeks ago I wrote an script that may help you.
>
> It separates all polygon clusters in the selected object, or selected
> polygon clusters, or selected polygon components,
> and keep their properties (or at least some of them).
>
> The script logic is something like this:
> Duplicate the selected object.
> Delete unneeded polygons (unselected clusters) from the new object.
> Take the polygon cluster material and delete it. (if the source was a
> poly-cluster, the new object won't have poly-clusters)
>
> Run it while clicking Ctrl and it will delete the source.
>
> Since it is duplicating and deleting instead of just extracting a polygon
> cluster, it is a little slow if the object is a very high poly model with
> lots of poly clusters.
> I used Duplicate because I wanted to keep the envelope weights in the
> extracted objects, and that's the only way I know.
>
> https://dc299.4shared.com/download/qpTKS78N/mSeparate.js
>
> It's just an script so you can't do one click undo.
> I may release a plugin version soon.
>
> Martin Yara
>
>
> On Thu, Apr 26, 2012 at 12:16 AM, Marc-Andre Carbonneau <
> [email protected]> wrote:
>
>> Hi, ****
>>
>> me again…if you haven’t notice, we really need a TD tool writer here. ;)*
>> ***
>>
>> ** **
>>
>> Ok, I can’t find a tool or script that turns clusters into objects but
>> keep its materials.****
>>
>> Anybody has such a tool or knows where to find one?  I searched the web
>> but hélas!****
>>
>> Thanks****
>>
>> MAC****
>>
>> ** **
>>
>
>

Reply via email to