obj file loaded as Scene object instead of Trimesh object - python

I'm trying to load a mesh from an .obj file (from ShapeNet dataset) using Trimesh, and then use the repair.fix_winding(mesh) function.
But when I load the mesh, via
trimesh.load('/path/to/file.obj') or trimesh.load_mesh('/path/to/file.obj'),
the object class returned is Scene, which is incompatible with repair.fix_winding(mesh), only Trimesh object are accepted.
How can I force it to load and return a Trimesh object or parse the Scene object to Trimesh object? Or any other way to fix winding of the triangles?
Using:
Python 3.6.5
Trimesh 3.0.14
MacOS 10.14.5

With the current trimesh version (3.9.42), you need to use trimesh.load with force='mesh':
trimesh.load('/path/to/file.obj', force='mesh')
https://trimsh.org/trimesh.html?highlight=load#trimesh.load

I have the same problem. You can access the "geometry" member of the scene, but I have no idea how the original mesh is split into multiple ones. Following.

The load_mesh has an option to be passed as aa Trimesh constructor
as explained in the documentation.
https://trimsh.org/trimesh.html?highlight=load_mesh#trimesh.load_mesh
here is an example code
mesh= t.load_mesh('./test.stl',process=False)
mesh.is_watertight
the second line ".is_watertight" is an attribute of Trimesh so you will get True if it is successfully imported as Trimesh.

Related

Loading a Mesh in OpenCV Python with viz module

I am creating an AR-app, where you can virtually try on shoes. For now I have the location of the foot calculated using SolvePnp. Now I want to render the 3D object (.obj) file on top of the image, using the OpenCv Viz module. (Manually compiling worked)
I want to load the object as an cv::viz::Mesh object using the cv::viz::Mesh::load function, but I don't find the corresponding Python function call.
I tried
mesh = cv2.viz_Mesh()
mesh.load('shoe.obj', cv2.viz_Mesh_LOAD_OBJ)
and also
mesh = cv2.viz_Mesh_load('shoe.obj', cv2.viz_Mesh_LOAD_OBJ)
I always get the error:
AttributeError: 'cv2.viz_Mesh' object has no attribute 'load'
Using the dir() function from python I found all other methods except the load method, which is a static method (that's probably why it isn't here).
>>> dir(cv2) #contains the Viz Classes
['', ... , 'viz',... ,'viz_Mesh]
>>> dir(cv2.viz) #Stuff like Enums , Colors etc
['',...'Color_amethyst', ..., 'MOUSE_EVENT_MOUSE_DBL_CLICK'...]
>>> dir(cv2.viz_Mesh) #pyhton functions and other non static methods
['__class__', ... , 'colors','polygons']
Does someone know what the correct python call is?
Appendix: the cv2.viz_Mesh_LOAD_OBJ and cv2.viz_MESH_LOAD_OBJ represent only integers 2 ( = enums), thus
>>>print(cv2.viz_MESH_LOAD_OBJ)
>>>print(cv2.viz_Mesh_LOAD_OBJ)
2
2
I found the solution. I overlooked some methods in the dir(cv2.viz).
The correct python call would be
mesh = cv2.viz.readMesh('./shoe.ply')
It only accepts the path to .ply file, but not to .obj files (Version 4.5.5). Maybe other formats will be added in newer versions

Using file format constants when saving PowerPoint presentation with comtypes

How do I access the constants available as file formats when saving a Powerpoint presentation through comtypes?
In the following example 32 is used as the format but I would like to use the constants listed here) or at least find a documented list with value for each constant.
For Word there is this list that also contains the value for each constant.
import comtypes.client
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, 32)
You can access all enum names associated with the COM object you loaded via the comtypes.client.Constants() class; pass it the PowerPoint.Application COM object you created:
from comtypes.client import Constants, CreateObject
powerpoint = CreateObject("Powerpoint.Application")
pp_constants = Constants(powerpoint)
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, pp_constants.ppSaveAsPDF)
The Constants instance loads the underlying Type Library and dynamically translates attribute lookups to typelib access. It is not included in the comtypes documentation for some obscure reason, even though it was added nearly 10 years ago now.
Another option is to access the attributes on a generated module in the generated type library, as shown in the Properties with arguments (named properties) section. This would give you access to any of the constants associated with the Powerpoint IDL, including auto completion support IDEs (once generated by accessing the PowerPoint.Application object the first time around).
The module is generated automatically when you use CreateObject() if type information is exposed on the object that's being created; this is definitely the case for 'Powerpoint.Application' as you don't set an interface explicitly. Automatic interface selection only works if there is type information available.
Enumeration names are added to the generated module at the top level, so directly use those:
import comtypes.client
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
# only import the generated namespace after the com object has been created
# at least once. The generated module is cached for future runs.
from comtypes.gen import PowerPoint
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, PowerPoint.ppSaveAsPDF)
The short name of the type library can be found in a VBA Object Browser; the screenshot in Steve Rindsberg's answer shows that for the PpSaveAsFileType enum that's PowerPoint. I believe the same name is also used in the documentation for the ppSaveAsFileType enum; note the (PowerPoint) addition to the documentation title.
You can also use the GUID of the type library, plus version number, but that doesn't quite roll of the keyboard if you have to type that by hand.
You can use from comtypes.gen import PowerPoint; help(PowerPoint) to see what names have been defined if you need a reminder, or just reference the Microsoft documentation.
Either method avoids having to use magic numbers; the type library definition itself gives you the symbolic names.
If you find any code examples using win32com instead, then any use of win32com.client.constants attributes translates directly to comtypes.client.Constant(...) or comtypes.gen.<module> attributes.
I don't have access to a Windows setup to actually test any of this, I'm deducing information from reading documentation and the source code of comtypes.
Here is the list from Microsoft which contains the values of each constant:
https://learn.microsoft.com/en-us/office/vba/api/powerpoint.ppsaveasfiletype
Assuming you have a copy of PowerPoint, start it, press ALT+F11 to open the VBA editor, press F2 to open the Object Browser then search on SaveAs to get this list. Click on any constant name to see the value of the constant at the bottom of the dialog.

