Calling Custom Code Block

I’m trying to call a node that I have working as a node not-called. I’ve seen other posts saying it isn’t possible to call custom nodes but I don’t believe that is correct as I’ve done it with a simple node as shown.
However, whenever I try to apply the logic I’ve learned from the simple called block to what I’m wanting to work - then I get errors or nulls at best.

If anyone has a better way to do what I’m trying to accomplish, I’m all ears.

Call%20Code%20Block

Try declaring your def within an imperative scope.

Also you could probably simplify this expression significantly; use ifs for the jumps in the domains (10’s, 25’s, 50’s and 100’s) then use simple math to round to the nearest multiple. Something like:

def MyDef(x : double)
{
	return = [Imperative]
	{
		if (x <= 100.0)
		{
			divisor = 10.0;
			number = Math.Ceiling(x / divisor) * divisor;
		    return = number < 20.0 ? 20.0 : number;
		}
		else if (x <= 250.0) // No need to check if its greater than 100 otherwise it would pass the first if statement
		{
			divisor = 25.0;
		}
		else // Greater than 300.0
		{
			divisor = 100.0;
		}
		
		return = Math.Ceiling(x / divisor) * divisor;
	}
};
1 Like

Turns out theres a bug in DS which prevents the ternary operator from returning a value if embedded in an if statement.

Try this instead. You can remove the superfluous brackets in one-line if else statements too, which may be better for code readability:

def MyDef(x : double)
{
	return = [Imperative]
	{
		divisor = 0.0;
		if (x <= 100.0)
		{
			divisor = 10.0;
			number = Math.Ceiling(x / divisor) * divisor;

			if (number < 20.0)
				return =  20.0;
			else
				return = number;
		}
		else if (x <= 250.0) // No need to check if its greater than 100 otherwise it would pass the first if statement
			divisor = 25.0;
		else // Greater than 300.0
			divisor = 100.0;

		return = Math.Ceiling(x / divisor) * divisor;
	}
};
1 Like

Fantastic Thomas_Mahon! That worked. Now - I just need to dig into that to understand it line by line and apply it to the rest of my script!

The reason for why your original function failed was because you never defined a final false statement. It ends with an incomplete if: x>300&&x<-400?return = 400: needs a false statement and a semi colon to end it.

Yeah, that was just me playing around and forgetting I had left that off. I corrected that and appear to be able to call it, but as you can see in the image below, it returns nulls - not values like the original.

It is because you introduce a but then use x in the whole function, which you have not defined.

1 Like

I noticed that - changed it back to x in both but still have nulls.

Looking at your function a second time : you have no return statements.

Why would I need it if it works the way it is without being a called function?
Do I just add ‘return=’ as shown - because it didn’t like that!
Invalid Associative Functional Statement warning.
Call%20Code%20Block%20Still%20no%20Work|368x500

See this as follows. Imagine you define multiple variables inside a function, say 50 variables. How should the function know which result you want it to return at the end ? This is why having a return statement is mandatory in 99.9% of the cases.

The problem with your first screenshot is the multiple return statements. Design Script do not work like Python does. The essence of the If statements is totally different. An If statement in Python instructs to execute a part of code (and may or may NOT contain elif and else statements), where as in DesignScript, an If statement execute a line of code to determine a value (another way of seeing it is that the If statement IS a value).

Thus, in your first screenshot, you asked the If statement in your function to return a value, which provoked an error, as If statement in DS do not return values (returning a value is not a value itself) but ARE values.

A correct code would have looked like something like :

return = x<40? 40 : 20

and not :

x < 40 ? return = 40 : return = 20

I hope this is clearer.

4 Likes

Oh I see! Thanks mellouze! That works perfectly now!