Running Python script from idle in Dynamo

Another way if you want to copy the rectpack folder into a given directory:

import clr
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
import os
os.system('python C:\rectpack-master\rectpack.py')
from rectpack import newPacker

rectangles = [(100, 30), (40, 60), (30, 30),(70, 70), (100, 50), (30, 30)]
bins = [(300, 450), (80, 40), (200, 150)]

packer = newPacker()
#Add the rectangles to packing queue

for r in rectangles:
	packer.add_rect(*r)
#Add the bins where the rectangles will be placed

for b in bins:
	packer.add_bin(*b)
#Start packing

packer.pack()

nbins = len(packer)

OUT= nbins

Edit: It does not work in this case.

4 Likes

could you explain why to write the r before the path? just out of curiosity

r is used to indicate a raw string.

Since the backslash is used to escape characters that otherwise have a special function, putting r before the string indicates to read the backslash as backslash and not as escape command.

Some more info here:
https://docs.python.org/2.0/ref/strings.html

2 Likes

Things seem to work now. Thank you @lucamanzoni

Thanks @Alban_de_Chasteigner

what does this line do?

os.system(‘python C:\rectpack-master\rectpack.py’)

and how did you get rectpack-master folder with rectpack.py init?

I couldn’t find rectpack.py anywhere

You’re right, there is no rectpack.py file so it can’t load the file.
I missed something in my test.

The best way is to copy the folder in C:\Program Files (x86)\IronPython 2.7\Lib to load the module.

@Alban_de_Chasteigner
Does os.system(‘python C:\rectpack-master\rectpack.py’) mean you can python shell command?

can you please explain it I am curious to know more about the line that you used?

Here is a successful example:

1 Like

Very interesting idea to run the script. Thanks @Alban_de_Chasteigner

Is this being used to stack views on a sheet? Would be amazing to have rectangles (viewport length and width) in as a list with the script. Then an in for the bin (sheet). But then would need an output of the center point of the stacked rectangles. Could then use that information to put views on the sheet. Another obstacle is to output views that didn’t fit to go on another sheet. I don’t have the skills at python to achieve this but thought I’d put it out there if anyone’s keen to try :slight_smile:

@vanman
Yeah, it’s possible :smiley:

1 Like

This may also be relevant: Box

That said we’re getting off topic, if we’d like to continue I can split out relevant posts (or someone start a new topic and link back as needed).

1 Like

The holy grail! Would you be kind enough to share?

I’ve been waiting for Thomas Corrie to finish his package but its been some time

Any idea how to format things correctly to change it to taking a list? :slight_smile: spent a few hours. Cannot figure out the arguments. Went from 27 down to 2 but need 3 or max 4. Been reading to try understand. Seems like I need “self” infront but I just don’t quite get it haha

I always get 1 if I do this but no errors which so I’m not really sure if I’ve done the library correct

Side question. Could you copy code from rectpac into dynamo python script instead of installing the library?

@vanman
Sorry for the late reply, but have you looked at this post: Placing views on sheets by coordinates

it might be what you seek :slight_smile:

Thanks Saeedy. I’ve used that but I’m really after something that works out where to place the views. Need the bin packing algorithm sorted to get the coordinates of where to place views to use that script from the link.