Best way to select objects created by current run of script?

I am working on a script that Would create 5 to 20 objects each time it is run. I would like to then block all the new objects and then place the block. What would be the best way to select just the objects created by this run of the script? Or is there a better way to add those objects to a new and unique block?

Thanks in advance.
Robb

I guess I would just make sure that the objects were selected when the script terminates… Then run Block… You could also just block them in the script.
–Mitch

My preference is to block them in the script but I can’t see how to select them… as well, I am unclear what the best approach is.

  1. Do I create the new block with the first object - add the new objects as created - then place the block?
    or
  2. Do I create all the objects - select them - block with unique name - then place the block.

These objects are on different layers and the block placement is in a different location than where the objects are created. I have no need to leave originals in the location of creation.

I’d add an array variable to the script and placed all the created objects into it one by one if they are created individually with ReDim Preserve or at once. Then just run AddBlock method with that array. No need to select anything.

If you’d rather prefer selecting your objects, you can always use PickWindow method, but keep in mind - PickWindow is view dependent (in other words you have to be zoomed out to see all the added objects on your screen).

Can you provide an example of what you are describing here?
Thanks Robb

Dim i, BlockObjs, Qty

ReDim BlockObjs(0)

Qty=…

For i=0 To Qty

BlockObjs(i) = Rhino.Add(whatever object) …

If Not i=Qty Then ReDim Preserve BlockObjs(i+1)

Next

Rhino.AddBlock BlockObjs, Array(0,0,0), “Block1”, True

Thanks - I guess I didn’t specify that I was a beginner working in Python on this problem. Am I correct in thinking your code is not Python? Struggling up the learning curve and almost think I could translate what you have provided.
Thanks again for your help.

Perhaps something like this:

import rhinoscriptsyntax as rs

#empty list
objs=[]

#create a loop (just an example)
for i in range(n):
    #do something that results in the creation of an object
    obj=SomeObjectCreationMethod()
    #add the object to the list
    objs.append(obj)
    
#objs will now have all the objects that you want to have in your block

origin = rs.coerce3dpoint([0,0,0])
#add the block definition to the document, delete the original objects
rs.AddBlock(objs,origin,"Block_Name",True)
#insert a block instance of the block
new_block = rs.InsertBlock("Block_Name",origin)

Let me know if this needs more explanation…
–Mitch

Wow - thanks… I’ll bang on this for a while and I am certain there will be questions. 1st is that it seems I should add lines 4 & 6 at the top of my def then add line 8 to each ObjectCreationMethod?

It’s RhinoScript. I learned it quick 'cause the syntax is nearly identical to 1990s MS BASIC which I knew pretty well from middle school. I wish I had time to learn Python…

The basic idea is to collect all of your objects - represented by their id’s - in one place: a single python list. That can be done in a variety of ways, my post was simply illustrating one. In fact it was done a bit too hastily, I omitted a loop, now corrected.

To add individual objects successively to a list, you can first start with an empty list: my_list=[]. Then, when you have an object to add, you can add it to the list with my_list.append(obj). If you have a method which returns multiple objects - another list - you can add it to the original list by using one of several other methods:

  1. loop through the return list and add each object successively using append as above. (the “hard way”)
    for obj in return_list: my_list.append(obj)

  2. use my_list.extend(return_list) (adds the whole new list to the existing in one go)

  3. use my_list + return_list (adding lists with “+” combines them)

Once you have all the objects you need for the block in one list, you use the AddBlock() method to create a block definition, then add one or more instances of the block with AddBlockInstance().

–Mitch

Python with rhinoscriptsyntax is not all that different than vb - there will be a learning curve, but it’s not too steep. The advantages of Python are many - if you ever want to port your scripts to Mac Rhino for example - but it’s simply a much more complete and flexible programming language. The tools for working with lists (arrays) are so much better - you will never miss your Dims and Redim Preserves…

–Mitch

This works great - Been banging on it for a while and find that that I need to create a unique name for each variation (pass through the script). The first time thru is perfect then each following pass with different variables creates the objects but fails with a block exists error and no block is created.

My intent is to throw up the UI with variables that can be changed then build a unique block based on the new variables with each run. I can’t figure out how to create a unique name for each pass of the script.

I would ideally have a base name and each pass increments that name for a unique iteration of the object properties.

def MakeKnob (bpx, bpy, bpz, SR, SH, BR, RT):
empty list
objs=
#create a loop (just an example)
for i in range(1):
#do something that results in the creation of an object
obj = rs.AddCylinder ([bpx, bpy, bpz], SH, SR)
#add the object to the list
objs.append(obj)
obj = rs.AddSphere ([bpx, bpy, bpz +SH], BR)
objs.append(obj)
obj = rs.AddTorus ([bpx, bpy, bpz +SH], BR, RT)
objs.append(obj)
#objs will now have all the objects that you want to have in your block
origin = rs.coerce3dpoint([0,0,0])
#add the block definition to the document, delete the original objects
rs.AddBlock(objs,origin,“Knob-01”,True)
#insert a block instance of the block
new_block = rs.InsertBlock(“Knob-01”,origin)

The above is just the object creation part with no UI for the variables.

btw-> Does Helvetosaur have anything to do with Massimo and retro rigor?

It can be a bit tricky, given that blocks can be named anything and that you don’t know what names already exist. There is no really clean way to pick up where you left off.

You can certainly work with sequential numbering, it will be fine for the first run of the script

block_001
block_002

block_nnn

The next run of the script, since the names already exist, you could first run a check for all names that look like block_xxx and when you find the largest xxx, you start from there. That can be a bit painful, but it’s one way to attack the problem. It also depends on how many blocks you think are going to have because it’s helpful to know how many numerical places after the “_” there might be…

–Mitch

It turns out I don’t really need the functionality of a block so Group works perfectly - letting Rhino manage the names.

My next challenge is to orient the group I have just created. OrientObject seems to just want one object and then the reference points don’t seem readily adaptable to to simple scripting. But then I am just a beginner…

TIA Robb