Save the current "selection set" and restore later

Next
 From:  fcwilt
11708.1 
Hi,

I would like to be able to take a "snap shot" of the current state of selected/unselected objects and restore it when desired.

I know how to iterate over the objects and determine the selected/unselected state.

What I don't know how to do is save the information to a file that I can later read and restore the selection state.

For all I know this capability already exists and I simply don't know about it.

Any help would be most appreciated.

Thanks.

Frederick
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11708.2 In reply to 11708.1 
Hi Frederick, well there is a way to do this currently - assign a name to your selected objects and then that name will appear in the "Objects" section of the scene browser. You can click on the right-hand side of that item (on the selection dot indicator) to restore selection.

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  fcwilt
11708.3 In reply to 11708.2 
Hi Michael,

They already have names that are meaningful.

In some cases an imported step will have several parts but in most cases I have no need to have those parts have unique name. So there I do get the set of parts a common name.

But I don't see that approach working for me in the majority of cases.

Does MoI have the abillity to write and read text files?

Thanks.

Frederick
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11708.4 In reply to 11708.3 
Hi Frederick,

re:
> Does MoI have the abillity to write and read text files?

Yes, see examples in ImportPointFile and SavePointFile at:
http://moi3d.com/download/scripts/PetrsMoiPage/PetrsMoiPage.htm

read:
code:
var file = moi.filesystem.openFileStream( 'filename', 'r' );
while ( !file.atEOF )
{
    var line = file.readLine();
    // do something with line of text.
}


write:
code:
var file = moi.filesystem.openFileStream( 'filename', 'w' );
file.writeLine( 'Line of text' );
file.close();


But it's probably not going to work very well to store it as an id value in a file instead of as an object property
like a name. It will break if you make any changes like just moving the object.

re:
> They already have names that are meaningful.

Ok, then just use obj.setUserText( key, value ) instead of obj.name .

For example - Set selection set 0:
code:
var objs = moi.geometryDatabase.getSelectedObjects();
for ( var i = 0; i < objs.length; ++i )
{
    var obj = objs.item(i);
    obj.setUserText( 'saved_selection_set_0', '1' );
}


Restore selection set 0:
code:
var objs = moi.geometryDatabase.getObjects();
for ( var i = 0; i < objs.length; ++i )
{
    var obj = objs.item(i);
    if ( obj.getUserText( 'saved_selection_set_0' ) == '1' )
        obj.selected = true;

    var subobjs = obj.getSubObjects();
    for ( var j = 0; j < subobjs.length; ++j )
    {
        var subobj = subobjs.item(j);
        if ( subobj.getUserText( 'saved_selection_set_0' ) == '1' )
            subobj.selected = true;
    }
}


Unlike storing id values in a separate file, the user text will inherit down to transformed
objects so it won't break so easily. And it will be saved directly in the .3dm file so you
won't have to manage separate files.

For the store part it might be good to clear that mark off of unselected objects unless you want it to be cumulative.

So this is probably better for the store part:
code:
/* Clear user text with key = 'saved_selection_set_0' off of all objects and sub objects. */
var all_objs = moi.geometryDatabase.getObjects();
for ( var i = 0; i < all_objs.length; ++i )
{
    var obj = all_objs.item(i);
    var clear_objs = obj.getSubObjects();
    clear_objs.addObject( obj );
    for ( var j = 0; j < clear_objs.length; ++j )
    {
         var clear_obj = clear_objs.item(j);
         clear_obj.removeUserText( 'saved_selection_set_0' );
    }
}

/* Store user text with key 'saved_selection_set_0' on selected objects. */

var objs = moi.geometryDatabase.getSelectedObjects();
for ( var i = 0; i < objs.length; ++i )
{
    var obj = objs.item(i);
    obj.setUserText( 'saved_selection_set_0', '1' );
}


- Michael

EDITED: 13 Apr by MICHAEL GIBSON

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11708.5 In reply to 11708.4 
Oh yeah also in MoI v5 there is a UI for adding, removing or editing user text values in the "Details..." object properties dialog in the "Object user text" section:



- Michael

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  fcwilt
11708.6 In reply to 11708.4 
Hi Michael,

The functionality you have built into MoI is amazing.

The ability to attach arbitrary bits of user information is first rate.

And I like the idea of saving the "selection set" info in the 3DM file itself.

I gather the ID of an object is not like a GUID that is unique and unchanging.

Is it just the current index into a given set of things, subject to change as things are added/deleted?

Thanks much!

Frederick
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  fcwilt
11708.7 In reply to 11708.4 
Hi Michael,

I changed the code a little to have a function to just remove the selection user info. So now I have command SelectionSetSave, SelectionSetRestore and SelectionSetClear.

And it is all working just fine, just what I needed.

In your example code you used a command removeUserText.

What other commands are there related to user text?

Thanks much.

Frederick
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11708.8 In reply to 11708.6 
Hi Frederick, great I'm glad that does what you were needing.

re:
> I gather the ID of an object is not like a GUID that is unique and unchanging.
>

Actually the ID of an object is a unique GUID that doesn't change.

The issue is that it is tied to that specific version of the object. With any type of editing
(including transforms), the result of the edit will have it's own new unique ID value.


re:
> In your example code you used a command removeUserText.
>
> What other commands are there related to user text?

From:
http://moi3d.com/wiki/V5Beta#Mar-28-2023

Implement document user text for scripts
New document user text values so scripts can add data that is saved and restored from 3DM files.
New methods under moi.geometryDatabase:
.setUserText( Key, Value ); - Set text value for given key.
.getUserText( Key ); - Returns text value for given key, or undefined if not present.
.removeUserText( Key ); - Remove user text value for given key.
.getAllUserText(); - Return list of all user text, each object has .key and .value properties.
.clearAllUserText(); - Clear all document user text.

There is also now object user text available for scripts to store data on an object:
.setUserText( Key, Value ); - Set text value for given key.
.getUserText( Key ); - Returns text value for given key, or undefined if not present.
.removeUserText( Key ); - Remove user text value for given key.
.getAllUserText(); - Return list of all user text, each object has .key and .value properties.
.clearAllUserText(); - Clear all object user text.

Document user text can also be viewed and edited under File > Notes > "Document user text" button and object user text will show on the "Details..." object properties dialog.

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
 From:  fcwilt
11708.9 In reply to 11708.8 
Morning Michael,

The ability to save/restore a set of selected objects was so useful, I created a similar set of commands to save/restore a set of "not hidden" objects which has already proven to be even more useful.

I'm also planning on using that same object related key/value feature (for lack of a better term) to create my sets of objects to be exported, rather than use a object name prefix.

My next goal is to figure out how to display a "dialog" like you do, in the upper right corner, which would allow specifying a set number (1 to 9) and provide proceed/cancel buttons.

I think I've got enough bits of info collected to get that working.

This scripting stuff is the greatest thing since sliced bread. ;)

Frederick
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged
 

Reply to All Reply to All