-
Notifications
You must be signed in to change notification settings - Fork 311
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
[SofaPython] ADD: Bindings for BoundingBox #736
[SofaPython] ADD: Bindings for BoundingBox #736
Conversation
…n BaseObject with example scene
@@ -270,6 +276,7 @@ SP_CLASS_METHOD_DOC(BaseObject, getCategories, | |||
SP_CLASS_METHOD_DOC(BaseObject, getTarget, | |||
"Returns the target (plugin) that contains the current object.") | |||
SP_CLASS_METHOD(BaseObject,getAsACreateObjectParameter) | |||
SP_CLASS_METHOD(BaseObject,computeBBox) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to use SP_CLASS_METHOD_DOC() to add a docstring so we can query the object from interactive python environemnt (like Ipython notebook) and we can generate the doc out of code.
import math | ||
import numpy as np | ||
|
||
class CreateGrid(Sofa.PythonScriptController): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I don't understand why you use a PythonScriptController with embedded arg parsing to create a Scene.
Cannot you do something like the following ?
def createGrid(parentNode, nx, ny, nz, mesh, name):
"""
create a grid...blahblah....
""""
## Add some checking on the values eg:
m = parentNode.createObject('MeshObjLoader', name="loader", filename=mesh)
model = parentNode.createObject('OglModel', name="model", src=m.getPathName())
model.init()
model.computeBBox()
root.createObject('RegularGrid', name="grid", nx=nx, ny=ny, nz=nz,
min=model.bbox.minBBox, max=model.bbox.maxBBox)
root.createObject('MechanicalObject', name="DOFs")
## ....
def createScene(root)!
try :
sys.argv[0]
except :
sys.argv = sys.argv[1].split()
else:
sys.argv = ['-h']
parser = argparse.ArgumentParser(
description='Loads a 3D model a generates a regular grid using its bounding box')
parser.add_argument(
'-x','--gridx', dest='nx', type=int, default=self.nx, help='')
parser.add_argument(
'-y','--gridy', dest='ny', type=int, default=self.ny, help='')
parser.add_argument(
'-z', '--gridz', dest='nz', type=int, default=self.nz, help='')
parser.add_argument(
'-m', '--mesh', dest='mesh', type=str, default=self.mesh, help='')
parser.add_argument(
'-o', '--output', dest='name', type=str, default="", help='')
args = parser.parse_args(argv)
createGrid(root, nx, ny, nz, mesh, name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review!
Sure thing, no need for a PythonScriptController here.. I initially copy-pasted the relevant code from a scene of mine that needs a script controller. I'll change it to what you say.
topoExporter.createObject('TetrahedronSetTopologyModifier') | ||
topoExporter.createObject('TetrahedronSetTopologyAlgorithms', template="Vec3d") | ||
topoExporter.createObject('TetrahedronSetGeometryAlgorithms', template="Vec3d", drawTetrahedra="0") | ||
topoExporter.createObject('Hexa2TetraTopologicalMapping', name="default28", input="@../grid", output="@Container") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is element of style but I recommend the systematic use of grid.getPathName() instead of hard coded link "@../grid" into string because this make the scene component relocatable in much easier way.
|
||
SP_CLASS_ATTR_SET(BoundingBox, minBBox)(PyObject */*self*/, PyObject * /*args*/, void*) | ||
{ | ||
SP_MESSAGE_ERROR("BoundingBox attributes are read-only") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is -1 for ? Is the the standard way to return a problem in a setter ?
If this is the case I think there is no need to send a message in addition to PyErro_BadArgument.
And if you want more details in the exception risen then it is best to use PyErr_SetString to set the message associated with the exxception.
In case the exception is not caught by python the message will be finally be printed by sofa using the sofa msg api.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got inspiration from DataFileName, that returns -1.
I will adjust
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if it is the standard way or the best way to do it, but that's how it is in every SP_CLASS_ATTR_SET methods that I found in Sofa.. :/
Hello Bruno, Thanks for this PR, it is good to see more binding. I dropped some comments on it. It is more style issue than fundamental ones. |
…ng methods, clearer example test scene..)
Reviews taken into account in last commit. Tell me if you have any other comments I should take into account. |
Looks good to me! |
[ci-build][with-scene-tests] |
This PR contains the code required to retrieve a component's bounding box in Python, and to manually call for a BaseObject's computeBBox method.
This was necessary in my case to create a RegularGrid from a mesh, since contrary to the sparseGrid, the regularGrid does not look at the node's MechanicalObject to create the grid.
An example scene in this PR shows my specific use case.
Concerning the bindings, I made the BoundingBox's minBBox and maxBBox read only since this should be computed by the component, and not set externally IMHO.
Could be useful to bind the different methods of BoundingBox too, but this is not covered in this PR
This PR:
Reviewers will merge only if all these checks are true.