CloudCompare-PythonRuntime
CloudCompare-PythonRuntime copied to clipboard
Assign RGB color to polyline, point, mesh
hello Thomas,
inside CC 2.13.2, your example to assign color to polyline won’t work....
import pycc
import cccorelib
import numpy as np
CC = pycc.GetInstance()
VERTICES = np.array([
[-0.5, -0.5, 0],
[1, 1, 0],
[2, 2, 0]
])
vertices = pycc.ccPointCloud(VERTICES[:, 0], VERTICES[:, 1], VERTICES[:, 2])
polyline = pycc.ccPolyline(vertices)
polyline.setColor(pycc.Rgb(255, 0, 0)) # set the color to red
polyline.showColors(True)
polyline.setClosed(False)
# This is important, otherwise the polyline would have a size of 0
polyline.addPointIndex(0, 3)
CC.addToDB(polyline)
CC.updateUI()
polyline.getDisplay().display3DLabel("Hello, world", cccorelib.CCVector3(1, 1,0))
this is the error:
AttributeError: module 'pycc_runtime' has no attribute 'Rgb'
At:
<string>(14): <module>
Is possibile to correct the code ? How to assign color to point and mesh ?
thanks Mario
Yes that is normal because the python runtime that is within CloudCompare 2.13 is old and does not have these functions
verified with 2.14 built 29/9/2024 the error persist
AttributeError: module 'pycc_runtime' has no attribute 'Rgb'
At:
Error
Hum that is because the version of the python runtime that is is CC's installer is also too old
Maybe you could try using the installer that is linked in the readme
there is something like this
import sys print(sys.version) import platform print(platform.python_version())
to check the python runtime version ?
I need to be able to assign RGB color to points, polyline and mesh, possibly I will open a request to daniel on CC forum for a make a new build with updated python runtime environment....
No there is no way to check the version of the CloudCompare python bindings
unfortunately not even in the latest version of CC (2.14.alpha 10/14/2024) the script for the color wont works, how can I solve the problem ? What can I ask Daniel to get the correct version of CloudCompare-PythonRuntime implemented ? Can I ask you if you are sure that the part of your code on this aspect is correct can you verify it ?
Thanks
I'll verify
Unfortunately, it does not work even in version 2.14 of 04/11/2024...
Yes because it seems its still not using the latest version of the runtime
What you can do is download the runtime installer from here https://u.pcloud.link/publink/show?code=XZlNs00Z5koREF3zkzSSFRmBs9tQUFj5Cq97 and use it to install the latest version (you may have to first run it to uninstall the current version first then re-run it to install)
Is the latest version of the runtime compatible with the latest 'sf_double' branch? I'm mostly waiting for this plugin to work with this branch to merge and update everything...
I'll do it this week end
I have this double-sf it compiles and works, but it needs a bit more work to be more robust and also some tests
Thanks. Can I still issue a new 'alpha' release or should I wait a little bit more?
Should wait that I confirm there is no trivial bug
I just pushed some small fixes, you can try to have a new alpha
Perfect
Ok, it's now online!
Many thanks, now your code of the color of the poly work correctly !!
Unfortunately, however, part of the code that handles calculations between scalars no longer works at the same time, here is the error and the code that worked in the previous version (CC 2024.11.04)
import pycc import numpy as np cc = pycc.GetInstance() entities = cc.getSelectedEntities() if not entities or len(entities) > 1: raise RuntimeError("Please select one point cloud") point_cloud = entities[0] if not isinstance(point_cloud, pycc.ccPointCloud): raise RuntimeError("Selected entity should be a point cloud") num_scalar_fields = point_cloud.getNumberOfScalarFields() print(f"Number of scalar fields: {num_scalar_fields}") if num_scalar_fields < 2: raise RuntimeError("The point cloud must have at least two scalar fields to compute 'C'.") scalar_field_1 = point_cloud.getScalarField(0).asArray() scalar_field_2 = point_cloud.getScalarField(1).asArray() scalar_field_C = scalar_field_1 + scalar_field_2 idx_C = point_cloud.addScalarField("C") scalar_C_array = point_cloud.getScalarField(idx_C).asArray() scalar_C_array[:] = scalar_field_C.astype(np.float32)[:] point_cloud.getScalarField(idx_C).computeMinAndMax() cc.addToDB(point_cloud, autoExpandDBTree=False) print(f"Nuovo campo scalare '{point_cloud.getScalarFieldName(idx_C)}' aggiunto e database aggiornato.")
error:
TypeError: unsupported operand type(s) for +: 'cccorelib.ScalarFieldView' and 'cccorelib.ScalarFieldView'
At:
this is working only for creating cloud+2 scalar
import numpy as np import pycc radius = 10 center = np.array([4, 7, -5]) num_points = 1000000 x = np.random.uniform(center[0] - radius, center[0] + radius, num_points) y = np.random.uniform(center[1] - radius, center[1] + radius, num_points) z = np.random.uniform(center[2] - radius, center[2] + radius, num_points) distances = np.sqrt((x - center[0])**2 + (y - center[1])**2 + (z - center[2])**2) valid_indices = np.where(distances < radius) x_valid = x[valid_indices] y_valid = y[valid_indices] z_valid = z[valid_indices] point_cloud = pycc.ccPointCloud(x_valid, y_valid, z_valid) point_cloud.setName("random") idx_A = point_cloud.addScalarField("A") A_array = point_cloud.getScalarField(idx_A).asArray() A_array[:] = np.random.uniform(10, 11, len(x_valid)) point_cloud.getScalarField(idx_A).computeMinAndMax() idx_B = point_cloud.addScalarField("B") B_array = point_cloud.getScalarField(idx_B).asArray() B_array[:] = np.random.uniform(100, 110, len(x_valid)) point_cloud.getScalarField(idx_B).computeMinAndMax() pycc.GetInstance().addToDB(point_cloud)
Yes,for now you should instead use toArray(), do some computations and set set the values back into the scalar field
array_field_1 = point_cloud.getScalarField(0).toArray()
array_field_2 = point_cloud.getScalarField(1).toArray()
// compations stay the same
point_cloud.getScalarField(idx_C)[:] = array_field_1 + array_field_2
thanks, now works !!!!!!!!!
It works! Just so I understand correctly, can I somehow assign a different colour for each individual point of the point cloud? Or can I just assign a single colour for the whole cloud? For example, can I randomly assign colours to the points? thanks !!!!
As is tradition here, this was only made possible a few hours ago, so you'll have to wait for when CloudCompare updates its runtime version
And example is available here
with the new build of CC 2024.12.26 works very good work Thanks !!!!