Advertisement
gr4ph0s

C4D - Mirroring Select

Feb 16th, 2017
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import c4d
  2. POINT = 0
  3. POLY = 1
  4.  
  5. def get_polygon_id_selected(obj):
  6. buffer_list = list()
  7. poly_count = obj.GetPolygonCount()
  8. sel_polys = obj.GetPolygonS()
  9.  
  10. list_polys = sel_polys.GetAll(poly_count)
  11. for i in xrange(poly_count):
  12. if list_polys[i]:
  13. buffer_list.append(i)
  14.  
  15. return buffer_list
  16.  
  17. def get_points_id_selected(obj):
  18. buffer_list = list()
  19. pts_count = obj.GetPointCount()
  20. sel_pts = obj.GetPointS()
  21.  
  22. list_pts = sel_pts.GetAll(pts_count)
  23. for i in xrange(pts_count):
  24. if list_pts[i]:
  25. buffer_list.append(i)
  26.  
  27. return buffer_list
  28.  
  29.  
  30. def get_nearest_vec(v1, v2, v_to_match):
  31. first_lenght = v_to_match - v1
  32. second_lenght = v_to_match - v2
  33. if first_lenght.GetLength() < second_lenght.GetLength():
  34. return v1
  35. else:
  36. return v2
  37.  
  38.  
  39. def get_nearest_pos_id(pos_to_match, list_pos):
  40. buffer_pos = None
  41. for pos in list_pos:
  42. if buffer_pos is None:
  43. buffer_pos = pos
  44. continue
  45.  
  46. buffer_pos = get_nearest_vec(buffer_pos, pos, pos_to_match)
  47.  
  48. return list_pos.index(buffer_pos)
  49.  
  50.  
  51. def get_symetrical_polygons(obj, X, Y, Z):
  52. if not isinstance(obj, c4d.PolygonObject):
  53. return
  54.  
  55. active_sel = get_polygon_id_selected(obj)
  56. if not len(active_sel):
  57. return
  58.  
  59. #Store each polygon center
  60. list_all_pos = list()
  61. all_polys = obj.GetAllPolygons()
  62. all_pts = obj.GetAllPoints()
  63. for poly in all_polys:
  64. a = all_pts[poly.a]
  65. b = all_pts[poly.b]
  66. c = all_pts[poly.c]
  67. d = all_pts[poly.d]
  68. moyenne = (a + b + c + d) / 4
  69. list_all_pos.append(moyenne)
  70.  
  71.  
  72. #Get pos of selected
  73. polys_to_search = list()
  74. for poly_id in active_sel:
  75. buffer_x = list_all_pos[poly_id].x
  76. buffer_y = list_all_pos[poly_id].y
  77. buffer_z = list_all_pos[poly_id].z
  78. polys_to_search.append(c4d.Vector(buffer_x, buffer_y, buffer_z))
  79.  
  80. #Find the inverse polygonId
  81. poly_id_to_select = list()
  82. for poly_pos in polys_to_search:
  83. if X:
  84. poly_pos.x *= -1
  85. if Y:
  86. poly_pos.y *= -1
  87. if Z:
  88. poly_pos.z *= -1
  89.  
  90. poly_id_to_select.append(get_nearest_pos_id(poly_pos, list_all_pos))
  91.  
  92. return poly_id_to_select
  93.  
  94. def get_symetrical_points(obj, X, Y, Z):
  95. if not isinstance(obj, c4d.PointObject):
  96. return
  97.  
  98. active_sel = get_points_id_selected(obj)
  99. if not len(active_sel):
  100. return
  101.  
  102. #Store each points pos
  103. list_all_pos = obj.GetAllPoints()
  104.  
  105. #Get pos of selected
  106. pts_to_search = list()
  107. for poly_id in active_sel:
  108. buffer_x = list_all_pos[poly_id].x
  109. buffer_y = list_all_pos[poly_id].y
  110. buffer_z = list_all_pos[poly_id].z
  111. pts_to_search.append(c4d.Vector(buffer_x, buffer_y, buffer_z))
  112.  
  113. #Find the inverse pointsId
  114. pts_id_to_select = list()
  115. for pts_pos in pts_to_search:
  116. if X:
  117. pts_pos.x *= -1
  118. if Y:
  119. pts_pos.y *= -1
  120. if Z:
  121. pts_pos.z *= -1
  122.  
  123. pts_id_to_select.append(get_nearest_pos_id(pts_pos, list_all_pos))
  124.  
  125. return pts_id_to_select
  126.  
  127. def set_selection(doc, obj, selected_id, mode):
  128. print selected_id
  129. doc.StartUndo()
  130. doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
  131.  
  132. if mode == POLY:
  133. selection = obj.GetPolygonS()
  134.  
  135. elif mode == POINT:
  136. selection = obj.GetPointS()
  137.  
  138. if not selection:
  139. return
  140.  
  141. for i in selected_id:
  142. selection.Select(i)
  143.  
  144. doc.EndUndo()
  145. c4d.EventAdd()
  146.  
  147. def main():
  148. X = True
  149. Y = False
  150. Z = False
  151.  
  152. doc = c4d.documents.GetActiveDocument()
  153. if not doc:
  154. return
  155.  
  156. obj = doc.GetActiveObject()
  157. if not obj:
  158. return
  159.  
  160. if doc.GetMode() == c4d.Mpolygons:
  161. id_to_select = get_symetrical_polygons(obj, X, Y, Z)
  162. set_selection(doc, obj, id_to_select, POLY)
  163.  
  164. if doc.GetMode() == c4d.Mpoints:
  165. id_to_select = get_symetrical_points(obj, X, Y, Z)
  166. set_selection(doc, obj, id_to_select, POINT)
  167.  
  168. if __name__=='__main__':
  169. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement