Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import c4d
- POINT = 0
- POLY = 1
- def get_polygon_id_selected(obj):
- buffer_list = list()
- poly_count = obj.GetPolygonCount()
- sel_polys = obj.GetPolygonS()
- list_polys = sel_polys.GetAll(poly_count)
- for i in xrange(poly_count):
- if list_polys[i]:
- buffer_list.append(i)
- return buffer_list
- def get_points_id_selected(obj):
- buffer_list = list()
- pts_count = obj.GetPointCount()
- sel_pts = obj.GetPointS()
- list_pts = sel_pts.GetAll(pts_count)
- for i in xrange(pts_count):
- if list_pts[i]:
- buffer_list.append(i)
- return buffer_list
- def get_nearest_vec(v1, v2, v_to_match):
- first_lenght = v_to_match - v1
- second_lenght = v_to_match - v2
- if first_lenght.GetLength() < second_lenght.GetLength():
- return v1
- else:
- return v2
- def get_nearest_pos_id(pos_to_match, list_pos):
- buffer_pos = None
- for pos in list_pos:
- if buffer_pos is None:
- buffer_pos = pos
- continue
- buffer_pos = get_nearest_vec(buffer_pos, pos, pos_to_match)
- return list_pos.index(buffer_pos)
- def get_symetrical_polygons(obj, X, Y, Z):
- if not isinstance(obj, c4d.PolygonObject):
- return
- active_sel = get_polygon_id_selected(obj)
- if not len(active_sel):
- return
- #Store each polygon center
- list_all_pos = list()
- all_polys = obj.GetAllPolygons()
- all_pts = obj.GetAllPoints()
- for poly in all_polys:
- a = all_pts[poly.a]
- b = all_pts[poly.b]
- c = all_pts[poly.c]
- d = all_pts[poly.d]
- moyenne = (a + b + c + d) / 4
- list_all_pos.append(moyenne)
- #Get pos of selected
- polys_to_search = list()
- for poly_id in active_sel:
- buffer_x = list_all_pos[poly_id].x
- buffer_y = list_all_pos[poly_id].y
- buffer_z = list_all_pos[poly_id].z
- polys_to_search.append(c4d.Vector(buffer_x, buffer_y, buffer_z))
- #Find the inverse polygonId
- poly_id_to_select = list()
- for poly_pos in polys_to_search:
- if X:
- poly_pos.x *= -1
- if Y:
- poly_pos.y *= -1
- if Z:
- poly_pos.z *= -1
- poly_id_to_select.append(get_nearest_pos_id(poly_pos, list_all_pos))
- return poly_id_to_select
- def get_symetrical_points(obj, X, Y, Z):
- if not isinstance(obj, c4d.PointObject):
- return
- active_sel = get_points_id_selected(obj)
- if not len(active_sel):
- return
- #Store each points pos
- list_all_pos = obj.GetAllPoints()
- #Get pos of selected
- pts_to_search = list()
- for poly_id in active_sel:
- buffer_x = list_all_pos[poly_id].x
- buffer_y = list_all_pos[poly_id].y
- buffer_z = list_all_pos[poly_id].z
- pts_to_search.append(c4d.Vector(buffer_x, buffer_y, buffer_z))
- #Find the inverse pointsId
- pts_id_to_select = list()
- for pts_pos in pts_to_search:
- if X:
- pts_pos.x *= -1
- if Y:
- pts_pos.y *= -1
- if Z:
- pts_pos.z *= -1
- pts_id_to_select.append(get_nearest_pos_id(pts_pos, list_all_pos))
- return pts_id_to_select
- def set_selection(doc, obj, selected_id, mode):
- print selected_id
- doc.StartUndo()
- doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
- if mode == POLY:
- selection = obj.GetPolygonS()
- elif mode == POINT:
- selection = obj.GetPointS()
- if not selection:
- return
- for i in selected_id:
- selection.Select(i)
- doc.EndUndo()
- c4d.EventAdd()
- def main():
- X = True
- Y = False
- Z = False
- doc = c4d.documents.GetActiveDocument()
- if not doc:
- return
- obj = doc.GetActiveObject()
- if not obj:
- return
- if doc.GetMode() == c4d.Mpolygons:
- id_to_select = get_symetrical_polygons(obj, X, Y, Z)
- set_selection(doc, obj, id_to_select, POLY)
- if doc.GetMode() == c4d.Mpoints:
- id_to_select = get_symetrical_points(obj, X, Y, Z)
- set_selection(doc, obj, id_to_select, POINT)
- if __name__=='__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement