Phyton Script - How to use it on Dynamo

#1

I’m new on Dynamo Phyton Scripting.So any help will be appreciated.
I have a python script that creates pairs of all possible combinations from a list. I would like to implement it into Dynamo. Not sure how???

0 Likes

#2

You can use that exact same code inside a python node, and only change how you take in the input values
Have you tried it?

If you haven’t done any dynamo/python at all, take a look at Dynamo Primer’s intro to Python Node:
http://dynamoprimer.com/en/09_Custom-Nodes/9-4_Python.html

0 Likes

#3

Thanks for the response.
I did try it but I did not get the results I wanted. What am I missing?

0 Likes

#4
  1. Please paste your code as text, instead of an image, if you’d like it to be reviewed. (Use the preformatted text option )

  2. You could try Dynamo’s built-in “Combinations” node as an alternative:

0 Likes

#5

ok.
I did try the List.Combinations node but I did not get what I needed.
Thanks for the help.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

stringlist = IN[0]

import itertools

def generate_groups(lst, n):
    if not lst:
        yield []
    else:
        for group in (((lst[0],) + xs) for xs in itertools.combinations(lst[1:], n-1)):
            for groups in generate_groups([x for x in lst if x not in group], n):
                yield [group] + groups

stringlist.append(list(generate_groups([stringlist], 2)))

#Assign your output to the OUT variable.
OUT = stringlist
0 Likes

#6

The problem arises from the fact that you’re wrapping your input list in another list when you’re calling the function:

generate_groups(**[**stringlist], 2)))

1 Like

#7

This should do it:

stringlist = IN[0]
import itertools

def generate_groups(lst, n):
    if not lst:
        yield []
    else:
        for group in (((lst[0],) + xs) for xs in itertools.combinations(lst[1:], n-1)):
            for groups in generate_groups([x for x in lst if x not in group], n):
                yield [group] + groups


OUT = generate_groups(stringlist, 2)
2 Likes

Python Nodes Basics
#8

Many thanks Dimitar_Venkov and salvatoredragotta… It makes more sense now.

0 Likes