Jump to content

weird VEX behavior


Juraj

Recommended Posts

Hi,

I have a quick question.

What is difference between this two VEX codes?

 

vex1:

if (@Frame == 25) {
    f@a = @Frame;
}

vex2:

float a;
if (@Frame == 25) {
    a = @Frame;
}
f@a = a;

Until now I thought that they are exactly same. However when checking attribute spreadsheet they behave quite differently.

Now I feel like I am missing something extremly basic.

 

Thanks,

Juraj

Link to comment
Share on other sites

first of all to get rid of obvious difference in returned values

float a;

 results in unitialized variable, and since it's not being set in all codepaths the content of that is unpredictable
so you would need to do:

float a = 0.0;
if (@Frame == 25) {
    a = @Frame;
}
f@a = a;

then the other difference is that

if (@Frame == 25) {
    f@a = @Frame;
}

sets the attribute only on frame 25
the other one sets it always
so if you feed back the geo (DOPs, Solver, Foreach) or the default of a attrib is not 0 they would still behave differently

 

EDIT: so to make that the same you would maybe need to do:

float a = f@a;
if (@Frame == 25) {
    a = @Frame;
}
f@a = a;
Edited by anim
Link to comment
Share on other sites

Hi,

thank you for your reply but I am still confused.

 

This code should initialize variable only on frame 25. How is that a attribute exists on every frame? The condition was not true so code in { } shouldn't evaluate. 

if (@Frame == 25) {
    f@a = @Frame;
}

Also, let's say that value of is not predictable. How does Houdini then know that it should be set to current frame number? (Which shouldn't because condition returns false so the code in { } should not exist for Houdini. )

 

EDIT: Does it work in every programming language like that?

float a;
if (@Frame == 25) {
    a = @Frame;
}
f@a = a;
Edited by Juraj Tomori
Link to comment
Share on other sites

...

This code should initialize variable only on frame 25. How is that a attribute exists on every frame? The condition was not true so code in { } shouldn't evaluate.

...

thats not how attribute binding works, actually @ is not even VEX syntax, but just a token that snippets use to simplify data binding

it get's expanded to VEX code before compiling, so this:

if (@Frame == 25) {
    f@a = @Frame;
}

actually produces this VEX Code:

void
_obj_torus_object1_attribwrangle1_attribvop1_snippet1(float _bound_Frame; float _bound_a)
{
    if (_bound_Frame == 25) {
        _bound_a = _bound_Frame;
    }
}
cvex
attribvop1(export float Frame = 0;
        export float a = 0)
{
    _obj_torus_object1_attribwrangle1_attribvop1_snippet1(Frame, a);
}

which in other words means that bound attribute will be created no matter what if referenced in snipped (and it's included in exportlist parm on Wrangle or Snippet)

so the If condition has no effect on actual attribute creation, just on the values

to create attribute only when the condition is true you will have to do:

if (@Frame == 25) {
    addattrib(0, "point", "a", 0.0);
    setpointattrib(0, "a", @ptnum, @Frame);
}
Link to comment
Share on other sites

Also, let's say that value of a is not predictable. How does Houdini then know that it should be set to current frame number? (Which shouldn't because condition returns false so the code in { } should not exist for Houdini. )

...

it doesn't know

while I can't explain why it populates value with Frame even if false, it may have some explanation in how it's optimized, especially since @Frame is global beforehand known uniform variable so maybe it just dumps it in instead of leaving some garbage, but the point is you should not have uninitialized variables unless you really know that all code paths will populate it with values before you try to read from it

so if you try assigning some varying variable like @ptnum, you will see that uninitialized a will really contain garbage values unless condition is met

Edited by anim
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...