Code Block evaluation bug?

Hi, I’m working with Dynamo (2.0 now, though started at 1.6) for a graduation project and so far it’s been going well and I’ve been able to learn and solve any issues I’ve had (though I’m by no means an expert), but now I’ve come across a problem that I don’t think should even be one.

Background:
I’ve created a code block with a relatively straightforward imperative block to work out on which element faces a series of points lie. The points and the surfaces have been sorted by the relevant elements for optimisation reasons so as to not have to test every point against every surface with in this particular setup 9600 permutations but limiting the whole thing to a mere 1200 and generally making my life easier further down the road. The actual numbers may become much greater further down the line so I’m having to optimise as much as I can when I can.

The issue I’m having is that when I open the graph, the code block doesn’t evaluate properly; it returns an empty list. This remains the case no matter what I change outside this particular block.

Weirdly, I can work around this by changing seemingly anything in the code - meaning changing some character, adding some brackets, whatever - then clicking outside the box, and it’ll evaluate just fine (running on auto), returning a list of the surfaces I need. Changing it back doesn’t affect the results either; from this point on it works fine until I reopen the file.

However this is clearly not desired behaviour and I’m wondering if it’s something I’m doing wrong but not wrong enough for it to yell at me for it, or if it’s inherent to Dynamo’s internals? It’s not just annoying but would presumably make it impossible to run from the Dynamo Player, though I haven’t been able to test this as I’ve had some issues installing the latest version of Revit on my machine.

There is a fairly large graph riddled with other code blocks preceding this and they all function as normal. I do have all other imperative blocks in defined functions, but this block wasn’t giving the same results that way and I couldn’t be bothered to finagle around with it to get it right.

Can anyone shed any light on this for me?

try defining the variables inside the imperative block only.
I also think there is an issue with imperative nodes and static method calls -
https://github.com/DynamoDS/Dynamo/issues/8796 (but this might not effect you because you’re not using full namespaces)

Thanks for your response.
Unfortunately, defining the variables (inputs) inside the imperative block doesn’t work: The inputs disappear and the code block turns red, though without error message, interestingly.
I’ve also tried assigning the inputs to separate variables, both inside and outside the imperative block, but to no avail.

So I tried using a function define again (method, I guess? My background is mainly in C), and while I got the lacing right this time, the results are essentially the same once I open Dynamo anew:

Would you reckon this is related to that issue with the method calls? I use the same type of construction elsewhere, just with fewer loops, and it seems to work just fine…

Edit:
Defining the function (method?) inside the same block resolves the warning but not the problem.

You are correct insofar that I can get it to evaluate and output normally, but only if I make a change to the relevant code block, be it inside or outside the imperative scope. Changing anything else and either automatically or manually running the script/graph again does not yield results…

Aside from this being annoying I wouldn’t be too bothered were it not for that I’m aiming to deliver a product that can be run proverbially with the push of a button, without having to open up the graph and making local changes just to get it to run.

It’s like having a car that you need to smack with a wrench to get it to run. :confused:

@Michael_Kirschner2 @Lalicorne
I have found the cause and working solution:
The problem is with List.Count(). It does not work correctly within an Imperative block so all the conditions in while- or for-statements need to be determined before entering the imperative block. Since the sizes of the lists and sublists in my case need to be dynamic, I can’t just count them inside the codeblock using List.Count() beforehand since I don’t know in advance how many lists I need to count. Neither can run it in a loop since that necessitates an imperative block, let alone a loop within a loop as it involves multidimensional lists.

The only way that I have found - without resorting to learning Python - is using the List.Map node (any reason this can’t be called from a CBN?) together with List.Count to produce an array of counted sublists, and then adding those as extra inputs to my method.

That being said, I’m not convinced this is functioning as intended, especially since there is no way for the user to know that using List.Count() within an imperative will not behave as expected.
I opened an issue about it last week over here: https://github.com/DynamoDS/Dynamo/issues/8977

For now though, I’ve got what I need.