Skip to content

Commit

Permalink
Merge branch 'master' of github.com:/dfki-ric/phobos
Browse files Browse the repository at this point in the history
  • Loading branch information
hwiedPro committed Sep 26, 2023
2 parents c3d779f + ed52516 commit 685c201
Show file tree
Hide file tree
Showing 12 changed files with 787 additions and 121 deletions.
69 changes: 62 additions & 7 deletions phobos/blender/io/blender2phobos.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,62 @@ def deriveInterface(obj):
**annotations
)

def deriveAnnotationHelper(value, name, parent, obj):
"""
Resolves macros
"""
if "GA_macros" in obj:
if [parent, name] in obj["GA_macros"]: # This is a macro
effParent = sUtils.getEffectiveParent(obj)
# $parent
if obj["$include_parent"] and effParent:
while "$parent" in value:
index = value.index("$parent")
propertyIndex = index+len("$parent")
# $parent.{prop}, print property value or null
if propertyIndex+2 < len(value) and value[propertyIndex:propertyIndex+2] == ".{":
propertyEndIndex = value.index("}", propertyIndex)
prop = value[propertyIndex+2:propertyEndIndex].strip()
endReplace = propertyEndIndex
if prop in effParent:
replace = str(effParent[prop])
else:
replace = "null"
else: # $parent without property, print name
endReplace = propertyIndex-1
replace = effParent.name
value = value[:index]+replace+value[endReplace+1:]
# $transform
if obj["$include_transform"] and effParent:
while "$transform" in value:
index = value.index("$transform")
indexTail = index+len("$transform")
split = value[indexTail:].split(maxsplit=1)
tail = split[0]
pose = deriveObjectPose(obj, effParent)
if tail[0] != ".":
tail = ".xyz"
replaceEnd = indexTail
else:
replaceEnd = indexTail+len(tail)
try:
replace = getattr(pose, tail[1:])
if type(replace) == tuple:
string = ""
for i in range(len(replace)):
v = replace[i]
v = str(v)
if i > 0:
string += ", "
string += v
replace = "(" + string + ")"
except AttributeError as e:
print(f"Unknown tail {tail} for $transform")
replace = f"transform{tail} (unknown)"

value = value[:index] + str(replace) + value[replaceEnd:]

return value

