This is due to the way Python works - you need to specify how deep a list is (i.e it’s Rank) manually - or through some very fancy methods that I didn’t make in this set
However, you can leverage the awesome framework we have in Custom Nodes to get around this and properly manage your data.
In the example below you see three groups:
- The blue group as my previous example. Works perfectly fine for a single list!
- The pink group - same code as before. Will shuffle the entire list order, but noto the contents at the deepest level! Useful in some situations - but not this one
- The cyan group which contains the same code as before, but simply wrapped into a Custom Node.
To do this, take a copy of the Python Node, select it and navigate to: Edit -> Create Custom Node, or use the shortcut of Ctrl + D.
This will pop up the Custom Node Properties window. It contains three fields to fill in with data:
- Your node name (This will appear on the node itself)
- Your nodes description (This will appear if you hover on the node itself)
- Your Add-ons Category (This will be where it appears in the Library and each period will create a new sub-menu item. In our case you would look for: Helpers -> List -> Find ‘myShuffle’)
When you type this data in, due to how the syntax of the Python Node works you will get an error message - as below. You can safely ignore this as we will fix the problem in a second! It also makes your custom node exist (As seen in the image below also) that is currently in it’s yellow warning state.
This will take you into a new mode, the Custom Node Editor window, which is yellow in the background. It will also have auto-generated two new nodes, an Input node and an output node; these correlate to the input and output ports generated on your new node and are also the current source of our warning state! Click OK on this warning and then double-click on your new custom node if it doesn’t automatically open.
You’ll now see your new Custom Node Editor mode in a new tab - as shown below:
You’ll have your input port in a red error state and your output port in a good state - both with default names taken from the ports of the Python node.
To finish creating the Custom Node and fixing the error we proceed as such:
- Rename the input port from:
IN[0]
to list : var[]..[]
- Rename the output port from:
OUT
to shuffledList
(Albeit this is discretionary!)
This will change the error state into a good state for our input port by stipulating it’s new port name of list
and it’s ability to parse data of any arbitrary length, stipulated by the DesignScript syntax of : var[]..[]
where the var
means any data type can come through (i.e strings, numbers, elements etc.) and the rank of those lists can be any type (When you get deeper into DesignScript you can find the other forcing functions of single list, double list etc.).
Then click on File --> Save As and save your new Custom Node (Note that it saves with a file extension of .dyf
). This will save into your AppData/Roaming/Dynamo/Dynamo Core/2.X/definitions
folder - and this is where your custom nodes live
After you have saved, you can click X on your Custom Node tab and go back to your Dynamo Graph.
The final step to do is enable List@Level on the Custom Node, which is done by clicking on the list
input port chevron and selecting Use Levels. It will default to Level2 which is what we want in this case - and voila! You now have your sublists being randomly shuffled.
You’ll also be able to find this node (Note: On your machine only unless you share this file with your colleagues) in your library too!