This is a script I just created to demonstrate the basic steps to perform.
It (hopefully) might trigger some to dabble in script-writing ...
The script was written for R20, but should run in more recent versions.
... if not, or if you need the script to perform additional things: check out https://developers.maxon.net/docs/Cinema4DPythonSDK/html/index.html and modify the script to your liking.
import c4d
# This script will split off the selected polygons from an object into a new object
# (c) 2022 Daniel Sterckx - 26-aug-2022
def state():
return c4d.CMD_ENABLED
def removeSelectedPolygons(obj, polyS):
# remove the selected polygons
c4d.utils.SendModelingCommand(command=c4d.MCOMMAND_DELETE,
list=[obj],
mode=c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,
doc=doc)
# Prior to R20 removing polygons would leave the points intact,
# resulting in orphan points, which need an additional step to be removed/cleaned-up
# The following lines of code can be removed for R20 and above
if c4d.GetC4DVersion() < 20000:
# remove unused points as a result of polygon removal
bc = c4d.BaseContainer()
bc.SetData(c4d.MDATA_OPTIMIZE_UNUSEDPOINTS,True)
c4d.utils.SendModelingCommand(c4d.MCOMMAND_OPTIMIZE, list = [obj], mode = c4d.MODIFY_ALL, bc=bc, doc = doc)
return
def main():
# Get the (single) selected object
obj = doc.GetActiveObject()
if not obj:
print("No object selected")
return
if not obj.IsInstanceOf(c4d.Opolygon):
print("Not a polygon object")
return
# Get the polygon selection
objPolyS = obj.GetPolygonS()
if objPolyS.GetCount() == 0:
print("No polygon(s) selected")
return
# Preapre the document for undo
doc.StartUndo()
# We duplicate the selected object, revert the polygon selection, and delete these selected polygons
# Since we cloned the "split-off" object from the original object it will have the "active flag" set,
# we thus remove the BIT_ACTIVE from the duplicated object to prevent it from being selected cloneObj = obj.GetClone()
cloneObj = obj.GetClone()
cloneObj.DelBit(c4d.BIT_ACTIVE)
doc.InsertObject(cloneObj, parent=None, pred=obj)
doc.AddUndo(c4d.UNDOTYPE_NEW, cloneObj)
clonePolyS = cloneObj.GetPolygonS()
clonePolyS.ToggleAll(0, cloneObj.GetPolygonCount())
removeSelectedPolygons(cloneObj, clonePolyS)
# We delete the original selected polygons on the original object
doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
removeSelectedPolygons(obj, objPolyS)
# Finalize the undo, and trigger Cinema4D that something was changes
doc.EndUndo()
c4d.EventAdd()
return
if __name__=='__main__':
main()