-
-
Save cmontesano/78083e8175e9c2ba312da0e05bb393d5 to your computer and use it in GitHub Desktop.
""" | |
This is a python port of my old Select Loop C++ plugin for Cinema 4D. Rather | |
than porting this to R20 and recompiling, distributing, etc I decided to port | |
it to Python for use as a Script manager plugin. | |
Just import this script into your Script Manager and add it to a toolbar | |
or assign a shortcut. | |
MIT License | |
Copyright 2019 Christopher Montesano | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is furnished | |
to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import c4d | |
from c4d.utils import Neighbor | |
from c4d import gui | |
def get_opposite_edge(poly, p1, p2): | |
if (p1 == p2): | |
return None | |
if (poly.c == poly.d): # triangle | |
return None | |
points = (p1, p2) | |
if poly.a in points and poly.b in points: | |
return poly.c, poly.d | |
if poly.b in points and poly.c in points: | |
return poly.a, poly.d | |
if poly.c in points and poly.d in points: | |
return poly.a, poly.b | |
if poly.d in points and poly.a in points: | |
return poly.b, poly.c | |
return None | |
def get_shared_points(poly1, poly2): | |
shared_points = set() | |
for pta in (poly1.a, poly1.b, poly1.c, poly1.d): | |
for ptb in (poly2.a, poly2.b, poly2.c, poly2.d): | |
if pta == ptb: | |
shared_points.add(pta) | |
return list(shared_points) | |
def select_poly_loop(obj, nth=1, sim=False): | |
if obj is None: | |
raise ValueError("No object selected") | |
if not isinstance(obj, c4d.PolygonObject): | |
raise ValueError("Not a polygon object") | |
cply = obj.GetPolygonCount() | |
cpnt = obj.GetPointCount() | |
bsel = obj.GetPolygonS() | |
if cply == 0: | |
raise ValueError("No polygons found") | |
if bsel.GetCount() != 2: | |
raise ValueError("Exactly two polygons must be selected") | |
plys = obj.GetAllPolygons() | |
selected_ids = [] | |
for index, selected in enumerate(bsel.GetAll(cply)): | |
if not selected: | |
continue | |
selected_ids.append(index) | |
assert(len(selected_ids) == 2) | |
poly1 = plys[selected_ids[0]] | |
poly2 = plys[selected_ids[1]] | |
shared_points = get_shared_points(poly1, poly2) | |
if len(shared_points) != 2: | |
raise ValueError("Selected polygons must share an edge") | |
n = Neighbor() | |
n.Init(obj) | |
for poly_index in range(2): | |
this_poly_index = selected_ids[poly_index] | |
pt1 = shared_points[0] | |
pt2 = shared_points[1] | |
for i in range(cply): | |
nply = n.GetNeighbor(pt1, pt2, this_poly_index) | |
if nply is None: | |
break | |
this_poly_index = nply | |
bsel.Select(nply) | |
if i % nth != 0: | |
bsel.Toggle(nply) | |
opposite_edge = get_opposite_edge(plys[this_poly_index], pt1, pt2) | |
if opposite_edge is None: | |
break | |
pt1, pt2 = opposite_edge | |
if pt1 == shared_points[0] and pt2 == shared_points[1]: | |
break | |
obj.Message(c4d.MSG_CHANGE) | |
c4d.EventAdd() | |
def main(): | |
nth = gui.InputDialog("Select every nth polygon:", "1") | |
nth = int(nth) | |
try: | |
select_poly_loop(op, nth) | |
except ValueError as err: | |
gui.MessageDialog(str(err)) | |
if __name__=='__main__': | |
main() |
Make sure that you are selecting two polygons that are touching. They need to share exactly two points. If you already have two polygons selected that appear to be touching then you might have disconnected geometry. Running an optimize command might help.
It needs those two touching polygons to determine the loop direction. If you select "2" (every other polygon) or "3" (every 3rd polygon) it will deselect one of the neighboring polygons.
If you select two neighboring polygons and choose a number greater than one you should get a pretty good idea for how it works.
Thanks for your amazing contribution!
I modify the script a bit to:
- Support jumping select points of Spline object, according to this link
- Support selecting multiple loops with offsets, e.g., when
nth
is 5, you can select all polygons whose idx (% nth) are in [0,2,3]. - Fix IndexError when using
GetNeighbor
on plane object polygons
The most recently updated version of this script could be found here since I might not update it here in time.
"""
Jump Select
(Original name: Select Loop)
MIT License
Copyright 2019 Christopher Montesano
Modified by Hansimov, 2020.03
"""
import c4d
from c4d.utils import Neighbor
from c4d import gui
def get_opposite_edge(poly, p1, p2):
if (p1 == p2) or (poly.c == poly.d):
return None
points = (p1, p2)
poly_abcd = [poly.a, poly.b, poly.c, poly.d]
poly_oppo = []
for ele in poly_abcd:
if ele not in points:
poly_oppo.append(ele)
if poly_oppo != []:
return poly_oppo[:2]
else:
return None
def get_shared_points(poly1, poly2):
shared_points = set()
for pta in (poly1.a, poly1.b, poly1.c, poly1.d):
for ptb in (poly2.a, poly2.b, poly2.c, poly2.d):
if pta == ptb:
shared_points.add(pta)
return list(shared_points)
def select_poly_loop(obj, nth, sim=False):
if obj is None:
raise ValueError("No object selected")
if not isinstance(obj, c4d.PolygonObject):
raise ValueError("Not a polygon object")
cply = obj.GetPolygonCount()
cpnt = obj.GetPointCount()
bsel = obj.GetPolygonS()
if cply == 0:
raise ValueError("No polygons found")
if bsel.GetCount() != 2:
raise ValueError("Must select if and only if 2 edges!")
offsets = get_offsets(nth)
plys = obj.GetAllPolygons()
selected_ids = []
for index, selected in enumerate(bsel.GetAll(cply)):
if not selected:
continue
selected_ids.append(index)
assert(len(selected_ids) == 2)
poly1 = plys[selected_ids[0]]
poly2 = plys[selected_ids[1]]
shared_points = get_shared_points(poly1, poly2)
if len(shared_points) != 2:
raise ValueError("Selected polygons must share an edge!")
n = Neighbor()
n.Init(obj)
for poly_index in range(2):
this_poly_index = selected_ids[poly_index]
pt1 = shared_points[0]
pt2 = shared_points[1]
for i in range(cply):
try:
nply = n.GetNeighbor(pt1, pt2, this_poly_index)
# This IndexError happens on `plane` object polygons
except IndexError as err:
gui.MessageDialog("IndexError of GetNeighbor!")
break
if nply in [None,-1]:
break
this_poly_index = nply
bsel.Select(nply)
if not i%nth in offsets:
bsel.Toggle(nply)
opposite_edge = get_opposite_edge(plys[this_poly_index], pt1, pt2)
if opposite_edge is None:
break
pt1, pt2 = opposite_edge
if pt1 == shared_points[0] and pt2 == shared_points[1]:
break
obj.Message(c4d.MSG_CHANGE)
c4d.EventAdd()
def select_spline_point_loop(op,nth):
sel = op.GetPointS()
sel.DeselectAll()
cnt = op.GetPointCount()
offsets = get_offsets(nth)
for i in range(cnt):
if i%nth in offsets: sel.Select(i)
c4d.EventAdd()
def get_offsets(nth):
if nth == 1:
return [0]
offset_list = gui.InputDialog("Set offsets (int, 0-{}), use space to split several offsets".format(nth-1), "1")
offset_list = offset_list.split()
offsets = []
for offset in offset_list:
offsets.append(min(int(offset),nth-1))
return offsets
def main():
op = doc.GetActiveObject()
if not op:
gui.MessageDialog("No active object!", c4d.GEMB_ICONSTOP)
return False
if type(op) == c4d.PolygonObject:
nth = int(gui.InputDialog("Set nth (int) of polys:", "2"))
try:
select_poly_loop(op, nth)
except ValueError as err:
gui.MessageDialog(str(err), c4d.GEMB_ICONSTOP)
elif type(op) == c4d.SplineObject:
nth = int(gui.InputDialog("Set nth (int) of points:", "2"))
try:
select_spline_point_loop(op,nth)
except ValueError as err:
gui.MessageDialog(str(err), c4d.GEMB_ICONSTOP)
else:
return False
if __name__=='__main__':
main()
Nice update, and good catch on the IndexError. I expect that would happen when it reaches a boundary of a broken loop.
Hi there, thanks for your code.
I can't seem to get mine to work, I always get the error the selected polygons must share an edge. And I want to select every 1&3rd polygons.
Please can you get back to me soon