I’m trying to make a script for moving and exporting a group of objects but I get stuck. I did a few tutorials on rhino python but am not completely common with it.
What I want to do for every group is move (copy) it with the lower left corner to the zero point, export it to a folder I can choose and delete afterwards so that I still have the original group in it’s original place but the dxf is exported at the zero point. The layers with it’s colors should be maintained in the dxf.
When I try moving I already get stuck because it moves every object separately and not as a group.
This is what I have so far:
import rhinoscriptsyntax as rs
import dxfwrite
objcts = rs.GetObjects(“Select objects”)
zeropoint = [0,0,0]
for x in objcts:
bb = rs.BoundingBox(x)
bb1 = bb[0]
mvvec = rs.VectorCreate(zeropoint, bb1)
rs.MoveObject(x, mvvec)
Okay I currently got it to move each group of objects to zero position and be deleted. I’m sure this is a very cumbersome way to do it but it works. Now I need to do the exporting step.
for x in objcts:
group = rs.ObjectGroups(x)
if group not in grouplist:
grouplist.append(group)
count = len(grouplist)
groupedlist = [[] for x in xrange(count)]
grouplist = []
for x in objcts:
group = rs.ObjectGroups(x)
if group in grouplist:
a = grouplist.index(group)
groupedlist[a].append(x)
else:
grouplist.append(group)
b = grouplist.index(group)
print b
groupedlist[b].append(x)
for lst in groupedlist:
for x in lst:
rea = rs.Area(x)
if rea >= arealst:
arealst = rea
biggestobject = x
bb = rs.BoundingBox(biggestobject)
bb1 = bb[0]
for x in lst:
y = rs.CopyObject(x)
mvvec = rs.VectorCreate(zeropoint, bb1)
rs.MoveObject(y, mvvec)
dellist.append(y)
rs.DeleteObjects(dellist)
Thanks Wim. Here is the script.
It doesn’t work for groups inside groups though.
#a python script that takes groups of sheets,
# moves them to zero point and exports them to a folder of choice
#DOESN'T work with groups in groups, it will export every single group
#import the stuff we need
import rhinoscriptsyntax as rs
#asks for the objects
objcts = rs.GetObjects("Select objects")
"""if objcts = None:
return"""
direct = rs.BrowseForFolder(None, "Choose a folder", None)
#turn of redrawing for speed
rs.EnableRedraw(False)
#point to move to
zeropoint = [0,0,0]
#define some lists
arealst = 0
biggestobject = []
grouplist = []
groupedlist = []
a = 0
b = 0
#counting the amount of groups that are selected
for x in objcts:
group = rs.ObjectGroups(x)
if group not in grouplist:
grouplist.append(group)
count = len(grouplist)
#make list with right amount of (empty) lists
groupedlist = [[] for x in xrange(count)]
grouplist = []
#check for in which group each object is and put object in the right list
for x in objcts:
group = rs.ObjectGroups(x)
if group in grouplist:
a = grouplist.index(group)
groupedlist[a].append(x)
else:
grouplist.append(group)
b = grouplist.index(group)
groupedlist[b].append(x)
#starting number for exporting
count = 1
#Copy each list of objects (group)
for lst in groupedlist:
copylist = rs.CopyObjects(lst)
#move to zero point
bb = rs.BoundingBox(copylist)
bb1 = bb[0]
mvvec = rs.VectorCreate(zeropoint, bb1)
rs.MoveObject(copylist, mvvec)
#define filepath & export
countstr = str(count)
path = direct+"/"+"Sheet"+countstr+".dxf"
rs.SelectObjects(copylist)
commandline = '_-export "{0}" _Enter'.format(path)
rs.Command(commandline)
count += 1
#delete objects
rs.DeleteObjects(copylist)
#redraw
rs.Redraw()
Hi Dale, how to change the export directory? For example, I would like to name each 3dm file from a list of variables: itemname. I tried to conjugate the export directory like this, and it’s not working: