Skip to content

Commit 2247225

Browse files
ahcordeadlarkin
andauthored
USD to SDF: Added Lights attached to models (#903)
Signed-off-by: ahcorde <ahcorde@gmail.com> Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
1 parent ee762e8 commit 2247225

File tree

4 files changed

+79
-25
lines changed

4 files changed

+79
-25
lines changed

test/usd/upAxisZ.usda

+12
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ def Xform "box" (
121121
uniform token[] xformOpOrder = ["xformOp:scale"]
122122
}
123123
}
124+
125+
def DiskLight "boxModelLight" (
126+
prepend apiSchemas = ["ShapingAPI"]
127+
kind = "model"
128+
)
129+
{
130+
float inputs:intensity = 3000
131+
float inputs:specular = 0.5
132+
float3 xformOp:rotateZYX = (0, 0, 45)
133+
float3 xformOp:translate = (0, 0, 1000)
134+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateZYX"]
135+
}
124136
}
125137
}
126138

usd/src/usd_parser/USDStage_TEST.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ TEST(USDStage, Constructor)
3939

4040
EXPECT_EQ("Z", stage.UpAxis());
4141
EXPECT_DOUBLE_EQ(0.01, stage.MetersPerUnit());
42-
EXPECT_EQ(25u, stage.USDPaths().size());
42+
EXPECT_EQ(26u, stage.USDPaths().size());
4343
}
4444

4545
// Check up Axis equal to Y and metersPerUnit

usd/src/usd_parser/USDWorld.cc

+35-14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <utility>
2424
#include <vector>
2525

26+
#include <ignition/common/Filesystem.hh>
2627
#include <ignition/common/Util.hh>
2728

2829
#pragma push_macro ("__DEPRECATED")
@@ -174,20 +175,6 @@ namespace usd
174175
}
175176
}
176177

177-
if (prim.IsA<pxr::UsdLuxBoundableLightBase>() ||
178-
prim.IsA<pxr::UsdLuxNonboundableLightBase>())
179-
{
180-
auto light = ParseUSDLights(prim, usdData, linkName);
181-
light->SetName(primName);
182-
if (light)
183-
{
184-
_world.AddLight(light.value());
185-
// TODO(ahcorde) Include lights which are inside links
186-
}
187-
continue;
188-
}
189-
// TODO(anyone) support converting other USD light types
190-
191178
sdf::Model *modelPtr = nullptr;
192179
if (!currentModelName.empty())
193180
{
@@ -202,6 +189,40 @@ namespace usd
202189
}
203190
}
204191

192+
if (prim.IsA<pxr::UsdLuxBoundableLightBase>() ||
193+
prim.IsA<pxr::UsdLuxNonboundableLightBase>())
194+
{
195+
auto light = ParseUSDLights(prim, usdData, linkName);
196+
if (light)
197+
{
198+
light->SetName(primName);
199+
200+
// assume this light belongs to the world unless the corresponding
201+
// model/link for this light are found
202+
bool worldLight = true;
203+
204+
// if the light prim we are parsing has no parent (or if its parent
205+
// is the root prim), this means the light belongs to the world
206+
const bool noModelAncestor = !prim.GetParent() ||
207+
(prim.GetParent().GetName().GetString() == "/");
208+
if (!noModelAncestor && modelPtr)
209+
{
210+
if (auto link =
211+
modelPtr->LinkByName(ignition::common::basename(linkName)))
212+
{
213+
link->AddLight(light.value());
214+
worldLight = false;
215+
}
216+
}
217+
218+
if (worldLight)
219+
{
220+
_world.AddLight(light.value());
221+
}
222+
}
223+
continue;
224+
}
225+
205226
if (prim.IsA<pxr::UsdPhysicsJoint>())
206227
{
207228
if (!modelPtr)

usd/src/usd_parser/usd2sdf_TEST.cc

+31-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include <ignition/utils/ExtraTestMacros.hh>
2727

28+
#include "sdf/Light.hh"
29+
#include "sdf/Link.hh"
2830
#include "sdf/Model.hh"
2931
#include "sdf/Root.hh"
3032
#include "sdf/World.hh"
@@ -121,6 +123,16 @@ TEST(check_cmd, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
121123
EXPECT_EQ(
122124
"ignition-gazebo-scene-broadcaster-system", plugins[3].Filename());
123125

126+
// the world should have lights attached to it
127+
std::set<std::string> savedLightNames;
128+
for (unsigned int i = 0; i < world->LightCount(); ++i)
129+
savedLightNames.insert(world->LightByIndex(i)->Name());
130+
EXPECT_EQ(3u, savedLightNames.size());
131+
132+
EXPECT_NE(savedLightNames.end(), savedLightNames.find("defaultLight"));
133+
EXPECT_NE(savedLightNames.end(), savedLightNames.find("diskLight"));
134+
EXPECT_NE(savedLightNames.end(), savedLightNames.find("sun"));
135+
124136
// make sure all models in the USD file were correctly parsed to SDF
125137
std::set<std::string> savedModelNames;
126138
for (unsigned int i = 0; i < world->ModelCount(); ++i)
@@ -140,20 +152,29 @@ TEST(check_cmd, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
140152
ASSERT_NE(nullptr, world->ModelByName("box"));
141153
EXPECT_FALSE(world->ModelByName("box")->Static());
142154

143-
// check that models have the right links
144-
std::function<void(const std::string &, const std::string &)> checkLink =
145-
[&world](const std::string &_modelName, const std::string &_linkName)
155+
// Check that models have the right links.
156+
// If a link should have a light attached to it, check that as well
157+
std::function<void(const std::string &, const std::string &,
158+
const std::string &)> checkLink =
159+
[&world](const std::string &_modelName, const std::string &_linkName,
160+
const std::string &_lightLinkName)
146161
{
147162
const auto modelPtr = world->ModelByName(_modelName);
148163
ASSERT_NE(nullptr, modelPtr);
149164
EXPECT_EQ(1u, modelPtr->LinkCount());
150-
EXPECT_NE(nullptr, modelPtr->LinkByName(_linkName));
165+
const auto modelLink = modelPtr->LinkByName(_linkName);
166+
ASSERT_NE(nullptr, modelLink);
167+
168+
if (!_lightLinkName.empty())
169+
EXPECT_NE(nullptr, modelLink->LightByName(_lightLinkName));
170+
else
171+
EXPECT_EQ(0u, modelLink->LightCount());
151172
};
152-
checkLink("ground_plane", "link");
153-
checkLink("box", "box_link");
154-
checkLink("cylinder", "cylinder_link");
155-
checkLink("sphere", "sphere_link");
156-
checkLink("capsule", "capsule_link");
157-
checkLink("ellipsoid", "ellipsoid_link");
173+
checkLink("ground_plane", "link", "");
174+
checkLink("box", "box_link", "boxModelLight");
175+
checkLink("cylinder", "cylinder_link", "");
176+
checkLink("sphere", "sphere_link", "");
177+
checkLink("capsule", "capsule_link", "");
178+
checkLink("ellipsoid", "ellipsoid_link", "");
158179
}
159180
}

0 commit comments

Comments
 (0)