addObject() and undo

Scripting
Pete
2016-08-13
2016-08-15
  • Pete

    Pete - 2016-08-13

    Hi!

    This is not necessarily any major issue but rather an annoyance after repeating the same thing over and over.

    I have a (groovy) script that adds objects to the scene, based on another object that is selected in the beginning. It is much longer, but the basic structure is like:

    scene = window.getScene();
    oInfoRoot = window.getSelectedObjects()[0];
    
    undo = new UndoRecord;
    window.setUndoRecord(undo);
    
    many = 100; // for example
    
    for (i = 0; i < many; i++)
    {
        // doing some caculations
        // creating a new object
        window.addObject(...., undo):
    }
    
    window.updateImage();
    window.rebuildItemList();
    

    Now, when I hit Ctrl-Z, the scene is supposed to be reverted back to how it was before running the script? It does almost that. What happens is that the selected object is no longer selected, though it appears highlighted on the object list.

    I had a look at the code and where ever there is .addObject() there also seems to be an undorecord for returning the selection to what it used to be, but somehow it does not work in this case. Then, when I tried to add that undorecord myself,

    Object[] uObj = {window.getSelectedIndices()};
    undo.addCommand(UndoRecord.SET_SCENE_SELECTION, uObj);
    

    ...the selection is correct after using undo, but the views are not updated. The removed objects stay visible until I click a view or pick a different object on the list. Now it looks, that it is allowed to create an element to the object[] out of window.updateImage() but I have no idea, how to make that have any effect on the views. I also tried to use addCommandAtBeginning() (after the loop) but that just entirely disables the undorecord.

    EDIT: After adding the SET_SCENE_SELECTION to undo, it looks like the the 'oInfoRoot' were selected, but it really is not. At least the script does not recognize it.

    -P-

     
    Last edit: Pete 2016-08-13
  • Peter Eastman

    Peter Eastman - 2016-08-14

    Try this:

    selected = window.getSelectedIndices();
    undo = new UndoRecord(window, false);
    window.setUndoRecord(undo);
    for (i = 0; i < 10; i++)
        window.addObject(new Cube(1,1,1), new CoordinateSystem(), "cube", undo);
    undo.addCommand(UndoRecord.SET_SCENE_SELECTION, (Object[]) [selected]);
    

    Peter

     
  • Pete

    Pete - 2016-08-15

    Thanks. That works.

    So, what made the difference was, that I should have used []'s instead of {}'s around the selected.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks