Skip to content

Support for materials #1815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

adrianschneider94
Copy link

Implemented material support.
In this approach, materials are stored in python dataclasses, the OpenCascade objects are only created when building the assembly.
Tests/docs are missing at the moment.

@shimwell
Copy link
Contributor

This looks great, I wonder if there is ever a use case for a material to be a string in as an option in addition to these nice material classes you have here. I was wondering if something like this would be possible / useful

assy = cq.Assembly()
assy.add(cube, name="red_cube", material="steel")

@adam-urbanczyk
Copy link
Member

@shimwell it would certainly be nice to have some predefined materials, and maybe allow to use the material name for other things too. But let's start with making CI green.

@adrianschneider94 adrianschneider94 force-pushed the materials-new branch 3 times, most recently from 38218df to 36308a6 Compare April 17, 2025 19:41
Copy link

codecov bot commented Apr 17, 2025

Codecov Report

Attention: Patch coverage is 98.50187% with 4 lines in your changes missing coverage. Please review.

Project coverage is 95.74%. Comparing base (f754469) to head (4a0bc97).

Files with missing lines Patch % Lines
cadquery/materials.py 98.80% 0 Missing and 2 partials ⚠️
cadquery/occ_impl/assembly.py 96.61% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1815      +/-   ##
==========================================
+ Coverage   95.66%   95.74%   +0.07%     
==========================================
  Files          28       29       +1     
  Lines        7431     7606     +175     
  Branches     1122     1150      +28     
==========================================
+ Hits         7109     7282     +173     
  Misses        193      193              
- Partials      129      131       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adrianschneider94
Copy link
Author

Material support in step exporting will get better in OCCT:
Open-Cascade-SAS/OCCT#447

@adrianschneider94
Copy link
Author

adrianschneider94 commented Apr 18, 2025

This looks great, I wonder if there is ever a use case for a material to be a string in as an option in addition to these nice material classes you have here. I was wondering if something like this would be possible / useful


assy = cq.Assembly()

assy.add(cube, name="red_cube", material="steel")

Would probably nice to have a set of materials to get started, but I think, any real-world application will require custom material sets.
Maybe there's another solution and we can find an existing material repository that can be utilized.

(edit: for example https://physicallybased.info)

@adrianschneider94
Copy link
Author

VRML export runs, but is not correct yet.

@adrianschneider94 adrianschneider94 marked this pull request as draft April 20, 2025 08:41
@adrianschneider94 adrianschneider94 force-pushed the materials-new branch 7 times, most recently from 9bad718 to a76304a Compare April 25, 2025 17:05
@adrianschneider94 adrianschneider94 marked this pull request as ready for review April 25, 2025 17:58
@adrianschneider94 adrianschneider94 force-pushed the materials-new branch 5 times, most recently from 2a8410b to a4232a0 Compare April 28, 2025 08:29
@adrianschneider94
Copy link
Author

The PR would be ready from my side for a first iteration.

I think it would be great to add textures later on, one could create quite good visualizations directly with cadquery/vtk. For the time, the PBR materials already achieve quite nice results.

@adam-urbanczyk
Copy link
Member

Thanks for all the hard work! Are the new classes picklable?

@adrianschneider94
Copy link
Author

Yes, they are pure dataclasses/basic python types, so everything can be pickled.

How far is the progress on the serialization topic? Have you thought about using pydantic? I plan to implement model caching in our internal project and json would be easier to use in a db like with Postgres' JSONB column or redis' JSON datatype compared to pickle.

@adam-urbanczyk
Copy link
Member

adam-urbanczyk commented Apr 28, 2025

Perfect, thanks!

There are no plans to implement serialization beyond pickling which is already there. For caching I'd suggest joblib, but maybe you have something else in mind.

@jmwright
Copy link
Member

@adrianschneider94 @adam-urbanczyk What action items are left to do before getting this PR merged?

@adam-urbanczyk adam-urbanczyk self-requested a review May 22, 2025 19:58
@adam-urbanczyk
Copy link
Member

Review... Given the size of the PR it might take a while, but we'll definitely get this merged.

@jmwright
Copy link
Member

I'll plan to spend some time reviewing it next week then.

@jmwright
Copy link
Member

I see that this branch has conflicts now that I must have missed last week. Should it be rebased?

Copy link
Member

@adam-urbanczyk adam-urbanczyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for all the hard work, but this requires some serious rework with backward compatibility in mind. May I as why there are so many changes that are not needed?



@dataclass(frozen=True)
class Color:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have the the original Color class not removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new Color class has the same behaviour as the old one (except toTuple, I will bring that back as alias for rgba()). I think it makes sense to unify color handling throughout the codebase instead of mixing Color, str, and tuple (with 3 or 4 floats). As I touched every place where colors come up, and need them inside materials, it seemed obvious to me to update it (but you're right, backwards compatibility in the vtk functions is partially broken).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose to go back to the original class. Maybe also move the materials to the assy module.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale behind that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimize the impact of the PR. For the second point, the organization of your module does not follow what is in general happening in the codebase. At least it should go to occ_impl


return self.toTuple() == other.toTuple()
@dataclass
class AssemblyElement:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert to the original style of iteration

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding this I have a question: What parts of the API do you consider as public and which as internal?
Is the line between documented (https://cadquery.readthedocs.io/en/latest/apireference.html) and undocumented functions? Or between functions without and with leading underscores? Or something else?

I can revert this, but I still think, the usability of a dataclass is better than expanding a tuple of five entries where you must look into the source code to understand which item is which. With the dataclass, the field names in conjunction with the type system make clear what we receive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% if the question above is related to the request above. Methods/functions/attributes with _ are "private". The rest is not. Please in general minimize the scope of PRs.

@@ -537,29 +537,6 @@ def solve_result_check(solve_result: dict) -> bool:
return all(checks)


def test_color():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need to remove a test, something is really wrong with the PR. I think you made to many backward incompatible changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was not removed, just moved here:

def test_occt_conversion(self):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert the removal.

@@ -1106,7 +1083,7 @@ def check_nodes(doc, expected):
(["box0", "box0_part"], {"color_shape": (1.0, 0.0, 0.0, 1.0)}),
(
["box1", "box1_part"],
{"color_shape": cq.Color().toTuple()},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not change the API without a clear need.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will bring back toTuple, my suggestion is, to keep rgb() and rgba() as toTuple() does not convey what kind of values we get. Is it RGB, RGBA, HSV?
toTuple() would then be an alias for rgba() that maybe could be marked for deprecation if a major revision approaches (or kept, if too much depends on it).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, the preference is to not change existing methods. No problem with rgba/rgb

@adrianschneider94
Copy link
Author

I see that this branch has conflicts now that I must have missed last week. Should it be rebased?

Rebase is done.

@adrianschneider94
Copy link
Author

Hey, thanks for all the hard work, but this requires some serious rework with backward compatibility in mind. May I as why there are so many changes that are not needed?

I think there are only two changes that affect backwards compatibility: The missing toTuple() method and the changed signatures of some export methods. Or am I missing something?

The only changes not needed (in my eyes) are the assembly iteration and unifying the color handling. I think both make sense in terms of readability/clarity and as they appear in all places where materials need to be handled, it seemed obvious to me to handle it.

As a suggestion, I will revert the iteration and widen the signatures of all places where colors can be passed, so it's entirely backwards compatible at all those places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants