-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
135 lines (99 loc) · 2.88 KB
/
utils.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
from . import app
from .common_utils import *
try:
log_to_file('/tmp/ootlog.txt')
except FileNotFoundError:
# Perhaps a Windows user can contribute a suitable log file path?
# For now, we just won't log on Windows. Add your own path here
# if you need to debug the tool.
# log_to_file(r"C:\Blah\OOT Scene Tool Log.txt")
pass
import bpy
import functools
import mathutils
def clear_collection(coll):
for obj in coll.objects:
coll.objects.unlink(obj)
obj.parent = None
print("REMOVE", obj)
bpy.data.objects.remove(obj)
for ch in coll.children:
clear_collection(ch)
coll.children.unlink(ch)
bpy.data.collections.remove(ch)
def empty_collection(name, parent=None):
if coll := bpy.data.collections.get(name):
clear_collection(coll)
else:
coll = bpy.data.collections.new(name)
if not parent:
parent = bpy.context.scene.collection
try:
parent.children.link(coll)
except:
pass
return coll
def move_to_collection(obj, collection):
collection.objects.link(obj)
try:
bpy.context.scene.collection.objects.unlink(obj)
except:
pass
def descendants(obj):
for x in obj.children:
yield x
yield from descendants(x)
def points_bounds(cos):
axis_vals = [
[co[axis] for co in cos]
for axis in [0, 1, 2]
]
return Bounds([
Range(min(vals), max(vals))
for vals in axis_vals
])
def object_bounds(obj):
return points_bounds([
obj.matrix_world @ mathutils.Vector(v)
for v in obj.bound_box
])
def objects_bounds(objs):
return functools.reduce(
bounds_union,
map(object_bounds, objs),
Bounds(),
)
def duplicate_object(obj, collection):
new_obj = obj.copy()
for coll in new_obj.users_collection:
coll.objects.unlink(new_obj)
collection.objects.link(new_obj)
new_obj.data = new_obj.data.copy()
return new_obj
def clip_object(obj1, obj2):
mod = obj1.modifiers.new('mod', 'BOOLEAN')
mod.operation = 'INTERSECT'
mod.object = obj2
mod.use_hole_tolerant = True
mod.use_self = True
#bpy.ops.object.modifier_apply(apply_as='DATA', modifier='mod')
def map_rect(from_rect, to_rect):
ax0, ay0 = from_rect.origin
bx0, by0 = to_rect.origin
aw, ah = from_rect.size
bw, bh = to_rect.size
return mathutils.Matrix([
[bw/aw, 0, bx0 - ax0 * bw/aw],
[ 0, bh/ah, by0 - ay0 * bh/ah],
[ 0, 0, 1]
])
class Image:
def __init__(self, key):
self.key = key
@property
def name(self):
return '_'.join(map(str, self.key))
@property
def render_path(self):
os.makedirs(app.scene.render_dir, exist_ok=True)
return f'{app.scene.render_dir}/{self.name}.png'