Skip to content

Instantly share code, notes, and snippets.

@donovankeith
Created November 9, 2016 19:36
Show Gist options
  • Save donovankeith/3c30a5bb22e3ba0f99af5b6e5cc73aeb to your computer and use it in GitHub Desktop.
Save donovankeith/3c30a5bb22e3ba0f99af5b6e5cc73aeb to your computer and use it in GitHub Desktop.
Welds the points of a cube so that it looks like a tent.
"""Weld Cube Points
Welds cube points together.
Usage Instructions:
1. Create some cubes.
2. Make them editable
3. Run this script.
"""
import c4d
def SquishPoints(cube):
"""Welds the top 4 points of a polygon Cube."""
if (cube is None) or (not cube.IsInstanceOf(c4d.Opolygon)):
return
points = cube.GetAllPoints()
point_id_groups = [[1,3],[5,7]] # Split into sub-lists so each can be welded in turn.
# Go through each set of points to weld.
for point_id_group in point_id_groups:
# Weld command isn't easily called via script, so we'll move the points we want to weld
# to the same position, then call the Optimize command.
# Calculate the center of the points
point_position_sum = c4d.Vector(0.0)
for point_id in point_id_group:
point_position_sum += points[point_id]
number_of_points = len(point_id_group)
center_point = point_position_sum / number_of_points
# Move points to the center
for point_id in point_id_group:
points[point_id] = center_point
# Store the changed points
cube.SetAllPoints(points)
def main():
# Get a list of active objects.
active_objects = doc.GetActiveObjects(flags=c4d.GETACTIVEOBJECTFLAGS_0)
# Start recording changes you'll later want to undo.
doc.StartUndo()
# Squish together the points for all selected objects
for obj in active_objects:
doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
SquishPoints(obj)
# Use the optimize command to weld them
c4d.CallCommand(14039, 14039) # Optimize...
# Let C4D know that something has happened and it should redraw.
doc.EndUndo()
c4d.EventAdd()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment