Skip to content

How to get a scene for each selected element using python in cadwork? #127

Description

@lucas-ita

Based on the code from the Cadwork API documentation, I have created the code below. My intention is to obtain a scene for each selected element. However, when I select multiple elements, it currently creates a scene for each one. Unfortunately, the content of each scene includes all the elements instead of having each element in a separate scene.

import element_controller as ec
import cadwork
import scene_controller as sc

# Get the IDs of all selected elements
element_ids = ec.get_active_identifiable_element_ids()

# Check if there are selected elements
if element_ids:
    # Create an individual scene for each element
    for i in range(len(element_ids)):
        scene_name = f'Scene {i + 1}'  # Add 1 to start counting from 1
        new_scene = sc.add_scene(scene_name)

        # Check if the scene was successfully created
        if new_scene:
            # Add the corresponding element to the scene
            sc.add_elements_to_scene(scene_name, [element_ids[i]])

            # Activate the scene
            sc.activate_scene(scene_name)

else:
    print("No elements selected.")

Activity

  1. NOPKGer commented on Feb 23, 2024

    @NOPKGer

    hey, you have to set the elements inactive.

    import element_controller as ec
    import cadwork
    import scene_controller as sc
    **import visualization_controller as vc**
    
    # Get the IDs of all selected elements
    element_ids = ec.get_active_identifiable_element_ids()
    **vc.set_inactive(element_ids)**
    
    # Check if there are selected elements
    if element_ids:
        # Create an individual scene for each element
        for i in range(len(element_ids)):
            scene_name = f'Scene {i + 1}'  # Add 1 to start counting from 1
            new_scene = sc.add_scene(scene_name)
    
            # Check if the scene was successfully created
            if new_scene:
                # Add the corresponding element to the scene
                sc.add_elements_to_scene(scene_name, [element_ids[i]])
    
                # Activate the scene
                sc.activate_scene(scene_name)
    
    else:
        print("No elements selected.")
    
  2. Brunner246 commented on Feb 23, 2024

    @Brunner246
    Contributor

    We will check this behaviour, looks like an unexpected behaviour. As an List[elementId] is explicitly passed as a parameter, only this should be added to the scene.

    import element_controller as ec
    import scene_controller as sc
    import visualization_controller as vc
    
    
    def main():
        element_ids = ec.get_active_identifiable_element_ids()
        vc.set_inactive(element_ids) # this should not be necessary in the future (bug)
        print(f"{len(element_ids)} active elements.")
        for index, element_id in enumerate(element_ids):
            scene_name = f"Scene {index}"
            if sc.add_scene(scene_name):
                sc.add_elements_to_scene(scene_name, [element_id])
    
    
    if __name__ == '__main__':
        main()
  3. Brunner246 commented on Feb 26, 2024

    @Brunner246
    Contributor

    The function corresponds to the standard behaviour of cadwork.
    add_scene creates a scene and adds the active elements.
    Therefore, the elements must be deactivated before the scene is added.

  4. lucas-ita commented on Feb 27, 2024

    @lucas-ita
    Author

    Thanks!

  5. locked and limited conversation to collaborators on Mar 1, 2024
  6. converted this issue into a discussion #131 on Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions