Null to Empty to Variable

Hello,

I have to ask some frequent tasks that I would like to achieve with OOTB nodes or DesignScript, although I ended up using Python nodes but they do not get to work input as a lists&sublists and not wanting to insert them in a custom node to run with Long lacing mode.

I have seen some people using Imperative Design script or something called Replication guides, not far from the Python codes,

Here what I got so far working with, looking to improve, any suggestion would be appreciated, thanks.

Null to Empty:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

inpo = IN[0]
result = []

if isinstance(inpo, (list,)):
	for each in inpo:
		if each == None:
			result.append([])
		else:
			result.append(each)
elif inpo == None:
	result = []
else:
	result = inpo
OUT = result

Empty to Variable:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

inpo = IN[0]
string = IN[1]
result = []

if isinstance(inpo, (list,)):
	for each in inpo:
		if each == []:
			result.append(string)
		else:
			result.append(each)
elif inpo == []:
	result = None
else:
	result = inpo
OUT = result

I use this node in all my python scripts to ensure lists are received, even if only one element is provided:

def tolist(input):
    result = input if isinstance(input, list) else [input]
    return result

You can apply similar logic to the first item in a list using try/except statements to force lists of a certain depth to be available, but yes as you said otherwise you will need either imperative design script or custom nodes to make your life easier. I ended up going with custom nodes in the end both for myself (Crumple) and for some of my clients with inhouse packages. It’s just so much easier having level and lacing inputs available rather than making complex Python or imperative statements in most cases.

1 Like

I agree but for that simple task like a boolean or replacement I think is unnecessary any levels/lacing as I always expect the same output behaviour

Time to learn imperative design script then I guess. I saw you got some help from Vikram on another thread about similar things - check out his post history and there’ll be plenty of examples.

1 Like

Hi @ruben.romero ,

I’m not sure about what you’re looking for, but is something like this?

def item_tolist (input):
	tolist = input if isinstance(input,list) else [input]
	return tolist

var = item_tolist(IN[0])
result = []

for item in var:
	if item==None:
		result.append([])
	else:
		result.append(item)
	
OUT = result
def item_tolist (input):
	tolist = input if isinstance(input,list) else [input]
	return tolist

var = item_tolist(IN[0])
string = item_tolist(IN[1])
result = []

for item in var:
	if item==[]:
		result.append(string)
	else:
		result.append(item)

OUT = result

I get same identical list before the python script, it does not work for some reason

It didn’t work because you have a list with sub-lists, which means that you have to add another loop to iterate the sub-lists as well. So this would be something like:

def item_tolist (input):
	tolist = input if isinstance(input,list) else [input]
	return tolist

var = item_tolist(IN[0])
string = IN[1]

result = []
first_lp_list = []

for lst in var:
	if lst == []:
		first_lp_list.append(string)
		result.append(first_lp_list)
	else:
		sec_lp_list = []
		for item in lst:
			if item==[]:
				sec_lp_list.append(string)
			else:
				sec_lp_list.append(item)
		result.append(sec_lp_list)
	
OUT = result

You can also check @AmolShah solution for a similar case.

2 Likes

Another solution with a python recursive function

5 Likes

I tried to modify the script of @c.poupin to get the results needed, I like that solution easy to edit and runs pretty well. As a resume:

Null to Empty List I get it resolved finally like this:

import sys
def recurseReplace(lst, replace):
	global funcEval
	newlst = lst[:]
	for idx, elem in enumerate(lst):
		if isinstance(elem, list) and len(elem) > 0:
			newlst[idx] = recurseReplace(lst[idx], replace)
		else:
			if funcEval(lst[idx]):
				newlst[idx] = replace
			else:
				continue
	return newlst

nestedlist = IN[0]
funcEval = lambda x : x is None
NulltoEmpty = recurseReplace(nestedlist, replace = [])

OUT = NulltoEmpty

Also combined double condition of Empty List to String and Nulls to String in a single script with 1 input list and 2 inputs strings, which is something I was looking at the end as well.

import sys
def recurseReplace(lst, replace):
	global funcEval
	newlst = lst[:]
	for idx, elem in enumerate(lst):
		if isinstance(elem, list) and len(elem) > 0:
			newlst[idx] = recurseReplace(lst[idx], replace)
		else:
			if funcEval(lst[idx]):
				newlst[idx] = replace
			else:
				continue
	return newlst

nestedlist = IN[0]
funcEval = lambda x : x is x == []
EmptytoString = recurseReplace(nestedlist, replace = IN[1])
funcEval = lambda x : x is None
Nulltostring = recurseReplace(EmptytoString, replace = IN[2])

OUT = Nulltostring

image

Many thanks for all your support @EdsonMatt @GavinCrump @Organon @Vikram_Subbaiah , I tested all the solutions provided and learnt something new.

I think it’s time to find your own solution. You already have a lot of good solutions shared by different users. You need to get your hands dirty, instead of adding more requirements for your work, and expect someone else to do it for you.

3 Likes