Page MenuHome

Refactor Smart Project UV from Python to C
ClosedPublic

Authored by Andreas Terrius (andreasterrius) on Jul 16 2020, 3:25 PM.
Tags
None
Tokens
"Dat Boi" token, awarded by shader."Love" token, awarded by Tetone."Love" token, awarded by Limarest."Yellow Medal" token, awarded by mywa880."Love" token, awarded by demmordor."Love" token, awarded by AndreasResch."Love" token, awarded by dulrich."Love" token, awarded by ogierm.

Details

Summary

Ported the finding projection vector code to C.
Packing, Scaling the uv maps uses existing C code aswell.

Need your help to check whether I'm freeing memory properly, as I'm not too familiar with C

Ported the code directly from C to Python
Tested using a couple of meshes on "Release" build
a high poly model (286971 polys), old: hangs blender, new: 2.25 seconds
5 suzanne (2500 polys), old: 0.10 seconds, new: 0.013947 seconds

You can test it out by going to editmode -> Smart Uv Project C, I still kept the old one for comparison sake.

Potential backward breaking compatibillity:
After Smart Project Uv C is conducted, the UV selection before the operation is not retained. (The python operator retains the UV selections)

Suzzane unwrap comparison
old (Smart Projection time: 0.16):

new (Smart Projection time: 0.014):

Diff Detail

Repository
rB Blender
Branch
refactor-smart-uv (branched from master)
Build Status
Buildable 9040
Build 9040: arc lint + arc unit

Event Timeline

Andreas Terrius (andreasterrius) requested review of this revision.Jul 16 2020, 3:25 PM
Andreas Terrius (andreasterrius) created this revision.
Andreas Terrius (andreasterrius) edited the summary of this revision. (Show Details)
ogierm added a subscriber: ogierm.
Brecht Van Lommel (brecht) added inline comments.
source/blender/editors/uvedit/uvedit_unwrap_ops.c
1851

This is comparing pointers rather than area.

I fixed the pointer comparison but I still see some projection bug when I paid a closer attention to the unwrapped result

I'll fix it and report back when the bug has been ironed out

  • Fixed wrong area comparison function
  • Removed debug print, match default parameter to old one
  • Fix wrongly change default value

I think this can be reviewed again now, I've mostly fixed the bugs from the first diff.

Please notify me if I should just remove the old operator and replace the operator completely in the UI with the C operator, this shouldn't be too hard.

Dalai Felinto (dfelinto) added inline comments.
source/blender/editors/uvedit/uvedit_unwrap_ops.c
2064

Please check code style for comments:

/**
 * Smallest first, ...
 */

https://wiki.blender.org/wiki/Style_Guide/C_Cpp#Comments

If this is effectively replacing the other operator, you can just remove it. Unless it makes code review more complicated

Good start, most commends inline.

source/blender/editors/uvedit/uvedit_unwrap_ops.c
1848

There should be a comment explaining why this small threshold is needed.

1864

Use r_ prefix for return arguments. See https://wiki.blender.org/wiki/Style_Guide/C_Cpp#Naming

1882

Alloca should only be used for small allocations, especially for functions called often where malloc may be a bottleneck.

Use MEM_mallocN or MEM_callocN here.


Note, it seems this is only used for tagging, you can use BM_ELEM_TAG for this. See code that uses BM_mesh_elem_hflag_disable_all.

1884

Use f_index (instead of camel caps). Matching conventions for surrounding code. Same goes for other camel-caps naming in this function.

1885

Both --value and value-- used here without any reason for the difference. Prefer value-- unless the other order is needed.

1902

Prefer call this normal than vec, eg average_normal.

1929

Use !is_zero_v3(..) here.

1943

Naming here could be improved angle_diff_best, angle_diff_test (instead of temp).

2114–2118

axis_dominant_v3_to_m3 to create a matrix from a direction vector.

2168

Why is this needed?

2201–2208

Angles can be radians, the UI will show as degrees. See use of PROP_ANGLE.

Campbell Barton (campbellbarton) requested changes to this revision.Jul 23 2020, 2:42 PM
This revision now requires changes to proceed.Jul 23 2020, 2:42 PM
  • Replaced old UV smart project with the C one
  • Removed alloca, and used BM_ELEM_TAG instead
  • Various conformance to code style guidelines
  • Removed unneeded codes related to uv sync
  • Changed RNA_FLOAT for projection_angle to PROP_ANGLE
  • Reverted accidental comment change
  • Conform to code conventions

Committed with edits rB850234c1b10a828678f1b91001f2731db807f7e2

Made the following edits:

  • Only ignore small faces when sorting, instead of a small difference between faces since many small differences could make sorting unpredictable depending on the current order.
  • Pass the 3D view to BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs so local-view will be respected as this is normally accessed from the 3D view.
  • Use pointers for project_thick_faces instead of copying the ThickFace.
  • Use a memory arena for LinkList use to avoid many small allocations.
  • Don't change selection, allow operating on unselected UV's instead rB9482cc68652d: UV: support creating ParamHandle without checking UV selection
This revision is now accepted and ready to land.Jul 26 2020, 1:38 PM

While it's very nice that this is now much faster it would have been good not to remove the UV Smart project operator from object mode or at least document that you were doing this in the release notes, as any addon which calls UV smart project from object mode is now broken. If you're going to change the context in which an operator works in such a major way please mention this. Bug report submitted https://developer.blender.org/T83038