gr4ph0s

GetNgon c4d

Apr 28th, 2017
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. import c4d
  2. import time
  3.  
  4. def deconnexion(obj):
  5.     doc = c4d.documents.GetActiveDocument()  
  6.    
  7.     settings = c4d.BaseContainer()
  8.     settings[c4d.MDATA_DISCONNECT_PRESERVEGROUPS] = False
  9.     test = c4d.utils.SendModelingCommand(command = c4d.MCOMMAND_DISCONNECT,
  10.                                 list = [obj],
  11.                                 mode = c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,
  12.                                 bc = settings,
  13.                                 doc = doc)
  14.    
  15. def get_poly_info(nbr,id_poly):
  16.     poly_connected = []
  17.     pli = nbr.GetPolyInfo(id_poly)["face"]  
  18.     for face in pli:
  19.         if face != c4d.NOTOK:
  20.             poly_connected.append(face)
  21.            
  22.     return list(set(poly_connected))
  23.    
  24. def get_ngonv3(obj):
  25.     save_before = obj.GetClone()
  26.  
  27.     deconnexion(obj)
  28.    
  29.     all_poly_done = []
  30.     all_ngons = []
  31.    
  32.     nbr = c4d.utils.Neighbor()
  33.     nbr.Init(obj)
  34.     for i in xrange(obj.GetPolygonCount()):
  35.         poly_to_do = []
  36.         poly_ngon  = []
  37.         poly_to_do.append(i)
  38.        
  39.         for id_poly in poly_to_do:
  40.             if not id_poly in all_poly_done:
  41.                 buffer_poly_info = get_poly_info(nbr,id_poly)
  42.                
  43.                 poly_to_do += buffer_poly_info
  44.                
  45.                 poly_ngon.append(id_poly)
  46.                
  47.                 all_poly_done.append(id_poly)
  48.                
  49.         if len(poly_ngon):
  50.             all_ngons.append(poly_ngon)
  51.                
  52.     save_before.CopyTo(obj,0)
  53.     return all_ngons
  54.  
  55. def GetAllNgons(obj):
  56.  
  57.     """Returns a list of ngons & polygons. Its all n-gons as they first appear based on poly_id then edge order.
  58.  
  59.    Followed by all non-ngon polys in their poly order.
  60.  
  61.    
  62.  
  63.    Output will looks something like:
  64.  
  65.    [[0, 2, 3], [4, 6], [1], [5]]
  66.  
  67.    """
  68.  
  69.  
  70.  
  71.     neighbor = c4d.utils.Neighbor()
  72.  
  73.     neighbor.Init(obj)
  74.  
  75.    
  76.  
  77.     polys = obj.GetAllPolygons()
  78.  
  79.     poly_count = len(polys)
  80.  
  81.     ngon_count = obj.GetNgonCount()
  82.  
  83.    
  84.  
  85.     ngon_edges_compact = obj.GetNgonEdgesCompact()  # N-gon info for each polygon
  86.  
  87.     non_ngons = []
  88.  
  89.     ngon_polys = []
  90.  
  91.     ngons = None
  92.  
  93.    
  94.  
  95.     edges = []
  96.  
  97.    
  98.  
  99.     # Get NGon Polygons
  100.  
  101.     for poly_id in xrange(poly_count):
  102.  
  103.         edge_value = ngon_edges_compact[poly_id]  # Bitmask w/ slots for each edge in a quad
  104.  
  105.        
  106.  
  107.         # No need to calculate n-gons if there aren't any
  108.  
  109.         if not edge_value:
  110.  
  111.             non_ngons.append([poly_id])  # Store it as a one item list to match format of ngons
  112.  
  113.             continue
  114.  
  115.        
  116.  
  117.         poly = polys[poly_id]
  118.  
  119.         poly_info = neighbor.GetPolyInfo(poly_id)
  120.  
  121.        
  122.  
  123.         for side in xrange(4):        
  124.  
  125.             # Skip the fourth "edge" of triangles.
  126.  
  127.             if side==2 and (poly.c == poly.d):
  128.  
  129.                 continue
  130.  
  131.            
  132.  
  133.             edge = None
  134.  
  135.            
  136.  
  137.             if side == 0:
  138.  
  139.                 edge = (poly.a, poly.b)
  140.  
  141.             elif side == 1:
  142.  
  143.                 edge = (poly.b, poly.c)
  144.  
  145.             elif side == 2:
  146.  
  147.                 edge = (poly.c, poly.d)
  148.  
  149.             elif side == 3:
  150.  
  151.                 edge = (poly.d, poly.a)
  152.  
  153.                    
  154.  
  155.             # N-Gon Edge
  156.  
  157.             if not (edge_value & (1 << side) == 0):
  158.  
  159.                 if poly_id not in ngon_polys:
  160.  
  161.                     ngon_polys.append(poly_id)
  162.  
  163.                        
  164.  
  165.                 # Get the neighborning hidden ngon poly, should be safe to assume there will always be one.
  166.  
  167.                 adjacent_poly = neighbor.GetNeighbor(edge[0], edge[1], poly_id)
  168.  
  169.                
  170.  
  171.                 if ngons is None:
  172.  
  173.                     ngon = [poly_id, adjacent_poly]
  174.  
  175.                     ngons = [ngon]
  176.  
  177.                 else:
  178.  
  179.                     # Add this polygon to an existing set if one exists
  180.  
  181.                     present = False
  182.  
  183.                     for i, poly_set in enumerate(ngons):
  184.  
  185.                        
  186.  
  187.                         if adjacent_poly in poly_set:
  188.  
  189.                             ngons[i] = ngons[i][:] + [poly_id]
  190.  
  191.                             present = True
  192.  
  193.                            
  194.  
  195.                     if not present:
  196.  
  197.                         ngons.append([poly_id, adjacent_poly])
  198.  
  199.                
  200.  
  201.         for i, poly_set in enumerate(ngons):
  202.  
  203.             ngons[i] = list(set(ngons[i]))
  204.  
  205.            
  206.  
  207.    
  208.  
  209.     ngons = ngons + non_ngons
  210.  
  211.    
  212.  
  213.     return ngons
  214.  
  215.  
  216. def main():
  217.     t = time.time()    
  218.     doc = c4d.documents.GetActiveDocument()
  219.     obj = doc.GetActiveObject()
  220.     print GetAllNgons(obj)
  221.     print time.time() - t
  222.    
  223.     c4d.EventAdd()
  224.  
  225.     t = time.time()    
  226.     doc = c4d.documents.GetActiveDocument()
  227.     obj = doc.GetActiveObject()
  228.     print get_ngonv3(obj)
  229.     print time.time() - t
  230.  
  231.  
  232. if __name__=='__main__':
  233.     main()
Add Comment
Please, Sign In to add comment