`rotate()` does not set `BBAccumRotation`?

189 Views Asked by At

Illustrator uses the BBAccumRotation to track an object's rotation in radians 0.51246700.

BBAccumRotation IS set if object:

  • Is currently rotated - radians = value of rotation
  • Was previously rotated and is currently at 0 degrees - radians = 0.000000
  • Is rotated by dragging a handle
  • Is rotated via Object > Transform > Rotate
  • Is rotated via Properties panel > Rotate

BBAccumRotation IS NOT set if object:

  • Was rotated via a script using rotate()
  • Is rotated via live Effects > Distort & Transform > Transform > Rotate
  • Was rotated with a live effect and expanded

The question is, is there a native / baked-in way to have rotate() set BBAccumRotation?

While it is possible to add a BBAccumRotation tag to an object, it gets treated like any custom tag - it isn't recognized by Illustrator at its rotation tag, so it isn't reflected in places like the Properties panel.

I understand I could use a custom tag to track what rotate() is doing - I'd rather not go there.

It just seems like rotate() should set BBAccumRotation and I'm hoping there's something I don't know.

If you're interested in seeing this for yourself, here are some test scripts:

Check an object's tags:

if (app.documents.length == 0 ) {
    alert('Open a document to run this script.');
} else if (app.activeDocument.selection.length != 1)  {
    alert('Select 1 object to run this script.');
} else {
    TagAlert();
}


function TagAlert() {

    var iDoc = app.activeDocument;
    var aObj = iDoc.selection;
    var nObj = aObj.length;

    for ( i = 0; i < nObj; i++ ) {

        var obj = selection[0];
        var aTags = obj.tags;
        var nTags = aTags.length;

        if (nTags == 0) {

            alert('No tags.')

        } else {

            for ( i = 0; i < nObj; i++ ) {

                var obj = aObj[0];
                var aTags = obj.tags;
                var nTags = aTags.length;

                for ( i = 0; i < nTags; i++ ) {
                    tagName = aTags[i].name;
                    tagVal = aTags[i].value;
                    alert(tagName + ' ' + tagVal);
                }
            } // for

        } // if
    } // for

}; // TagAlert

Rotate an object 30 degrees:

if (app.documents.length == 0 ) {
    alert('Open a document to run this script.');
} else if (app.activeDocument.selection.length != 1)  {
    alert('Select 1 and only 1 object to run this script.');
} else {
    RotateObject();
}

function RotateObject() {

    var iDoc = app.activeDocument;
    var aObj = iDoc.selection;
    var nObj = aObj.length;

    for(i = 0; i < nObj; i++) { 
        aObj[i].rotate(30);
    }

};

Thank you in advance :)

2

There are 2 best solutions below

0
Manny Morales On

The [Tags].add() method returns a Tag object that, when renamed to match a built-in tag such as, 'BBAccumRotation' is recognized by illustrator normally, such as in the properties panel.

The below code rotates the first selected object -22 degrees using .rotate() then rotates the bounding box accordingly. Note that [Tags].value must be converted from a String to a Number. Also note that this feature does not appear to be documented.

#target illustrator

// convert back to radians
function deg2Rad( n ) {
    return n * ( Math.PI / 180 );
}
function rotateBBox( layer, degrees ) {
    // `tag` will revert to an exiting BBAccumRotation tag if it exists
    // if it doesn't exit, it is created
    var tag = null;
    var i;
    for ( i = 0; i < layer.tags.length; i++ ) {
        if ( layer.tags[i].name === 'BBAccumRotation' ) {
            tag = layer.tags[i];
        }
        i += 1;
    }
    if ( !tag ) {
        tag = layer.tags.add();
        tag.name = 'BBAccumRotation';
    }
    if ( tag.value ) {
        // tag.value must be converted from a String
        tag.value = Number( tag.value ) + deg2Rad( degrees );
    } else {
        tag.value = deg2Rad( degrees );
    }
}
function main() {
    var doc = app.activeDocument;
    var angle, sel;
    if ( doc.selection.length < 1 ) {
        alert( 'At least one (1) layer must be selected.' );
        return 1;
    } else {
        doc = app.activeDocument;
        sel = doc.selection[0];
        angle = -22;
        sel.rotate( angle );
        rotateBBox( sel, angle );
    }
}
main();```
0
Yuri Khristich On
function rotate(item, degrees) {
    var angle = 0;
    try {
        angle = parseFloat(item.tags["BBAccumRotation"].value);
    } catch(e) {
        var tag   = item.tags.add();
        tag.name  = "BBAccumRotation";
        tag.value = 0;
    }
    item.tags["BBAccumRotation"].value = angle + degrees*Math.PI/180;
    item.rotate(degrees);
}

var item = app.activeDocument.selection[0]; // no tag

rotate(item, 15);  // BBAccumRotation 15°
rotate(item, 15);  // BBAccumRotation 30°
rotate(item, -25); // BBAccumRotation 5°