def deriveAnnotation(obj):
"""Derives the annotation info of an annotation object.
Expand All @@ -540,17 +596,16 @@ def deriveAnnotation(obj):
if hasattr(v, "to_list"):
v = v.to_list()
elif "PropertyGroup" in repr(type(v)):
v = {_k: _v for _k, _v in v.items()}
v = {_k: deriveAnnotationHelper(_v, _k, k, obj) for _k, _v in v.items()}
else:
v = deriveAnnotationHelper(v, k, "", obj)
props[k] = v

props = misc.deepen_dict(props)

name = obj.split(":")[1]
return representation.GenericAnnotation(
GA_category=obj.split(":")[0],
GA_name=name if not name.startswith("unnamed") else None,
GA_parent=obj.parent.get("link/name", obj.parent.name),
GA_parent_type=obj.parent.phobostype,
GA_parent=obj.parent.get("link/name", obj.parent.name) if obj.parent else None,
GA_parent_type=obj.parent.phobostype if obj.parent else None,
GA_transform=deriveObjectPose(obj),
**props
)
Expand Down Expand Up @@ -842,7 +897,7 @@ def deriveRobot(root, name='', objectlist=None):
# [TODO v2.1.0] Re-add lights and SRDF support

for named_annotation in [deriveAnnotation(obj) for obj in objectlist if obj.phobostype == 'annotation']:
robot.add_categorized_annotation(named_annotation["$name"], {k: v for k, v in named_annotation.items() if k.startswith("$")})
robot.categorized_annotations.append(named_annotation)

robot.assert_validity()
return robot
15 changes: 11 additions & 4 deletions phobos/blender/io/phobos2blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ def createAnnotation(ga: representation.GenericAnnotation, parent=None, size=0.1
annot_obj = bUtils.createPrimitive(
f"{ga.GA_category}:{ga.GA_name if ga.GA_name is not None else 'unnamed'}",
'box',
[1, 1, 1],
(1, 1, 1),
player=defs.layerTypes['annotation'],
plocation=None if parent is None else parent.matrix_world.to_translation(),
#plocation=None if parent is None else parent.matrix_world.to_translation(), #TODO
phobostype='annotation'
)
annot_obj.scale = (size,) * 3
Expand All @@ -522,11 +522,14 @@ def createAnnotation(ga: representation.GenericAnnotation, parent=None, size=0.1
else:
annot_obj["$include_parent"] = False

annot_obj["$include_transform"] = ga.GA_transform is None
annot_obj["$include_transform"] = ga._GA_transform is None

props = ga.to_yaml()

for k, v in misc.flatten_dict(props).items():
# TODO: Check if type has to be stored
props.pop("GA_parent_type", None)

for k, v in props.items():
annot_obj[k] = v

return annot_obj
Expand Down Expand Up @@ -580,6 +583,10 @@ def createRobot(robot: core.Robot):
for interface in robot.interfaces:
newobjects.append(createInterface(interface, newlinks[interface.parent]))

log("Creating annotations...", 'INFO')
for anno in robot.generic_annotations:
newobjects.append(createAnnotation(anno))

# [TODO v2.1.0] Re-Add SRDF support
# log("Creating groups...", 'INFO')
# if 'groups' in model and model['groups']:
Expand Down
24 changes: 11 additions & 13 deletions phobos/blender/operators/editing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
from ..model import links as modellinks
from ..phobosgui import prev_collections
from ..phoboslog import log, ErrorMessageWithBox, WarnMessageWithBox
from ..operators.generic import addObjectFromYaml, DynamicProperty
from ..operators.generic import addObjectFromYaml, DynamicProperty, AddAnnotationsOperator, \
EditAnnotationsOperator, AnnotationsOperator
from ..utils import blender as bUtils
from ..utils import editing as eUtils
from ..utils import general as gUtils
Expand Down Expand Up @@ -1625,8 +1626,9 @@ def execute(self, context):
if validInput:
for joint in (obj for obj in context.selected_objects if obj.phobostype == 'link'):
context.view_layer.objects.active = joint
assert joint.parent is not None and joint.parent.phobostype == "link", \
f"You need to have a link parented to {joint.name} before you can create a joint"
if joint.parent is None or joint.parent.phobostype != "link":
ErrorMessageWithBox(f"Link {joint.name} has to be parented to another link before you can define a joint")
return {'CANCELLED'}
if len(self.name) > 0:
joint["joint/name"] = self.name.replace(" ", "_")
jUtils.setJointConstraints(
Expand Down Expand Up @@ -2091,13 +2093,7 @@ def draw(self, context):
class AddSensorOperator(Operator):
"""Add a sensor at the position of the selected object.
It is possible to create a new link for the sensor on the fly. Otherwise,
the next link in the hierarchy will be used to parent the sensor to.
Args:
Returns:
"""
the next link in the hierarchy will be used to parent the sensor to"""

bl_idname = "phobos.add_sensor"
bl_label = "Add Sensor"
Expand Down Expand Up @@ -2199,10 +2195,9 @@ def draw(self, context):
for i in range(len(self.sensorProperties)):
name = self.sensorProperties[i].name.replace('_', ' ')

# use the dynamic props name in the GUI, but without the type id
self.sensorProperties[i].draw(layout, name)
self.sensorProperties[i].draw(layout, self.sensorProperties)
layout.label(text="You can add custom properties under")
layout.label(text="Object Properties > Custom Properties")
layout.label(text="Object Properties > Custom Properties", icon="OBJECT_DATA")

def invoke(self, context, event):
"""
Expand Down Expand Up @@ -3563,6 +3558,9 @@ def poll(self, context):

classes = (
DynamicProperty,
AnnotationsOperator,
AddAnnotationsOperator,
EditAnnotationsOperator,
SafelyRemoveObjectsFromSceneOperator,
MoveToSceneOperator,
SortObjectsToLayersOperator,
Expand Down
Loading

0 comments on commit 685c201

Please sign in to comment.