pythonrhino3d

rhino python script move object


i'm going to script a code to get 2 dimension and draw a rectangel then repeat that u-times by rotating and moving it.and present all rectangels.. i've wrote this:

 #start
import rhinoscriptsyntax as rs
#set the work plane
plane = rs.WorldXYPlane()
#draw a rectangel 
a=rs.AddRectangle( plane, x, y )

#set a module to repeat rectangels 
c=[]
for i in range(u):
    plane = rs.RotatePlane(plane, i+2.0, [0,0,1])
    c=c.append(rs.AddRectangle(plane, x, y ))
    d=rs.MoveObject(c[i],(0,0,1))

it declare this error:

Runtime error (TypeErrorException): 'NoneType' object is not subscriptable

Traceback:
  line 13, in script

how i can fix it?


Solution

  • The error is from c=c.append(rs.AddRectangle(plane, x, y )) As @byxor commented, and should simply be c.append(rs.AddRectangle(plane, x, y ))

    There are several other issues with the code that may cause side effects based on your stated goal.

    1. a=rs.AddRectangle( plane, x, y ) is assigned but not used. If you are scripting in grasshopper and using a as the default output, you will only get a single rect as a result.
    2. plane = rs.RotatePlane(plane, i+2.0, [0,0,1]) is re-using plane so you will get a double increment with i+2.0 (prior rotation plus new rotation). This is not necessarily wrong, but a bit less intuitive than i*2.
    3. d=rs.MoveObject(c[i],(0,0,1)) moves your item, but d is not returned and is overwritten by the next iteration. Also, you are moving all items to the same location and if you want increasing movement, you need an incrementor like (0,0,1*i).

    Bellow is an example that is a bit more clear, and assumes X,Y are float and U is a int with a the grasshopper script component output.

    Working Example:

    #Imports.
    import rhinoscriptsyntax as rs
    
    #Set the work plane.
    plane = rs.WorldXYPlane()
    
    #Create Output Array. 
    c=[]
    
    #Create rectangles.
    for i in range(u):
        #Rotate by increasing increments
        newPlane = rs.RotatePlane(plane, i*2.0, [0,0,1])
        #Create rectangle.
        rect = rs.AddRectangle(newPlane, x, y )
        #Move rectange by increasing increments.
        rs.MoveObject(rect,(0,0,1*i))
        #Add final rectangle to output list.
        c.append(rect)
    
    #Return final List.
    a = c