Juraj Posted October 7, 2015 Share Posted October 7, 2015 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 Quote Link to comment Share on other sites More sharing options...
anim Posted October 7, 2015 Share Posted October 7, 2015 (edited) 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 unpredictableso 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 25the other one sets it alwaysso 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 October 7, 2015 by anim Quote Link to comment Share on other sites More sharing options...
Juraj Posted October 7, 2015 Author Share Posted October 7, 2015 (edited) 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 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. ) EDIT: Does it work in every programming language like that? float a; if (@Frame == 25) { a = @Frame; } f@a = a; Edited October 7, 2015 by Juraj Tomori Quote Link to comment Share on other sites More sharing options...
anim Posted October 8, 2015 Share Posted October 8, 2015 ... 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); } Quote Link to comment Share on other sites More sharing options...
anim Posted October 8, 2015 Share Posted October 8, 2015 (edited) 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 October 8, 2015 by anim Quote Link to comment Share on other sites More sharing options...
Juraj Posted October 8, 2015 Author Share Posted October 8, 2015 Makes sense now Thank you very much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.