marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
1.98k stars 257 forks source link

Dictionary input for vedo.Assembly #1047

Closed JeffreyWardman closed 4 months ago

JeffreyWardman commented 4 months ago

Will implement once decided on how it's handled.

For a vedo.Assembly, you can unpack via name, however name has to be set via:

mesh.name = "name"
mesh2.name = "name2"

I recommend being able to handle vedo.Assembly({"mesh": mesh, "mesh2": mesh2}) where it just allocates the name upon initialisation if the input argument is a dictionary:

class Assembly(CommonVisual, Actor3DHelper, vtk.vtkAssembly):
    """
    Group many objects and treat them as a single new object.
    """

    def __init__(self, *meshs):
        if isinstance(meshs, dict):
            for name, mesh in meshs.items():
                mesh.name = name
        ...

It'd be nice to convert self.actors and self.objects such that you can then do assembly["mesh2"]. Did you have any thoughts on if this should/shouldn't be implemented @marcomusy? If so, what's your preferred approach for the indexing?

marcomusy commented 4 months ago

I think it's a good idea to do it in the constructor. For objects and actors they are normal python lists everywhere else. In fact you can already unpack by name via __getitem__():

msh = my_assembly["my_mesh"]

the same should happen in class Group, but it's lacking right now. (the difference between the 2 is that the assembly can be rotated and moved in 3d space, whereas groups are more generic)

JeffreyWardman commented 4 months ago

I've added dictionary as an input for both Group and Assembly. Did not resolve being able to index Group. Tested with a dictionary of one and two. Both work as intended. See #1057.

JeffreyWardman commented 4 months ago

Merged.