Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 4152

Obj_id not defined

$
0
0

Hi all. I do want to modify a python script that I am using to rotate parts into Rhino and I do want to also add an alignment operation into the same script. The last two lines of code (bb=rs.BoundingBox(obj_id)) are introduced from another script that I do use to align the top of my parts to the XY zero level. It is working as expected, the only issue I do face it is one error message that I do get every time I do run the script: name 'obj_id' is not defined

I barely know some basic Python and I can’t find a way how to fix this error.

Best regards.

"""Provide angle in degrees and axis vector as input arguments at bottom.
Selected grouped objects rotate around their collective bounding box center 
as a group.  All other objects rotate about their individual bounding box center.
Script by Mitch Heynick 13.11.23
https://discourse.mcneel.com/t/rotate-object-by-90-super-fast-method/79724/38
"""

import rhinoscriptsyntax as rs
import Rhino

def GetObjTopGroups(obj_ids):
    groups=set([rs.ObjectTopGroup(obj_id) for obj_id in obj_ids])
    return groups

def RotateObjsPlusGroupsAroundBBCtrAxis(deg,axis):
    msg="Select objects to rotate {} deg.".format(deg)
    obj_ids=rs.GetObjects(msg,preselect=True)
    if not obj_ids: return
    
    non_grouped=[]
    groups=set()
    for obj_id in obj_ids:
        otg=rs.ObjectTopGroup(obj_id)
        if not otg:
            non_grouped.append(obj_id)
        else:
            groups.add(otg)
            
    rs.EnableRedraw(False)
    
    if groups:
        for group in groups:
            group_objs=rs.ObjectsByGroup(group)
            bb=rs.BoundingBox(group_objs)
            xform=rs.XformRotation2(deg,axis,(bb[0]+bb[6])/2)
            rs.TransformObjects(group_objs,xform)
            
    if non_grouped:
        for obj_id in non_grouped:
            bb=rs.BoundingBox(obj_id)
            if bb:
                xform=rs.XformRotation2(deg,axis,(bb[0]+bb[6])/2)
                rs.TransformObject(obj_id,xform)
                
RotateObjsPlusGroupsAroundBBCtrAxis(90,Rhino.Geometry.Vector3d.XAxis)
bb=rs.BoundingBox(obj_id)
if bb: rs.MoveObject(obj_id,rs.coerce3dvector([0,0,-bb[4][2]]))

6 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 4152

Trending Articles