-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathduplicateSpaceScaleRotate.py
More file actions
88 lines (66 loc) · 3.34 KB
/
duplicateSpaceScaleRotate.py
File metadata and controls
88 lines (66 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import rhinoscriptsyntax as rs
import pprint
import random
'''
# Copyright 2020 Houtan Bastani
'''
def duplicateSpaceScaleRotate():
print("\n duplicateSpaceScaleRotate commands \n")
obj_ids = rs.GetObjects("Select object(s) from which to create the rectangular matrix", 0, True, True)
if obj_ids is None: return
box = rs.BoundingBox(obj_ids)
if not isinstance(box, list): return
origin = rs.PointDivide(rs.PointAdd(box[0], box[6]), 2)
nXdups = rs.GetInteger("Max Duplications X", 1, 1)
nYdups = rs.GetInteger("Max Duplications Y", 1, 1)
nZdups = rs.GetInteger("Max Duplications Z", 1, 1)
nXspace = rs.GetReal("Spacing X", 1, 0)
rXspace = rs.GetReal("Spacing X rand", 0, 0, 100) / 100
nYspace = rs.GetReal("Spacing Y", 1, 0)
rYspace = rs.GetReal("Spacing Y rand", 0, 0, 100) / 100
nZspace = rs.GetReal("Spacing Z", 1, 0)
rZspace = rs.GetReal("Spacing Z rand", 0, 0, 100) / 100
nXscale = rs.GetReal("Scale X", 1, 0)
rXscale = rs.GetReal("Scale X rand", 0, 0, 100) / 100
nYscale = rs.GetReal("Scale Y", 1, 0)
rYscale = rs.GetReal("Scale Y rand", 0, 0, 100) / 100
nZscale = rs.GetReal("Scale Z", 1, 0)
rZscale = rs.GetReal("Scale Z rand", 0, 0, 100) / 100
nXrotate = rs.GetReal("Rotate X", 0, 0, 360)
rXrotate = rs.GetReal("Rotate X rand", 0, 0, 100) / 100
nYrotate = rs.GetReal("Rotate Y", 0, 0, 360)
rYrotate = rs.GetReal("Rotate Y rand", 0, 0, 100) / 100
nZrotate = rs.GetReal("Rotate Z", 0, 0, 360)
rZrotate = rs.GetReal("Rotate Z rand", 0, 0, 100) / 100
rKeep = 1 - rs.GetReal("Percent to erase", 0, 0, 100) / 100
endpt = rs.GetPoint("To point")
sample_near_val = lambda val, rand: random.uniform(-rand*val, rand*val)
translations = []
# Copy Points with Spacing
for k in range(nZdups):
for j in range(nYdups):
for i in range(nXdups):
newpt = [origin[0] + i * nXspace + sample_near_val(nXspace, rXspace),
origin[1] + j * nYspace + sample_near_val(nYspace, rYspace),
origin[2] + k * nZspace + sample_near_val(nZspace, rZspace)]
translations = translations + [rs.VectorCreate(endpt, newpt)]
nObjs = len(translations)
objs_to_keep = random.sample(range(nObjs), int(round(nObjs*rKeep)))
translations = [translations[i] for i in objs_to_keep]
copied_objs = []
for tr in translations:
copied_objs = copied_objs + rs.CopyObjects(obj_ids, tr)
for obj in copied_objs:
bb = rs.BoundingBox(obj)
if bb:
center_point = rs.PointDivide(rs.PointAdd(bb[0], bb[6]), 2)
# pt = rs.SurfaceVolumeCentroid(obj)
rs.ScaleObject(obj, center_point, [nXscale + sample_near_val(nXscale, rXscale),
nYscale + sample_near_val(nYscale, rYscale),
nZscale + sample_near_val(nZscale, rZscale)])
plane = rs.ViewCPlane()
rs.RotateObject(obj, center_point, nXrotate + sample_near_val(nXrotate, rXrotate), plane.XAxis)
rs.RotateObject(obj, center_point, nYrotate + sample_near_val(nYrotate, rYrotate), plane.YAxis)
rs.RotateObject(obj, center_point, nZrotate + sample_near_val(nZrotate, rZrotate), plane.ZAxis)
if( __name__ == "__main__" ):
duplicateSpaceScaleRotate()