diff --git a/Core/Export/SampleLabelHandler.cpp b/Core/Export/SampleLabelHandler.cpp
index d27220a036bf08fa9b3c9aebd7308a84354aeb3c..6ae6b0a0aa2dc1fd688b606b653168afbdbc3233 100644
--- a/Core/Export/SampleLabelHandler.cpp
+++ b/Core/Export/SampleLabelHandler.cpp
@@ -15,6 +15,7 @@
 #include "Core/Export/SampleLabelHandler.h"
 #include "Sample/Aggregate/IInterferenceFunction.h"
 #include "Sample/Multilayer/MultiLayer.h"
+#include "Sample/Multilayer/Layer.h"
 #include "Sample/Particle/MesoCrystal.h"
 #include "Sample/Particle/Particle.h"
 #include "Sample/Particle/ParticleComposition.h"
@@ -36,7 +37,14 @@ std::string SampleLabelHandler::labelInterferenceFunction(const IInterferenceFun
 }
 
 std::string SampleLabelHandler::labelLayer(const Layer* layer) {
-    return m_LayerLabel[layer];
+    std::vector<const Layer*> v = objectsOfType<Layer>();
+    const auto vpos = std::find(v.begin(), v.end(), layer);
+    if (vpos == std::end(v))
+        throw std::runtime_error("BUG: object not found in SampleLabelHandler::labelLayer");
+    std::string ret = "layer";
+    if (v.size()>1)
+        ret += "_" + std::to_string(vpos-v.begin()+1);
+    return ret;
 }
 
 std::string SampleLabelHandler::labelLayout(const ParticleLayout* layout) {
@@ -109,6 +117,8 @@ void SampleLabelHandler::insertLattice3D(const Lattice3D* sample) {
 void SampleLabelHandler::insertLayer(const Layer* sample) {
     std::string label = "layer_" + std::to_string(m_LayerLabel.size() + 1);
     m_LayerLabel.insert(sample, label);
+
+    m_objects.push_back((const ISample*)(sample));
 }
 
 void SampleLabelHandler::insertLayout(const ParticleLayout* sample) {
diff --git a/Core/Export/SampleLabelHandler.h b/Core/Export/SampleLabelHandler.h
index 9d9a0f5cec54db5b116f073f2d2b0ed17743da01..93a6cc2728c4fb20f4e349eb731d79d9be60ba4c 100644
--- a/Core/Export/SampleLabelHandler.h
+++ b/Core/Export/SampleLabelHandler.h
@@ -16,6 +16,7 @@
 #define BORNAGAIN_CORE_EXPORT_SAMPLELABELHANDLER_H
 
 #include "Core/Export/OrderedMap.h"
+#include <vector>
 
 class Crystal;
 class IAbstractParticle;
@@ -23,6 +24,7 @@ class IFormFactor;
 class IInterferenceFunction;
 class ParticleLayout;
 class Material;
+class ISample;
 class IRotation;
 class Lattice2D;
 class Lattice3D;
@@ -107,6 +109,9 @@ public:
     void insertRotation(const IRotation* sample);
     void insertRoughness(const LayerRoughness* sample);
 
+    template<class T>
+    std::vector<const T*> objectsOfType() const;
+
 private:
     crystals_t m_CrystalLabel;
     formfactors_t m_FormFactorLabel;
@@ -124,6 +129,17 @@ private:
     particlescoreshell_t m_ParticleCoreShellLabel;
     rotations_t m_RotationsLabel;
     roughnesses_t m_LayerRoughnessLabel;
+
+    std::vector<const ISample*> m_objects;
 };
 
+template<class T>
+std::vector<const T*> SampleLabelHandler::objectsOfType() const {
+    std::vector<const T*> ret;
+    for (const ISample* s: m_objects)
+        if (const auto* c = dynamic_cast<const T*>(s); c)
+            ret.push_back(c);
+    return ret;
+}
+
 #endif // BORNAGAIN_CORE_EXPORT_SAMPLELABELHANDLER_H
diff --git a/Core/Export/SampleToPython.cpp b/Core/Export/SampleToPython.cpp
index a54464c72ba381a490af5fb5da9ff8b63e6a204b..1fb9ba02e68ed4f1123f845162dcd9f9fe6bbc75 100644
--- a/Core/Export/SampleToPython.cpp
+++ b/Core/Export/SampleToPython.cpp
@@ -188,29 +188,24 @@ std::string SampleToPython::defineMaterials() const {
 }
 
 std::string SampleToPython::defineLayers() const {
-    const auto* themap = m_label->layerMap();
-    if (themap->empty())
-        return "# No Layers.\n\n";
+    std::vector<const Layer*> v = m_label->objectsOfType<Layer>();
+    if (v.empty())
+        return "";
     std::ostringstream result;
-    result << std::setprecision(12);
     result << "\n" << indent() << "# Define layers\n";
-    for (auto it = themap->begin(); it != themap->end(); ++it) {
-        const Layer* layer = it->first;
-        result << indent() << it->second << " = ba.Layer("
+    result << std::setprecision(12);
+    for (const Layer* layer: v) {
+        const std::string& label = m_label->labelLayer(layer);
+        result << indent() << label << " = ba.Layer("
                << m_label->labelMaterial(layer->material());
         if (layer->thickness() != 0)
             result << ", " << pyfmt::printNm(layer->thickness());
         result << ")\n";
         if (layer->numberOfSlices() != 1)
-            result << indent() << it->second << ".setNumberOfSlices(" << layer->numberOfSlices()
+            result << indent() << label << ".setNumberOfSlices(" << layer->numberOfSlices()
                    << ")\n";
-    }
-    result << std::setprecision(12);
-    const auto layermap = m_label->layerMap();
-    for (auto it = layermap->begin(); it != layermap->end(); ++it) {
-        const Layer* layer = it->first;
         for (const auto* layout : layer->layouts())
-            result << indent() << it->second << ".addLayout(" << m_label->labelLayout(layout)
+            result << indent() << label << ".addLayout(" << m_label->labelLayout(layout)
                    << ")\n";
     }
     return result.str();
@@ -262,8 +257,7 @@ std::string SampleToPython::defineCoreShellParticles() const {
     result << "\n" << indent() << "# Define core shell particles\n";
     for (auto it = themap->begin(); it != themap->end(); ++it) {
         const ParticleCoreShell* coreshell = it->first;
-        result << "\n"
-               << indent() << it->second << " = ba.ParticleCoreShell("
+        result << indent() << it->second << " = ba.ParticleCoreShell("
                << m_label->labelParticle(coreshell->shellParticle()) << ", "
                << m_label->labelParticle(coreshell->coreParticle()) << ")\n";
         std::string core_shell_name = it->second;
diff --git a/auto/Wrap/doxygenCore.i b/auto/Wrap/doxygenCore.i
index bdfa7886db9813a503e6eec7aae4f71cda80a3b3..4401e798dcf41e79ac42e5a6cf248a2934fd6840 100644
--- a/auto/Wrap/doxygenCore.i
+++ b/auto/Wrap/doxygenCore.i
@@ -1889,6 +1889,9 @@ C++ includes: SampleLabelHandler.h
 %feature("docstring")  SampleLabelHandler::insertRoughness "void SampleLabelHandler::insertRoughness(const LayerRoughness *sample)
 ";
 
+%feature("docstring")  SampleLabelHandler::objectsOfType "std::vector< const T * > SampleLabelHandler::objectsOfType() const
+";
+
 
 // File: classSampleToPython.xml
 %feature("docstring") SampleToPython "