Checking arguments of a function in ipython

I'm using the jupyter ipython notebook by anaconda.
IS there a quick way of looking at the arguments of a function like we do in RStudio? For e.g. ?merge displays documentation for merge in the lower right window of RStudio.
I was specifically looking for arguments of matplotlib.figure() which I found here but this is time consuming: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure
Found this post related to the question: Getting list of parameter names inside python function
but not sure if it is the same question.
You can try help(matplotlib.figure) after importing matplotlib
Type matplotlib.figure? at the command prompt, and it'll give you the signature and documentation:
In [1]: import matplotlib
In [2]: matplotlib.figure?
Type: module
String form: <module 'matplotlib.figure' from '~/venv/lib/python2.7/site-packages/matplotlib/figure.pyc'>
File: ~/venv/lib/python2.7/site-packages/matplotlib/figure.py
Docstring:
The figure module provides the top-level
:class:`~matplotlib.artist.Artist`, the :class:`Figure`, which
contains all the plot elements. The following classes are defined
:class:`SubplotParams`
control the default spacing of the subplots
:class:`Figure`
top level container for all plot elements
From the IPython introduction:
Exploring your objects
Typing object_name? will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes. To get specific information on an object, you can use the magic commands %pdoc, %pdef, %psource and %pfile
You can also use the standard Python help() function; the output is a little more verbose and not coloured, like the ipython object? command however.

Python - Sift Class SIFT.compute

I am trying to retrieve descriptors for key points that I have already found. I am using the following lines of code:
sift = cv2.SIFT()
self.features,des = sift.compute(self.gray,self.features)
However, I am receiving the error:
'cv2.SIFT' object has no attribute 'compute'
I don't understand why, because according to this link (http://docs.opencv.org/trunk/modules/nonfree/doc/feature_detection.html), this should be possible.
Would anyone know what is going on here?
I also noticed that:
cv2.SIFT.detectAndCompute(image, mask[, descriptors[, useProvidedKeypoints]]) → keypoints, descriptors¶
allows you to use already detected keypoints... would someone be able to tell me how to input the arguments correctly for this function to work? I don't need to use the mask argument by the way.
Thank you for your help.
The link you just gave, is for OpenCV version 3, not for version 2, that you are using. Thus, any differences in functions or library structure is adequately explained as you are not using the same version (sift = cv2.SIFT()).

How to create a mesh object in ITK using Python bindings?

I am trying to create a Mesh object in Python. I am using the python bindings which are being installed from the following web page. As far as the c++ code is concerned we can do it as follows
MeshType::Pointer mesh = MeshType::New();
I am very new to use even ITK. Have no idea how to create it. In the constructor of Mesh in the c++ documentation, it says one necessary argument which is the TPixelType. Unable to locate that as well.
Could anybody help me please with this.
Thanks
If I were you I would take a look at the Python bindings that come with ITK 4.0. You can get access to them by turning on the option WRAP_ITK_PYTHON in cmake.
Once, you compile ITK with the python bindings turned on you can create 2 mesh types out of the box:
import itk
meshType2D = itk.Mesh.D2Q.New()
meshType3D = itk.Mesh.D3Q.New()
alternatively you can explicitly instantiate your classes as follows:
import itk
meshType2D = itk.Mesh[itk.D, 2, itk.QuadEdgeMeshTraits.D2BBFF]
meshType3D = itk.Mesh[itk.D, 3, itk.QuadEdgeMeshTraits.D3BBFF]
This will give you 2 and 3 dimensional meshes of double type pixel values with default mesh traits. As far as pixel types in ITK go, these amount to the basic C++ variables types: double, float, unsigned int, etc. These basic types are wrapped in python and can be found in the ITK namespace: itk.D, itk.F, itk.UI, etc.

Categories