15 Replies Latest reply on Jan 12, 2015 4:30 AM by daitranthanhoa

    How reset the rotation of a PageItem (or access it's matrix at least) ?

    orgicus

      I'd like to apply rotation to a few PageItem instances, but would like to reset the rotation first so it doesn't accumulate when I run the same script multiple times. I'm having a hard time trying to figure out how to restet the transformation matrix.

       

      I've spotted this post which is close to what I need but it's a bit old now.

      It looks like there isn't a matrix property anymore.

      This:

       

      alert(app.activeDocument.selection[0].matrix);
      

      ouputs undefined

       

      I've tried

       

      app.activeDocument.selection[0].transform(app.getIdentityMatrix());
      

      but it seems transformations are accumulated, multiplied, not reset.

       

      If I want to reset the rotation of a PageItem, how would I go about doing that ?

       

      Thanks,

      George

        • 1. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
          CarlosCanto Adobe Community Professional & MVP

          not all objects have a Matrix property, only TextFrames and PlacedItems if remember correctly.

           

          why don't you use

          app.activeDocument.selection[0].rotate(angle);

          1 person found this helpful
          • 2. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
            orgicus Level 1

            Thanks Carlos, but if I do

             

            app.activeDocument.selection[0].rotate(0);

             

             

            the expected behaviour is the object's rotation's reset to 0

            but that doesn't happen, rotation doesn't change since rotate

            seems to perform relative not absolute rotations

             

            So for example, if I call rotate(90) two times I get the selected objected rotated at 180 degrees

             

            Any other ways to reset the rotation ? Or at least extract the current rotation ?

            • 3. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
              CarlosCanto Adobe Community Professional & MVP

              ok, chech this thread and see if it helps, my workaround on post # 5, but read it all, good info there.

               

              http://forums.adobe.com/message/4478643#4478643

              1 person found this helpful
              • 4. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                orgicus Level 1

                Thanks Carlos,

                 

                This is very helpful. I didn't know about tags.

                This kind of works for my use case, but then again I might be using this the wrong way.

                 

                If I want to rotate back to an original transform, I do something like

                 

                 

                for(var i = 0 ; i < sel.length; i++){
                    if(sel[i].tags.length > 0)
                            if(sel[i].tags[0].name == "BBAccumRotation") sel[i].rotate(-sel[i].tags[0].value* 57.2957795);
                }
                app.redraw();
                
                

                 

                Which works the first time, but if I keep doing this the object keeps rotating.

                I then removed the tag after rotating in reverse:

                 

                sel[i].tags[0].remove();
                

                 

                The problem is, in my case the objects might be rotated from code (using .rotate()) prior to reseting, so no tag is generated.

                I've noticed I can't instantiate a Tag object or insert anything to the Tags object.

                 

                Am I using these wrong ?

                 

                The functionality I'd like to have access from script seems to be Reset Transformation, but I have no clue

                if that's accessible from the JavaScript API and if so, how.

                 

                Thanks again for the showing me the Tags, pretty handy!

                • 5. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                  CarlosCanto Adobe Community Professional & MVP

                  then no, I don't think there's a way to reset an object back to zero...but you can certainly add tags to your object,

                   

                  you can check for the tag existance, if none, then add it and add the ammount of rotation to it (if you're rotating by script)

                  1 person found this helpful
                  • 6. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                    orgicus Level 1

                    Hi Carlos,

                     

                    That sounds like all that I need to do:

                    If I can add a tag, I can simply create one, add the rotation and on a different run of the script, remove it.

                     

                    I've tried to instantiate a Tag like so: var t = new Tag()

                    but I get an error:

                    Tag does not have a constructor

                    I've tried to find out more about the Tag object but I got this:

                    function Tag() {

                        [native code]

                    }

                     

                    And in the Object Model Viewer I can only see these memebers:

                    name,parent,remove(),removeAll(),typename,value

                     

                    If you can let me know how I can add a tag and add the ammount of rotation by script that would be awesome !

                     

                    Thanks again,

                    George

                    • 7. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                      CarlosCanto Adobe Community Professional & MVP

                      check the Scripting Reference, it has a couple of samples, this is one of them

                       

                      Using tags

                      // Finds the tags associated with the selected art item,
                      // show names and values in a separate document
                      if ( app.documents.length > 0 ) {
                      doc = app.activeDocument;
                      if ( doc.selection.length > 0 ) {
                      for ( i = 0; i < selection.length; i++ ) {
                      selectedArt = selection[0];
                      tagList = selectedArt.tags;
                      if (tagList.length == 0) {
                      var tempTag = tagList.add();
                      tempTag.name = "OneWord";
                      tempTag.value = "anything you want";
                      }
                      // Create a document and add a line of text per tag
                      reportDocument = app.documents.add();
                      top_offset = 400;
                      for ( i = 0; i < tagList.length; i++ ) {
                      tagText = tagList[i].value;
                      newItem = reportDocument.textFrames.add();
                      newItem.contents = "Tag: (" + tagList[i].name +
                      " , " + tagText + ")";
                      newItem.position = Array(100, top_offset);
                      newItem.textRange.size = 24;
                      top_offset = top_offset - 20;
                      }
                      }
                      }
                      }
                      
                      • 8. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                        orgicus Level 1

                        Sweet! That totally worked. Don't know how I missed that in the docs.

                         

                        Here's a fixed silly little script that points a bunch of selected objects towards a selected target/point of interest:

                         

                        /*
                         *      Rotates a bunch of selected items towards a chosen target
                         *       
                         *      Usage: select at least 2 objects and mark the "look at" target by having POI in the name of the item
                        */
                        #target illustrator
                        
                        
                        var d = app.activeDocument;
                        var s = d.selection;
                        var hasDocCoords = app.coordinateSystem == CoordinateSystem.DOCUMENTCOORDINATESYSTEM;
                        
                        
                        var poi = getPOI(s);
                        if(s.length > 1 && poi != undefined){
                            var lookAt = getPos(poi);
                            for(var i = 0 ; i < s.length; i++){
                                 if(s[i] != poi){
                                    var pos = getPos(s[i]);
                                    //check if there's a rotation applied, if so, remove it first
                                    if(s[i].tags.length > 0){
                                        if(s[i].tags[0].name == "BBAccumRotation"){
                                            s[i].rotate(s[i].tags[0].value* -57.2957795);//reverse rotate
                                            s[i].tags[0].remove();
                                        }
                                     }
                                    //get the angle using atan2 and the difference vector between the two positions(current object and poi)
                                    var angle = Math.atan2(pos[1]-lookAt[1],pos[0]-lookAt[0]);
                                    //if it doesn't have a rotation tag, add one so it can be removed when the script is reapplied
                                    if(s[i].tags.length == 0){
                                        var t = s[i].tags.add();
                                        t.name = "BBAccumRotation";
                                        t.value = angle;
                                    }
                                    s[i].rotate(angle * 57.2957795);//finally convert radians to degrees
                                }
                            }
                            app.redraw();
                        }
                        function getPOI(s){//find POI in selection
                            for(var i = 0 ; i < s.length; i++)
                                if (s[i].name.toUpperCase().indexOf("POI") >= 0) return s[i];
                        }
                        function getPos(o){
                            var pos = hasDocCoords ? d.convertCoordinate (o.position, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM) : o.position;
                            pos[0] += o.width;//offset to centre of object
                            pos[1] -=  o.height;  
                          return pos;  
                        }
                        
                        

                        Thanks for making this a lot less buggy (the script can used multple times)

                        • 9. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                          orgicus Level 1

                          Carlos,

                           

                          As a side question, what's the best way to use move around the JS API ?

                          For example I didn't see the add() function in Object Model Viewer or ExtendScript Toolkit autocomplete.

                          I forgot to check the scripting reference.  Is the scripting reference the best thing to use ?

                          How do I work out wether I'm looking in the wrong place or not ? The API/Docs still feel a bit weird.

                           

                          Thanks!

                          • 10. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                            CarlosCanto Adobe Community Professional & MVP

                            you're welcome, good job.

                             

                            Is the scripting reference the best thing to use ?

                            How do I work out wether I'm looking in the wrong place or not ?

                            to me personally, I find the Reference(s) invaluable tools, I couldn't do without them. Unfortunately they're not infallible and I must/have to look at the OMV (which I hate) too as proven with the most recent release (cs6), the OMV lists lots of new Objects/Properties/Methods while the Reference doesn't...signature Adobe Style.

                             

                            bottom line is, there's no wrong place to look at, we have to look at ALL we're given, which is not much by the way...that's part of the fun, the discovery that is.

                            1 person found this helpful
                            • 11. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                              orgicus Level 1

                              Thanks again Carlos !

                              I'll keep an eye out for all resources (reference(s) ,OMV,etc.) in the future.

                               

                              Generaly I enjoy the discovery process, but for something simple as this script

                              it was easy to quicky prototype/test, but a lot more time consuming and frustrating

                              to make sure it works well (undocummented tag, notes on coordinates and conversion,etc.)

                              I was expecting this script to take a couple of hours maximum to whip out but took

                              longer because of incomplete/confusing documentation.

                               

                              Long story short, if there are any Adobe employees on the forums/this thread, this is how I wish to see documenation:

                              1. Complete (no undocumented functionalities)
                              2. Including examples. I know there are example scripts, but I mean something like the JSFL docs where most functions have example on how to use the calls
                              3. A place for issues: bugs with hacks/workaround for commonly used tasks. I understand people will have to work around limitations. Common things like: setting text to the clipboard, getting coordinates, etc. shouldn't be this hacky in the 1st place, but if they need to be at least document them

                               

                              And I'm done with my rant

                              • 12. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                                [Jongware] Most Valuable Participant

                                > As a side question, what's the best way to use move around the JS API ?

                                For example I didn't see the add() function in Object Model Viewer or ExtendScript Toolkit autocomplete.

                                 

                                The OMV browser in the ESTK editor is .. rather clumsy. I rewrote the Help files in handily browsable HTML files, and added more links. "add" can now be found in the logical place: as function of "Tags". See

                                 

                                http://jongware.mit.edu/iljscs6html/iljscs6/pc_Tags.html

                                 

                                If you are working on Windows, or don't mind downloading a viewer for Mac, you can also use the CHM version. This is even more convenient, as you can do a global Search for any keyword. Compiled CHM versions of the same Help can be downloaded from my website: http://www.jongware.com/idjshelp.html (scroll down to the section Illustrator CS6, then select the CHM version).

                                • 13. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                                  orgicus Level 1

                                  Thanks Jong, That's a very useful reference!

                                   

                                  There are chm viewers for osx and also it's possible to save a local copy of that folder with a tool like httrack.

                                  • 14. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                                    CarlosCanto Adobe Community Professional & MVP

                                    that's right, I forgot to mention JW help files, much friendlier that the OMV and Peter Kahrel's ScriptUI for dummies

                                    http://www.kahrel.plus.com/indesign/scriptui.html

                                    • 15. Re: How reset the rotation of a PageItem (or access it's matrix at least) ?
                                      daitranthanhoa Level 1

                                      I rotate 80, but ange=rotationTag.value * 57.2957795 return -10.

                                      Text in TextFrame display not correct.