CadQuery / cadquery

A python parametric CAD scripting framework based on OCCT
https://cadquery.readthedocs.io
Other
3.16k stars 289 forks source link

Add materials to assemblies #1620

Open adrianschneider94 opened 3 months ago

adrianschneider94 commented 3 months ago

As far as I can see, it's only possible to add colors so far. That could be extended to handling materials by using XCAFDoc_Material for physical properties and XCAFDoc_VisMaterial for visualization.

I implemented a first working approach: https://github.com/sangl-spezialtransformatoren/cadquery/tree/materials

The syntax is at the moment:


part = create_some_part()
assembly = Assembly()

copper = Material(
    name="Copper",
    base_color=Color(0.955, 0.637, 0.538, 1),
    density=8.9,
    metalness=1,
    roughness=0.1,
)

assembly.add(part, material=copper)

I wanted to use this issue as a discussion entrypoint, a pull request could follow later.

Links to OCCT documentation:

adam-urbanczyk commented 3 months ago

This would be interesting. What is your main use case?

adrianschneider94 commented 3 months ago

One is mass calculation, where the density is necessary. The other is, that you can export with the visualization settings, for example you can visualize metal nicely with gltf.

Jopie01 commented 3 months ago

I'm also interested in this. Maybe it's also possible to add textures? My main case is that I'm creating a structure and want to create an (rendered, would it be possible?) image of it to be added to a printed document. Also the 3D model is exported for visualization in a viewer.

lenianiva commented 3 months ago

This would be very useful for calculating load!

adrianschneider94 commented 3 months ago

@Jopie01 Textures should be possible, XCAFDoc_VisMaterialPBR has fields for that, but I don't know yet how those work.

adam-urbanczyk commented 3 months ago

Alright, let's give it a try @adrianschneider94 ! I did take a look at your branch and AFAICT you did some reformatting/reordering of imports. I will kindly ask to revert this. Our README contains instructions regarding which black version to use. Please do not apply other formatting fixes.

Regarding the PR I see the following tasks. First two are MVP, the rest would be nice to have but can ba via other PRs.

adrianschneider94 commented 3 months ago

Yes, so far I have not set up everything, just got everything working for my project.

Some points:

  1. I adapted the approach to keep all data in OCP objects using 'wrapped' attributes. But as for example XCAFDoc_VisMaterial.PbrMaterial() won't return a ref but a copy of the struct, I now think it's easier to define a python class holding the material properties and then apply them in toCAF(). What do you think about that?

  2. Exporting STEP and GLTF works out of the box (the other formats too I guess), but as far as I know, STEP does not have material properties, it will just take the base color as color.

  3. Regarding serialization: Is there already some approach in the library to serialization yet? I haven't seen it yet and even started to write own wrappers using pydantic. Would be great if that's already implemented.

adam-urbanczyk commented 3 months ago

[I updated your comment with numbers to be able to refert to the points]

[I updated your comment with numbers to be able to refer to the points]

Alright, here are my answers

  1. I did no analyze in detail, but that approach sounds OK too
  2. Indeed GLTF should work via CAF regarding vis props. IDK about material props. STEP does not support vis props other than color, but maybe we could encode material props in some way
  3. Only serialization to CAF is there. No deserialization ATM. For unrelated reasons I want to (finally!) work on pickling. It should solve it.

Long story short, I’d propose to keep the scope small and for now “just” implement materials up to the CAF part.