-
Notifications
You must be signed in to change notification settings - Fork 3
/
boolean_utilities.py
105 lines (83 loc) · 2.92 KB
/
boolean_utilities.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import maya.cmds as cmds
from pymel.all import Callback
# TEST Reset the scene to the original state
def BU_TestReset():
cmds.select(all = True)
cmds.delete()
# TEST Create two objects that will work with booleans
def BU_TestCreate():
cmds.polyCube(n = 'Object001')
cmds.polyCube(n = 'Object002')
cmds.move(0, 0, 0, 'Object001')
cmds.move(0.5, 0.5, 0.5, 'Object002')
def BU_DiffIntersection():
selection = cmds.ls(sl=True)
# If you don't have exactly two meshes selected it ends the function
if len(selection) > 2 or len(selection) < 2:
print "You have to select two elements"
return
firstObj = selection[0]
secondObj = selection[1]
# I duplicate the objects to use it again
firstObjCopy = cmds.duplicate(firstObj)
secondObjCopy = cmds.duplicate(secondObj)
# First I do the difference between the first and the second object
cmds.polyCBoolOp(firstObj, secondObj, op=2)
# Then I do the intersection
cmds.polyCBoolOp(firstObjCopy, secondObjCopy, op=3)
def BU_IntersectionPreserveMeshes():
selection = cmds.ls(sl=True)
# If you don't have exactly two meshes selected it ends the function
if len(selection) > 2 or len(selection) < 2:
print "You have to select two elements"
return
firstObj = selection[0]
secondObj = selection[1]
# I duplicate the objects to use it again
firstObjCopy = cmds.duplicate(firstObj)
secondObjCopy = cmds.duplicate(secondObj)
# This will be used later as well
secondObjCopy2 = cmds.duplicate(secondObj)
# First I do the difference between the first and the second object
cmds.polyCBoolOp(firstObj, secondObj, op=2)
# Then I do the intersection
intersObj = cmds.polyCBoolOp(firstObjCopy, secondObjCopy, op=3)
# This will get preserve all the meshes used for the intersection
intersObjCopy = cmds.duplicate(intersObj)
cmds.polyCBoolOp(secondObjCopy2, intersObjCopy, op=2)
def BU_FunctionalTest1():
BU_TestReset()
BU_TestCreate()
cmds.select('Object001')
cmds.select('Object002', add=True)
BU_DiffIntersection()
def BU_FunctionalTest2():
BU_TestReset()
BU_TestCreate()
cmds.select('Object002')
cmds.select('Object001', add=True)
BU_DiffIntersection()
def BU_FunctionalTest3():
BU_TestReset()
BU_TestCreate()
cmds.select('Object001')
cmds.select('Object002', add=True)
BU_IntersectionPreserveMeshes()
def BU_FunctionalTest4():
BU_TestReset()
BU_TestCreate()
cmds.select('Object002')
cmds.select('Object001', add=True)
BU_IntersectionPreserveMeshes()
def BU_InitUI():
bu_win_name = 'diff_intersection'
if cmds.window(bu_win_name, q=True, ex=True):
cmds.deleteUI(bu_win_name)
cmds.window(bu_win_name, t='Boolean Utilities')
cmds.window(bu_win_name, e=True, height=100, width=300, sizeable=False)
cmds.columnLayout(adj=True)
cmds.button(l='Difference + Intersection', height=50, c=Callback(BU_DiffIntersection))
cmds.button(l='Intersection (Preserve Meshes)', height=50, c=Callback(BU_IntersectionPreserveMeshes))
cmds.setParent('..')
cmds.showWindow(bu_win_name)
BU_InitUI()