-
Notifications
You must be signed in to change notification settings - Fork 1
/
fence.py
92 lines (74 loc) · 2.66 KB
/
fence.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
#!/usr/bin/env python3
import boxx
from boxx import *
from boxx import np
import os
import sys
sys.path.append(".")
import bpy
import bpycv
import random
from bpycv.dataset_utils.dataset_generator import MetaDatasetGenerator
from cfg_utils import get_arguments, get_default_cfg
class FenceGenerator(MetaDatasetGenerator):
def __init__(self, cfg):
bpy.ops.wm.open_mainfile(
filepath=os.path.join(cfg.SOURCE_ASSET, "fence/fence.blend")
)
super().__init__(cfg)
self.hdri_manager = bpycv.HdriManager(
hdri_dir=os.path.join(cfg.SOURCE_ASSET, "shared/hdri"), category="indoor",
)
def generate_one(self, dirr, index, meta_seed=0):
[bpycv.remove_obj(obj) for obj in bpy.data.objects if obj.get("is_duplicate")]
cfg = self.cfg
random.seed(f"{cfg.DIR},{meta_seed},{index}")
bpy.context.scene.frame_set(0)
bpycv.remove_useless_data()
hdri_path = self.hdri_manager.sample()
bpycv.load_hdri_world(hdri_path)
cam_radius = random.uniform(1.5, 1.8)
cam_deg = random.uniform(45, 90)
bpycv.set_cam_pose(cam_radius=cam_radius, cam_deg=cam_deg)
instn = random.choice(cfg.OBJ_NUM_DIST)
fence = bpy.data.objects["fence"]
fence["is_artifact"] = True
for _ in range(instn - 1):
with bpycv.activate_obj(fence):
obj = bpycv.duplicate(fence, True)
obj["is_duplicate"] = True
objs = [obj for obj in bpy.data.objects if obj.get("is_artifact")]
for idx, obj in enumerate(objs):
obj["inst_id"] = idx + 1000
ENV_SIZE = 1.2
location = (
random.uniform(-ENV_SIZE, ENV_SIZE),
random.uniform(-ENV_SIZE, ENV_SIZE),
0.04 + 0.1 * idx,
)
rotation = (
0,
0,
random.random() * 2 * 3.1415,
)
obj.location = location
obj.rotation_euler = rotation
for i in range(120):
bpy.context.scene.frame_set(bpy.context.scene.frame_current + 1)
result = bpycv.render_data()
def qualify_result(result):
return True
if qualify_result(result):
result.save(dirr, index, save_blend=False)
else:
self.generate_one(dirr, index, meta_seed=meta_seed + 1)
def get_cfg():
cfg = get_default_cfg()
cfg.OBJ_NUM_DIST = [2, 3, 3, 3, 4, 4]
return cfg.clone()
if __name__ == "__main__":
args = get_arguments()
cfg = get_cfg()
cfg.merge_from_list_or_str(args.opts)
fence_gen = FenceGenerator(cfg)
fence_gen.generate_all()