-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
137 lines (119 loc) · 4.58 KB
/
tools.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# ----------------------------------------------------------
# support routines and general functions
# Author: PieterVdc
#
# ----------------------------------------------------------
# noinspection PyUnresolvedReferences
import bpy
from os import path
# --------------------------------------------------------------------
# Set normals
# True= faces to inside
# False= faces to outside
# --------------------------------------------------------------------
def set_normals(myobject, direction=False):
bpy.context.view_layer.objects.active = myobject
# go edit mode
bpy.ops.object.mode_set(mode='EDIT')
# select all faces
bpy.ops.mesh.select_all(action='SELECT')
# recalculate outside normals
bpy.ops.mesh.normals_make_consistent(inside=direction)
# go object mode again
bpy.ops.object.editmode_toggle()
# --------------------------------------------------------------------
# Remove doubles
# --------------------------------------------------------------------
def remove_doubles(myobject):
bpy.context.view_layer.objects.active = myobject
# go edit mode
bpy.ops.object.mode_set(mode='EDIT')
# select all faces
bpy.ops.mesh.select_all(action='SELECT')
# remove
bpy.ops.mesh.remove_doubles()
# go object mode again
bpy.ops.object.editmode_toggle()
# --------------------------------------------------------------------
# Add modifier (boolean)
# --------------------------------------------------------------------
def set_modifier_boolean(myobject, bolobject):
bpy.context.view_layer.objects.active = myobject
if bpy.context.view_layer.objects.active.name == myobject.name:
bpy.ops.object.modifier_add(type='BOOLEAN')
mod = myobject.modifiers[len(myobject.modifiers) - 1]
mod.operation = 'DIFFERENCE'
mod.object = bolobject
# --------------------------------------------------------------------
# Parent object (keep positions)
# --------------------------------------------------------------------
def parentobject(parentobj, childobj):
# noinspection PyBroadException
try:
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = parentobj
parentobj.select_set(True)
childobj.select_set(True)
bpy.ops.object.parent_set(type='OBJECT', keep_transform=False)
return True
except:
return False
# ------------------------------------------------------------------------------
# Remove all children objects
# ------------------------------------------------------------------------------
def remove_children(myobject):
# Remove children
for child in myobject.children:
# noinspection PyBroadException
try:
# noinspection PyBroadException
try:
# remove child relationship
for grandchild in child.children:
grandchild.parent = None
# remove modifiers
for mod in child.modifiers:
bpy.ops.object.modifier_remove(name=mod.name)
except:
pass
# clear child data
if child.type == 'MESH':
old = child.data
child.select_set(True)
bpy.ops.object.delete()
bpy.data.meshes.remove(old)
if child.type == 'CURVE':
child.select_set(True)
bpy.ops.object.delete()
except:
pass
# --------------------------------------------------------------------
# Get all parents
# --------------------------------------------------------------------
def get_allparents(myobj):
obj = myobj
mylist = []
while obj.parent is not None:
mylist.append(obj)
objp = obj.parent
obj = objp
mylist.append(obj)
return mylist