Obj_id not defined

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]]))

obj_id is not defined in the scope of the code you’re using it as a variable. it is defined inside the def but not outside it. in python, the variables defined inside a function are not available outside it

And do you have any idea how I can make this script to work without errors? Because it is working as it is, the only issue it is the error. How I can take the selected objects from the above function and pass-it into the last two lines of code?

i’m gonna make a guess here, because i’m not too sure about the exact functionality you’re looking for


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)
            rs.MoveObject(obj_id,rs.coerce3dvector([0,0,-bb[4][2]])) ##

    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)
                rs.MoveObject(obj_id,rs.coerce3dvector([0,0,-bb[4][2]])) ##

RotateObjsPlusGroupsAroundBBCtrAxis(90,Rhino.Geometry.Vector3d.XAxis)

i moved the MoveObject inside the def

Unfortunately it is not working. I do need to align the parts top to XY zero plane after I do rotate the objects.

Looks like this it is working:

obj_ids=rs.GetObjects(preselect=True)
bb=rs.BoundingBox(obj_ids)
if bb: rs.MoveObject(obj_ids,rs.coerce3dvector([0,0,-bb[4][2]]))

I just copied the obj_ids=rs.GetObjects(preselect=True) from the beginning of the script. The only drawback it is that now I do need to have the objects preselected before running the script.