@@ -1016,11 +1016,15 @@ private static void DeserializeGeometricPrimitive(GameObject go, JObject primiti
10161016
10171017 #region URDF
10181018
1019- public XElement XML { get ; }
1020-
1019+ private List < Transform > _visualMeshesToExport = new List < Transform > ( ) ;
1020+ private List < Transform > _collisionMeshesToExport = new List < Transform > ( ) ;
10211021 protected void BuildURDFVisuals ( Transform visualTransform , XElement link , Vector3 anchorOffset ) {
10221022 // build 3d primitive if exists
10231023 MeshFilter meshFilter = visualTransform . GetComponent < MeshFilter > ( ) ;
1024+ if ( meshFilter == null ) {
1025+ // Check children
1026+ meshFilter = visualTransform . GetComponentInChildren < MeshFilter > ( ) ;
1027+ }
10241028 if ( meshFilter ) {
10251029
10261030 MeshRenderer meshRenderer = visualTransform . GetComponent < MeshRenderer > ( ) ;
@@ -1040,8 +1044,7 @@ protected void BuildURDFVisuals(Transform visualTransform, XElement link, Vector
10401044 if ( collider ) {
10411045 //TODO: add box collider
10421046 }
1043- }
1044- if ( meshFilter . sharedMesh . name . Contains ( "Sphere" ) ) {
1047+ } else if ( meshFilter . sharedMesh . name . Contains ( "Sphere" ) ) {
10451048 XElement sphere = new XElement ( "sphere" ) ;
10461049 float radius = visualTransform . localScale . x / 2.0f ;
10471050 sphere . SetAttributeValue ( "radius" , radius ) ;
@@ -1052,17 +1055,7 @@ protected void BuildURDFVisuals(Transform visualTransform, XElement link, Vector
10521055 //TODO: add box collider
10531056 }
10541057
1055- }
1056- // if (meshFilter.sharedMesh.name.Contains("Capsule")) {
1057- // collider = go.GetComponent<CapsuleCollider>();
1058- // primitiveJSON = new JObject(
1059- // new JProperty("type", "primitive.capsule"),
1060- // new JProperty("dimensions", go.transform.localScale.ToJSON()),
1061- // new JProperty("has_collisions", collider ? true : false),
1062- // new JProperty("color", meshRenderer.sharedMaterial.color.ToJSON())
1063- // );
1064- // }
1065- if ( meshFilter . sharedMesh . name . Contains ( "Cylinder" ) ) {
1058+ } else if ( meshFilter . sharedMesh . name . Contains ( "Cylinder" ) ) {
10661059 XElement cylinder = new XElement ( "cylinder" ) ;
10671060 float radius = visualTransform . localScale . x / 2.0f ;
10681061 float height = visualTransform . localScale . y * 2.0f ;
@@ -1076,19 +1069,22 @@ protected void BuildURDFVisuals(Transform visualTransform, XElement link, Vector
10761069 //TODO: add box collider
10771070 }
10781071
1072+ } else { // regular mesh so export meshes as OBJ
1073+ XElement mesh = new XElement ( "mesh" ) ;
1074+ mesh . SetAttributeValue ( "filename" , $ "{ visualTransform . name } .obj") ;
1075+ mesh . SetAttributeValue ( "scale" , visualTransform . localScale . ToXMLString ( ) ) ;
1076+ geometry . Add ( mesh ) ;
1077+
1078+ _visualMeshesToExport . Add ( visualTransform ) ;
10791079 }
10801080
10811081 if ( geometry . HasElements ) {
10821082 // build origin
10831083 Vector3 position = visualTransform . localPosition + anchorOffset ;
1084- Vector3 xyz = position . Unity2Ros ( ) ; //visualTransform.localPosition.Unity2Ros();
1084+ Vector3 xyz = position . Unity2Ros ( ) ;
10851085 Vector3 rpy = new Vector3 ( - visualTransform . localEulerAngles . z * Mathf . Deg2Rad ,
10861086 visualTransform . localEulerAngles . x * Mathf . Deg2Rad ,
10871087 - visualTransform . localEulerAngles . y * Mathf . Deg2Rad ) ;
1088- // Vector3 xyz = jointTransform.position.Unity2Ros();
1089- // Vector3 rpy = new Vector3(-transform.eulerAngles.z * Mathf.Deg2Rad,
1090- // transform.eulerAngles.x * Mathf.Deg2Rad,
1091- // -transform.eulerAngles.y * Mathf.Deg2Rad);
10921088
10931089 XElement origin = new XElement ( "origin" ) ;
10941090 origin . SetAttributeValue ( "xyz" , xyz . ToXMLString ( ) ) ;
@@ -1100,27 +1096,6 @@ protected void BuildURDFVisuals(Transform visualTransform, XElement link, Vector
11001096 link . Add ( visual ) ;
11011097 }
11021098
1103-
1104- // // add name
1105- // primitiveJSON.Add("name", go.name);
1106-
1107- // // add transform
1108- // primitiveJSON.Add("translation", go.transform.localPosition.ToJSON());
1109- // primitiveJSON.Add("rotation_quaternion", go.transform.localRotation.ToJSON());
1110- // primitiveJSON.Add("scale", go.transform.localScale.ToJSON());
1111-
1112-
1113- // // generate physics material if necessary (friction, restitution)
1114- // if (collider != null && collider.sharedMaterial != null) {
1115- // primitiveJSON.Add("physics_material",
1116- // new JObject(
1117- // new JProperty("bounciness", collider.sharedMaterial.bounciness),
1118- // new JProperty("dynamic_friction", collider.sharedMaterial.dynamicFriction),
1119- // new JProperty("static_friction", collider.sharedMaterial.staticFriction)
1120- // )
1121- // );
1122- // }
1123-
11241099 }
11251100
11261101
@@ -1153,89 +1128,7 @@ public void BuildURDFLink(XElement robot, Vector3 anchorOffset) {
11531128
11541129 }
11551130
1156- protected readonly struct URDFJoint {
1157- public URDFJoint ( ZOSimOccurrence child , ZOSimOccurrence parent , Vector3 anchor , Vector3 connectedAnchor ) {
1158- Child = child ;
1159- Parent = parent ;
1160- Anchor = anchor ;
1161- ConnectedAnchor = connectedAnchor ;
1162- }
1163-
1164-
1165- public ZOSimOccurrence Child {
1166- get ;
1167- }
1168-
1169- public ZOSimOccurrence Parent {
1170- get ;
1171- }
1172-
1173- public Vector3 Anchor {
1174- get ;
1175- }
1176-
1177- public Vector3 ConnectedAnchor {
1178- get ;
1179- }
1180-
1181- public static bool operator == ( URDFJoint a , URDFJoint b ) {
1182- return a . Equals ( b ) ;
1183- }
1184- public static bool operator != ( URDFJoint a , URDFJoint b ) {
1185- return ! a . Equals ( b ) ;
1186- }
1187-
1188- }
1189-
1190- public static void BuildURDF ( XElement robot , ZOSimOccurrence simOccurrence , Matrix4x4 baseTransform ) {
1191- List < URDFJoint > joints = new List < URDFJoint > ( ) ;
1192-
1193- simOccurrence . BuildURDFJoints ( robot , null , baseTransform , ref joints ) ;
1194-
1195-
1196- // build links
1197- HashSet < ZOSimOccurrence > links = new HashSet < ZOSimOccurrence > ( ) ;
1198- foreach ( URDFJoint joint in joints ) {
1199- if ( links . Contains < ZOSimOccurrence > ( joint . Parent ) == false ) { // build parent link if not exist
1200- Vector3 offset = - 1.0f * joint . ConnectedAnchor ;
1201- if ( joints [ 0 ] == joint ) { // if base joint do not apply any offset to parent link
1202- offset = Vector3 . zero ;
1203- }
1204- joint . Parent . BuildURDFLink ( robot , offset ) ;
1205- links . Add ( joint . Parent ) ;
1206- }
1207- if ( links . Contains < ZOSimOccurrence > ( joint . Child ) == false ) { // build child link if not exist
1208- Vector3 offset = - 1.0f * joint . ConnectedAnchor ;
1209- joint . Child . BuildURDFLink ( robot , offset ) ;
1210- links . Add ( joint . Child ) ;
1211- }
1212- }
1213-
1214- if ( joints . Count == 0 ) { // if we don't have any joints then create a dummy world link and joint to first link
1215-
1216- XElement link = new XElement ( "link" ) ;
1217- link . SetAttributeValue ( "name" , "World" ) ;
1218- robot . Add ( link ) ;
1219-
1220- simOccurrence . BuildURDFLink ( robot , Vector3 . zero ) ;
1221-
1222- XElement jointX = new XElement ( "joint" ) ;
1223- jointX . SetAttributeValue ( "name" , $ "World_to_{ simOccurrence . Name } ") ;
1224- jointX . SetAttributeValue ( "type" , "fixed" ) ;
1225-
1226- XElement parentX = new XElement ( "parent" ) ;
1227- parentX . SetAttributeValue ( "link" , "World" ) ;
1228- jointX . Add ( parentX ) ;
1229-
1230- XElement childX = new XElement ( "child" ) ;
1231- childX . SetAttributeValue ( "link" , simOccurrence . Name ) ;
1232- jointX . Add ( childX ) ;
1233- robot . Add ( jointX ) ;
1234-
1235- }
1236-
1237- }
1238-
1131+ #if BLAHBLAH
12391132 protected void BuildURDFJoints ( XElement robot , ZOSimOccurrence parent , Matrix4x4 worldJointMatrix , ref List < URDFJoint > joints ) {
12401133
12411134 // if we have a parent build a joint
@@ -1305,24 +1198,6 @@ protected void BuildURDFJoints(XElement robot, ZOSimOccurrence parent, Matrix4x4
13051198 }
13061199
13071200
1308- // build origin
1309-
1310- // Vector3 xyz = jointTransform.localPosition.Unity2Ros();
1311- // Vector3 rpy = new Vector3(-jointTransform.localEulerAngles.z * Mathf.Deg2Rad,
1312- // jointTransform.localEulerAngles.x * Mathf.Deg2Rad,
1313- // -jointTransform.localEulerAngles.y * Mathf.Deg2Rad);
1314-
1315- // remember everything is relative to parent.
1316- // TODO: THOUGH MAYBE IT IS RELATIVE TO PARENT JOINT WHICH MAY NOT BE THE SAME!!!
1317-
1318- // Vector3 xyz = jointMatrix.Position().Unity2Ros();
1319- // Vector3 rpy = jointMatrix.rotation.Unity2RosRollPitchYaw();
1320-
1321- // XElement origin = new XElement("origin");
1322- // origin.SetAttributeValue("xyz", xyz.ToXMLString());
1323- // origin.SetAttributeValue("rpy", rpy.ToXMLString());
1324- // jointX.Add(origin);
1325-
13261201 robot . Add ( jointX ) ;
13271202
13281203 XElement parentX = new XElement ( "parent" ) ;
@@ -1333,10 +1208,6 @@ protected void BuildURDFJoints(XElement robot, ZOSimOccurrence parent, Matrix4x4
13331208 childX . SetAttributeValue ( "link" , this . Name ) ;
13341209 jointX . Add ( childX ) ;
13351210
1336-
1337- // build the links
1338- // parent.BuildURDFLink(documentRoot, robot, parent);
1339- // this.BuildURDFLink(documentRoot, robot, parent);
13401211 }
13411212 // recursively go through the children
13421213 foreach ( Transform child in transform ) {
@@ -1348,6 +1219,8 @@ protected void BuildURDFJoints(XElement robot, ZOSimOccurrence parent, Matrix4x4
13481219
13491220 }
13501221
1222+ #endif // 0
1223+
13511224 /// <summary>
13521225 /// Sets the state of the object from XML. XML could come from file or network or wherever.
13531226 /// </summary>
0 commit comments