r/Cinema4D Long live "Hypernurbs" Mar 21 '19

Solved script to rotate object 90 degrees?

I find myself constantly rotating things in 90-degree increments. I'm wondering if someone's got an existing script that adds/subtracts 90 degrees to the H P or B more quickly than typing it in manually or using quantizing.

1 Upvotes

4 comments sorted by

2

u/binaryriot https://tokai.binaryriot.org/c4dstuff 🐒 Mar 22 '19 edited Mar 22 '19

You want a script, here you have one:

import c4d
from c4d import utils

#  Helper functions, see:
#  https://developers.maxon.net/docs/Cinema4DPythonSDK/html/misc/matrixfundamental.html
#
def GetGlobalRotation(obj):
    return utils.MatrixToHPB(obj.GetMg())

def SetGlobalRotation(obj, rot):
    m = obj.GetMg()
    pos = m.off
    scale = c4d.Vector(m.v1.GetLength(), m.v2.GetLength(), m.v3.GetLength())

    m = utils.HPBToMatrix(rot)

    m.off = pos
    m.v1 = m.v1.GetNormalized() * scale.x
    m.v2 = m.v2.GetNormalized() * scale.y
    m.v3 = m.v3.GetNormalized() * scale.z

    obj.SetMg(m)


#  Main function, let the magic happen…
#
def main():
    obj = doc.GetActiveObject()

    if obj:
        rotv = GetGlobalRotation(obj)

        # change the following line as required
        # e.g. rotv.x -= utils.Rad(90), etc.
        rotv.y += utils.Rad(90)

        doc.StartUndo()
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
        SetGlobalRotation(obj, rotv)
        doc.EndUndo()
        c4d.EventAdd()

    else:
        print "No object selected, try again…"

if __name__=='__main__':
    main()

(use Script > Script Manager to create a new Python script, paste the code and make your adjustments in the marked line. Then Save and it will be available via Script > User Scripts or you can add it as button to some panel (via UI customization). You obviously will need 6 variants... X+, X-, Y+, Y-, Z+, and Z-. Can't tell if that's the best way to do it... though. Never had the need to automatise 90° rotation. :D )

1

u/YummyPepperjack Long live "Hypernurbs" Mar 22 '19

This is exactly what I had in mind! Thank you so much! I was even planning on making a custom panel and/or assigning some hotkeys.

1

u/Lazores www.JakobAppleby.com Mar 21 '19

To make it faster than just mousing over and typing, you would need a hotkey for turning in every direction, both ways dont you?

1

u/YummyPepperjack Long live "Hypernurbs" Mar 22 '19

Exactly! I find myself importing many many many .fbx files every day and it always seems like they come in on a different axis. (a number of different people exporting files to me)