I hope that some techniques will allow us in the future to simplify such tasks. As often in this context, the challenge is to order lists, and to thread over lists. In Mathematica, these are simple `Sort` and `Map` commands, and I do hope that in some future we will have similar tools in LaTeX, yet at this point to the best of my knowledge we do not. This is something that works, I think, but takes a while to compile.
```
\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools} % https://github.com/marmotghost/tikz-3dtools
\makeatletter
\newif\if@pgf@list@first@item
\pgfkeys{/handlers/.add item/.code=\pgfkeys{\pgfkeyscurrentpath/.append={,#1}},
/handlers/.remove item/.code={%
\edef\pgfutil@tmpa{\pgfkeysvalueof{\pgfkeyscurrentpath}}%
\edef\pgfutil@tmpb{#1}%
\edef\pgfutil@tmpc{\noexpand\@removeelement{\pgfutil@tmpb}{\pgfutil@tmpa}{\noexpand\pgfutil@tmpa}}%
\pgfutil@tmpc
\pgfkeyssetvalue{\pgfkeyscurrentpath}{\pgfutil@tmpa}},% maybe test if the list is now empty?
}
\tikzset{3d/draw behind/.code 2 args={% #1 object, #2 list of objects in front
%\typeout{Draw #1 behind #2.}
\tikzset{3d/current path=#1,
3d/draw clipped against/.list={#2},
3d/draw avoiding={#2}}
},3d/draw clipped against/.code={% #1=clip path
%\typeout{Clip \pgfkeysvalueof{/tikz/3d/current path} against #1.}
\begin{scope}
\tikzset{/tikz/3d/polyhedron/before hidden/.code={%\typeout{clip!}%
\clip[use named path/.expanded=#1];},
/tikz/3d/polyhedron/before visible/.code={%\typeout{clip!}%
\clip[use named path/.expanded=#1];},
all hidden,
/tikz/polyhedron \pgfkeysvalueof{/tikz/3d/current path}}
\end{scope}},
3d/draw avoiding/.code={% #1=list of paths to be avoided
%\typeout{Draw \pgfkeysvalueof{/tikz/3d/current path} avoiding #1.}
\begin{scope}
\tikzset{protect/.list={#1}}
\tikzset{/tikz/3d/polyhedron/before hidden/.code={%\typeout{avoid!}%
\tikzset{protect/.list={#1}}},
/tikz/3d/polyhedron/before visible/.code={%\typeout{avoid #1!}%
\tikzset{protect/.list={#1}}},
/tikz/polyhedron \pgfkeysvalueof{/tikz/3d/current path}}
\end{scope}},3d/current path/.initial={},
3d/draw ordered objects/.code={%
\tikzset{3d/temporary list 1/.initial={#1},
3d/draw ordered object/.list={#1}}%
},3d/draw ordered object/.code={%
% test if remaining list has only one element
\edef\pgf@util@tempa{\noexpand\pgfutil@in@{,}{\pgfkeysvalueof{/tikz/3d/temporary list 1}}}%
\pgf@util@tempa
\tikzset{3d/temporary list 1/.remove item={#1}}
\ifpgfutil@in@%
\tikzset{3d/draw behind/.expanded={#1}{\pgfkeysvalueof{/tikz/3d/temporary list 1}}}
\else
\begin{scope}
\tikzset{polyhedron #1};
\end{scope}
\fi
}}
\makeatother
\def\pgfmathsetmacroFPU#1#2{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro#1{#2}%
\pgfmathsmuggle#1\endgroup}%
\tikzset{
protect/.code={%\typeout{protecting #1.}
\clip[overlay,reset cm,even odd clip,use named path=#1]
(-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt);
}}
\begin{document}
\foreach \Angle in {5,15,...,85} %{55} % {5,15,...,355} % {5,25,...,355} % {345} % {155} %
{\tdplotsetmaincoords{60}{\Angle}
\begin{tikzpicture}[same bounding box=A,%<- newly added to `3dtools`
scale=1,tdplot_main_coords,line join = round,
line cap = round, declare function={a = 4;b = 4;h=4;}]
\tikzset{3d/polyhedron/.cd,
%face edges/.style={},%
back/.style={3d/polyhedron/complete dashes,fill=none},
%
fore/.style={3d/visible,solid,fill=none,
3d/polyhedron/edges have complete dashes=false},
complete dashes,
/tikz/all hidden/.style={%
3d/polyhedron/fore/.style={3d/polyhedron/complete dashes,fill=none},
}}
\def\zbottom{0}
\def\icube{0}
\tikzset{set up cube/.code={\edef\icube{\the\numexpr\icube+1}%
\pgfmathsetmacroFPU{\acube}{pow(##1,1/3)/2}% note that you may need #1 instead of ##1
\path (-\acube,-\acube,\zbottom) coordinate (v-\icube-1)
(\acube,-\acube,\zbottom) coordinate (v-\icube-2)
(\acube,\acube,\zbottom) coordinate (v-\icube-3)
(-\acube,\acube,\zbottom) coordinate (v-\icube-4);
\pgfmathsetmacroFPU{\zbottom}{\zbottom-2*\acube}%
\path (-\acube,-\acube,\zbottom) coordinate (v-\icube-5)
(\acube,-\acube,\zbottom) coordinate (v-\icube-6)
(\acube,\acube,\zbottom) coordinate (v-\icube-7)
(-\acube,\acube,\zbottom) coordinate (v-\icube-8);
\path[save named path=cube-\icube,convex hull of={v-\icube-1,v-\icube-2,%
v-\icube-3,v-\icube-4,v-\icube-5,v-\icube-6,v-\icube-7,v-\icube-8}];
\expanded{\noexpand\tikzset{polyhedron cube-\icube/.style={%
/tikz/3d/polyhedron/.cd,
O={(0,0,\zbottom+\acube)},
draw face with corners={{(v-\icube-1)},{(v-\icube-2)},{(v-\icube-3)},{(v-\icube-4)}},
draw face with corners={{(v-\icube-1)},{(v-\icube-2)},{(v-\icube-6)},{(v-\icube-5)}},
draw face with corners={{(v-\icube-2)},{(v-\icube-3)},{(v-\icube-7)},{(v-\icube-6)}},
draw face with corners={{(v-\icube-3)},{(v-\icube-4)},{(v-\icube-8)},{(v-\icube-7)}},
draw face with corners={{(v-\icube-4)},{(v-\icube-1)},{(v-\icube-5)},{(v-\icube-8)}},
draw face with corners={{(v-\icube-5)},{(v-\icube-6)},{(v-\icube-7)},{(v-\icube-8)}},
}}}
\ifnum\icube=1
\edef\lstcubes{cube-1}
\else
\edef\lstcubes{cube-\icube,\lstcubes}
\fi
}}
\tikzset{set up cube/.list={1,8,27,64,125,216,343}}
\tikzset{3d/draw ordered objects/.expanded={\lstcubes}}
\end{tikzpicture}}
\end{document}
```
