Skip to content

Commit

Permalink
added struct function for spec bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronYoung5 committed Dec 16, 2024
1 parent 0cf5500 commit 287256e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions python/mujoco/codegen/generate_spec_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ def _value_binding_code(

return f'{classname}.def_property({",".join(def_property_args)});'

def _struct_binding_code(
field: ast_nodes.AnonymousStructDecl, classname: str = '', varname: str = ''
) -> str:
code = ''
name = classname + varname.title()
# explicitly generate for nested fields with arrays
if any(isinstance(f.type, ast_nodes.ArrayType) for f in field.fields):
for subfield in field.fields:
code += _binding_code(subfield, name)
# generate for the struct itself
field = ast_nodes.ValueType(name=name)
code += _value_binding_code(field, classname, varname)
return code

def _array_binding_code(
field: ast_nodes.ArrayType, classname: str = '', varname: str = ''
Expand Down Expand Up @@ -227,13 +240,7 @@ def _binding_code(field: ast_nodes.StructFieldDecl, key: str) -> str:
if isinstance(field.type, ast_nodes.ValueType):
return _value_binding_code(field.type, key, field.name)
elif isinstance(field.type, ast_nodes.AnonymousStructDecl):
code = ""
if field.name in ['headlight', 'rgba']:
for subfield in field.type.fields:
code += _binding_code(subfield, 'mjVisual'+field.name.title())
field.type = ast_nodes.ValueType(name='mjVisual'+field.name.title())
code += _value_binding_code(field.type, key, field.name)
return code
return _struct_binding_code(field.type, key, field.name)
elif isinstance(field.type, ast_nodes.PointerType):
return _ptr_binding_code(field.type, key, field.name)
elif isinstance(field.type, ast_nodes.ArrayType):
Expand Down

0 comments on commit 287256e

Please sign in to comment.