Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import c4d
- import time
- def deconnexion(obj):
- doc = c4d.documents.GetActiveDocument()
- settings = c4d.BaseContainer()
- settings[c4d.MDATA_DISCONNECT_PRESERVEGROUPS] = False
- test = c4d.utils.SendModelingCommand(command = c4d.MCOMMAND_DISCONNECT,
- list = [obj],
- mode = c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,
- bc = settings,
- doc = doc)
- def get_poly_info(nbr,id_poly):
- poly_connected = []
- pli = nbr.GetPolyInfo(id_poly)["face"]
- for face in pli:
- if face != c4d.NOTOK:
- poly_connected.append(face)
- return list(set(poly_connected))
- def get_ngonv3(obj):
- save_before = obj.GetClone()
- deconnexion(obj)
- all_poly_done = []
- all_ngons = []
- nbr = c4d.utils.Neighbor()
- nbr.Init(obj)
- for i in xrange(obj.GetPolygonCount()):
- poly_to_do = []
- poly_ngon = []
- poly_to_do.append(i)
- for id_poly in poly_to_do:
- if not id_poly in all_poly_done:
- buffer_poly_info = get_poly_info(nbr,id_poly)
- poly_to_do += buffer_poly_info
- poly_ngon.append(id_poly)
- all_poly_done.append(id_poly)
- if len(poly_ngon):
- all_ngons.append(poly_ngon)
- save_before.CopyTo(obj,0)
- return all_ngons
- def GetAllNgons(obj):
- """Returns a list of ngons & polygons. Its all n-gons as they first appear based on poly_id then edge order.
- Followed by all non-ngon polys in their poly order.
- Output will looks something like:
- [[0, 2, 3], [4, 6], [1], [5]]
- """
- neighbor = c4d.utils.Neighbor()
- neighbor.Init(obj)
- polys = obj.GetAllPolygons()
- poly_count = len(polys)
- ngon_count = obj.GetNgonCount()
- ngon_edges_compact = obj.GetNgonEdgesCompact() # N-gon info for each polygon
- non_ngons = []
- ngon_polys = []
- ngons = None
- edges = []
- # Get NGon Polygons
- for poly_id in xrange(poly_count):
- edge_value = ngon_edges_compact[poly_id] # Bitmask w/ slots for each edge in a quad
- # No need to calculate n-gons if there aren't any
- if not edge_value:
- non_ngons.append([poly_id]) # Store it as a one item list to match format of ngons
- continue
- poly = polys[poly_id]
- poly_info = neighbor.GetPolyInfo(poly_id)
- for side in xrange(4):
- # Skip the fourth "edge" of triangles.
- if side==2 and (poly.c == poly.d):
- continue
- edge = None
- if side == 0:
- edge = (poly.a, poly.b)
- elif side == 1:
- edge = (poly.b, poly.c)
- elif side == 2:
- edge = (poly.c, poly.d)
- elif side == 3:
- edge = (poly.d, poly.a)
- # N-Gon Edge
- if not (edge_value & (1 << side) == 0):
- if poly_id not in ngon_polys:
- ngon_polys.append(poly_id)
- # Get the neighborning hidden ngon poly, should be safe to assume there will always be one.
- adjacent_poly = neighbor.GetNeighbor(edge[0], edge[1], poly_id)
- if ngons is None:
- ngon = [poly_id, adjacent_poly]
- ngons = [ngon]
- else:
- # Add this polygon to an existing set if one exists
- present = False
- for i, poly_set in enumerate(ngons):
- if adjacent_poly in poly_set:
- ngons[i] = ngons[i][:] + [poly_id]
- present = True
- if not present:
- ngons.append([poly_id, adjacent_poly])
- for i, poly_set in enumerate(ngons):
- ngons[i] = list(set(ngons[i]))
- ngons = ngons + non_ngons
- return ngons
- def main():
- t = time.time()
- doc = c4d.documents.GetActiveDocument()
- obj = doc.GetActiveObject()
- print GetAllNgons(obj)
- print time.time() - t
- c4d.EventAdd()
- t = time.time()
- doc = c4d.documents.GetActiveDocument()
- obj = doc.GetActiveObject()
- print get_ngonv3(obj)
- print time.time() - t
- if __name__=='__main__':
- main()
Add Comment
Please, Sign In to add comment