POV-Ray : Newsgroups : povray.general : test if object has been created : Re: test if object has been created Server Time
30 Jul 2024 10:18:52 EDT (-0400)
  Re: test if object has been created  
From: clipka
Date: 7 Mar 2009 19:55:00
Message: <web.49b316ea6b3f2c258dc53c890@news.povray.org>
"[GDS|Entropy]" <gds### [at] hotmailcom> wrote:
> How might one test if an object has been created by a macro which may or may
> not produce an object, based upon various unpredictable factors? I'm trying
> to avoid the "need more than 1 object in csg" warnings.
>
> If you place the macro call within an if statement, would it return true if
> an object has been created, and false if not?

Be sure to grasp what macros actually do. It's a common pitfall to think of them
as functions. They're *not*. Neither do they produce objects. All they do is
auto-generate SDL code at the location where they are invoked.

So if you write, e.g.:

  #macro myMacro(p1,p2)
    #if (p2 > 0)
      sphere { p1, p2 }
    #else
      // nothing
    #end
  #end

  #declare this = myMacro(VRand(myRand),rand(myRand))

then this is effectively the same as if you had rolled a dice and either
written:

  #declare this = sphere{ <...>, ... }

or:

  #declare this = // nothing

In the former case, the macro invocation generates part of a statement that
assigns a sphere object; in the latter case, the macro invocation generates
part of an invalid statement. You cannot even test for any "return value".

Note that MegaPOV has mechanisms to check for the type of a variable; so you
could have your macro generate "false" if it does not generate an object
definition; this way, you could generate a statement that always parses
properly, then check whether generated variable assignment produced an object
or a number. However, official POV doesn't have this, so if you'd try to check
the variable content with an #if(this) or #if(this != false) you'd get a syntax
error in case it is an object variable.

You *can* write a macro this way:

  #macro myMacro(p1,p2)
    #if (p2 > 0)
      #declare this = sphere { p1, p2 }
    #else
      // nothing
    #end
  #end

In that case, you could check whether the object was created by using
#ifdef(this).


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.