On Thu, 24 Jun 2021, Jeong Dal wrote: > Dear Hans, > > Your code works fine. > > > \starttext > > > > \startMPcode > > numeric maxCount; > > maxCount := 10; > > setglobalmacro("MyMacro",decial maxCount); > > setglobalcount("MyCount",maxCount); > > \stopMPcode > > > > > > \dorecurse{\MyMacro}{It is \recurselevel. }\par > > \dorecurse{\MyCount}{It is \recurselevel. }\par > > > > \stoptext > > I applied your method to my code as following. > > \newcount\udCount %%% add count > \startMPinitializations%{UDBasic} > pair A,B,C,D; > pair E, F, G, H; > path p,q; > numeric ang,maxNum; > > numeric n; %AM Added > > u:=1in; > A=(.95*TextWidth,u); B=(0,u); C=(0,0); D=(.95*TextWidth,0); > p := A--B--C--D; > q := (TextWidth,1.5u)--(0,1.5u)--(0,-.5u)--(TextWidth,-.5u)--cycle; > z0=(.1u,0); > > vardef nextPt (expr M,N) = > if ypart(N) = 0: > H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[A,B]; > else: > H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[C,D]; > fi; > H > enddef; > > def grayUD(expr i) = > draw G -- if ypart(B)=ypart(G): B -- C else: C -- B fi --z[i]; > filldraw z[i]+2*(G-z[i])*u -- z[i] -- z[i+1] -- z[i+1]+(G-z[i])*u -- cycle withcolor .75white; > draw G -- z[i+1] dashed evenly; > draw z[0] for k=0 upto i: --z[k] endfor dashed evenly; > clip currentpicture to q; > enddef; > > def linesUD(expr i) = > draw p; > draw z0 for k=0 upto i+1: --z[k] endfor dashed evenly; > > % AM: removed. Set in in the main code > % setbounds currentpicture to boundingbox q; > enddef; > > ang := 40; > z[1] = whatever[z[0], z[0]+dir(ang)*u] = whatever[A,B]; > > % AM: Changed next line from G to z[-1] > z[-1] = whatever[z[0], z[0]+dir(2*ang)*u] = whatever[A,B]; > for k = 2 upto 20: > z[k] = nextPt(z[k-2],z[k-1]); > maxNum := k; > exitif xpart(z[k]) > TextWidth; > endfor; > maxNum := maxNum-1; > setglobalmacro("udMacro",decimal maxNum); %%% define udMacro and udCount > setglobalcount("udCount",maxNum); > > \stopMPinitializations > > \startuseMPgraphic{foldUD}{num} > numeric n; > n := \MPvar{num}-1; > G := z[n-1]; > grayUD(n); > \stopuseMPgraphic > \startuseMPgraphic{dotLines}{num} > numeric n; > n := \MPvar{num}-1; > linesUD(n); > \stopuseMPgraphic > > \setupinteraction[state=start] > \usemodule[animation] > \starttext > %\startanimation[menu=yes] > %{ > \dorecurse{\udCount} > {\useMPgraphic{foldUD}{num=\recurselevel}} > {\useMPgraphic{dotLines}{num=\recurselevel}} > %} > %\stopanimation > \stoptext > > 1. Using “udCount” doesn’t issue error message, but it draws the path p only. > 2. Using “udMacro” issue the error message. > > Is there anything that I miss? You need to call the setglobalmacro(...) macro. The code inside \startMPinitializations is only executed when you call any MP environment. But you are using \udCount before \useMPgraphic. A simple solution is to run dummy MP code so that MP initializations are run: \starttext \setbox\scratchbox\hbox{\startMPcode draw unitcircle; \stopMPcode} \dorecurse{\udCount} {\useMPgraphic{foldUD}{num=\recurselevel}} {\useMPgraphic{dotLines}{num=\recurselevel}} \stoptext Aditya