diff --git a/src/flamegpu/simulation/CUDASimulation.cu b/src/flamegpu/simulation/CUDASimulation.cu index 480640ac2..428314df6 100644 --- a/src/flamegpu/simulation/CUDASimulation.cu +++ b/src/flamegpu/simulation/CUDASimulation.cu @@ -1542,12 +1542,6 @@ void CUDASimulation::applyConfig_derived() { // We init Random through submodel hierarchy after singletons reseed(getSimulationConfig().random_seed); - -#ifdef FLAMEGPU_VISUALISATION - if (visualisation) { - visualisation->hookVis(visualisation, directed_graph_map); - } -#endif } void CUDASimulation::reseed(const uint64_t seed) { @@ -1662,6 +1656,12 @@ void CUDASimulation::initialiseSingletons() { // Ensure RTC is set up. initialiseRTC(); + +#ifdef FLAMEGPU_VISUALISATION + if (visualisation) { + visualisation->hookVis(visualisation, directed_graph_map); + } +#endif } void CUDASimulation::initialiseRTC() { @@ -1717,6 +1717,10 @@ const CUDASimulation::Config &CUDASimulation::getCUDAConfig() const { visualiser::ModelVis CUDASimulation::getVisualisation() { if (!visualisation) { visualisation = std::make_shared(*this); + // If visualisation is init after sim has been init ensure graphs are linked to vis + if (directed_graph_map.size()) { + visualisation->hookVis(visualisation, directed_graph_map); + } } return visualiser::ModelVis(visualisation, isSWIG); } diff --git a/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu b/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu index f74ef1950..2e2391d34 100644 --- a/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu +++ b/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu @@ -583,10 +583,12 @@ void CUDAEnvironmentDirectedGraphBuffers::syncDevice_async(detail::CUDAScatter& if (has_changed) { #ifdef FLAMEGPU_VISUALISATION if (auto vis = visualisation.lock()) { - vis->visualiser->lockDynamicLinesMutex(); - vis->rebuildEnvGraph(graph_description.name); - vis->visualiser->updateDynamicLine(std::string("graph_") + graph_description.name); - vis->visualiser->releaseDynamicLinesMutex(); + if (vis->graphs.find(graph_description.name) != vis->graphs.end()) { + vis->visualiser->lockDynamicLinesMutex(); + vis->rebuildEnvGraph(graph_description.name); + vis->visualiser->updateDynamicLine(std::string("graph_") + graph_description.name); + vis->visualiser->releaseDynamicLinesMutex(); + } } #endif } diff --git a/src/flamegpu/visualiser/ModelVis.cpp b/src/flamegpu/visualiser/ModelVis.cpp index 750e7c706..7af8d1aab 100644 --- a/src/flamegpu/visualiser/ModelVis.cpp +++ b/src/flamegpu/visualiser/ModelVis.cpp @@ -19,9 +19,11 @@ ModelVisData::ModelVisData(const flamegpu::CUDASimulation &_model) , modelData(_model.getModelDescription()) { } void ModelVisData::hookVis(std::shared_ptr& vis, std::unordered_map> &map) { + for (auto [key, buf] : map) { + buf->setVisualisation(vis); + } for (auto [name, graph] : graphs) { auto &graph_buffs = map.at(name); - graph_buffs->setVisualisation(vis); graph->constructGraph(graph_buffs); vis->rebuildEnvGraph(name); }