Getting lights using rhino3dm

Is there a way to get access to light objects in a Rhino3dm for Javascript or python?
File3dm.objects() does not return any light object. I also looked into File3dm.instanceDefinitions(), no luck there either.
Any ideas?

Hi Morteza,

Try this Python script, I modified it a bit from this example:

To just select lights:

from Rhino import *
from Rhino.DocObjects import *
from Rhino.Commands import *
from scriptcontext import doc

def RunCommand():
    object_enumerator_settings = ObjectEnumeratorSettings()
    object_enumerator_settings.IncludeLights = True
    object_enumerator_settings.ObjectTypeFilter = ObjectType.Light
    rhino_objects = doc.Objects.GetObjectList(object_enumerator_settings)
    count = 0
    for rhino_object in rhino_objects:
        if rhino_object.IsSelectable() and rhino_object.IsSelected(False) == 0:
            rhino_object.Select(True)
            count += 1;
    if count > 0:
        doc.Views.Redraw()
        RhinoApp.WriteLine("{0} light{1} selected", count, "" if count == 1 else "s")

    return Result.Success
    
if __name__ == "__main__":
    RunCommand()

Darby

PS. I’m new to Rhino/Python so I’m not sure if this is the best way but it seems to work OK on my simple test scene.

Hi @darbyjohnston,

Thanks for the code, but this I believe is Rhino Python implementation of what I was asking for. I’m looking for similar functionality within the standalone Rhino3dm library.

Ah, sorry about that! I haven’t used the Rhino3dm library, but if it’s any help this is the way I’ve been getting lights in C++ using the underlying OpenNUBRS library:

ONX_ModelComponentIterator geometryIt(onModel, ON_ModelComponent::Type::RenderLight);
for (auto onModelComponent = geometryIt.FirstComponent(); onModelComponent; onModelComponent = geometryIt.NextComponent())
{
    if (auto onModelGeometryComponent = ON_ModelGeometryComponent::Cast(onModelComponent))
    {
        if (auto attr = onModelGeometryComponent->Attributes(nullptr))
        {
            if (auto onLight = ON_Light::Cast(onModelGeometryComponent->Geometry(nullptr)))
            {
                if (onLight->IsDirectionalLight())
                {
                    ...
                }
                else if (onLight->IsPointLight())
                {
                    ...
                }
                else if (onLight->IsSpotLight())
                {
                    ...
                }
            }
        }
    }
}

@stevebaer probably knows more about this =)