Python, looping object guid

Hello !
I’m completely new into scripting, and I am trying to scale every polysurface of a rhino project from its centroid.
So far, I managed to get this:

import rhinoscriptsyntax as rs

obj = rs.GetObject("Select a polysurface", rs.filter.polysurface)

if obj:

    massprop = rs.SurfaceAreaCentroid(obj)

    if massprop: centroid = rs.AddPoint( massprop[0] )

    center3d = rs.PointCoordinates(centroid)

    rs.ScaleObject(obj, center3d, [0.9,0.9,0.9] )

As I said, I’m completely new. Now I’m trying to apply this scale to every polysurface of the file. And loops seam as a foreign language to me. If I manage to have a list with every guid of the polysurfaces, who do you I apply a transformation to each ?

Thank you.

(Sorry for my bad english.)

For loops in Python are pretty straightforward. The syntax is usually:

for item in list:
    #do something

The list can be any sort of iterable – a range of numbers, a string, an array of guids. And item is just the current item in the list that the following code will operate on. See the following links for more information:

You can get a list of objects by type by using the Rhinoscript function ObjectsByType. To adapt your code, you need to wrap your centroid scaling operations (the last four lines) in the for loop. Then you change the variables accordingly for the iterator and item. See the code below for a working example:

import rhinoscriptsyntax as rs

objs = rs.ObjectsByType(16, select=False)	#get all polysurfaces in model

if objs:
	scaled_objs = []	#empty list to store scaled objs further operations

	rs.EnableRedraw(False)		#disable redraw to speed up script

	#start of for loop
	for i in range(len(objs)):
		obj = objs[i]				#get polysurface at current index
		massprop = rs.SurfaceAreaCentroid(obj)
		if massprop: centroid = massprop[0]

		scaled = rs.ScaleObject(obj, centroid, [0.9,0.9,0.9], copy=False)

		scaled_objs.append(scaled)		#add scaled object to list
	#end of loop

	rs.EnableRedraw(True)	#enable redraw to finalize operation
1 Like

Awesome ! Thank you ! It really helps me. Glad to see you help beginners :smiley:
Everything start to be a bit clearer.
Thank you again !

1 Like