in mujoco_worldgen/objs/obj.py [0:0]
def compile(self, random_state, world_params):
# Some children are in placements that need to be randomly drawn
# Preprocess the list of children to draw placements for all of those
for placement_name, children in list(self.children.items()):
if placement_name in self.placements:
continue # Valid placement, nothing more to do
# Select a placement from a set of candidates for each child
candidates = self.placements.keys()
matches = [
pn for pn in candidates if pn.startswith(placement_name)]
assert len(matches) > 0, (
"No match found in {} for {}".format(placement_name, self.name))
for child in children: # Each gets an individual random draw
choice = random_state.choice(matches)
if choice not in self.children:
self.children[choice] = []
self.children[choice].append(child)
# Remove old placement
del self.children[placement_name]
if self._material is not None:
self._material.generate(random_state, world_params, None)
for placement_name in self.children.keys():
max_tries = 10
for _ in range(max_tries):
placement = self.placements[placement_name]
for child, _ in self.children[placement_name]:
child.size, child.placement = None, None
placement_size = np.array(placement['size'], dtype=np.float)
placement_size[0] -= 2 * world_params.placement_margin
placement_size[1] -= 2 * world_params.placement_margin
child.generate(random_state, world_params, placement_size)
if not child.placeable:
continue
assert child.size is not None, "missing size {}".format(
child)
assert child.placements is not None, "missing placements {}".format(
child)
child.relative_position = None
success = self.place(placement_name, self.children[placement_name],
random_state, world_params.placement_margin)
if success:
for child, _ in self.children[placement_name]:
if not child.placeable:
continue
assert child.relative_position is not None, "{}".format(
child)
break # next
if not success:
# TODO: debug level logging of _which_ placement failed
return False # one of our placements failed, so we failed
for placement_name in self.children.keys():
for child, _ in self.children[placement_name]:
if not child.compile(random_state, world_params):
return False
return True