I’m using rhino.compute to calculate a mesh.
How could I convert the 3dm decoded mesh to an STL file?
Currently, I can only save it as a 3dm:
import compute_rhino3d.Grasshopper as gh
import rhino3dm
output = gh.EvaluateDefinition(definition_path, trees)
mesh = output['values'][0]['InnerTree']['{0}'][0]["data"]
mesh = rhino3dm.CommonObject.Decode(json.loads(mesh))
doc = rhino3dm.File3dm()
doc.Objects.AddMesh(mesh)
doc.Write("model.3dm", version=0)
Thank you very much!
You can use Rhino.RhinoDoc.WriteFile to write to all file types that rhino conventionally supports for exporting.
def ExportStl():
#Path to save File.
filepath = r"C:\Temp\TestExport.stl"
#Create write options to specify file info.
write_options = Rhino.FileIO.FileWriteOptions()
#Export all geometry, not just selected geometry.
write_options.WriteSelectedObjectsOnly = False
#Write File.
result = Rhino.RhinoDoc.ActiveDoc.WriteFile(filepath, write_options)
ExportStl()
In this case, I'm using the Rhino 'RunPythonScript' command with the open ActiveDoc but in your example, you could use doc.WriteFile(filepath, write_options) instead.
When you first run this, there is a .stl export dialogue that has export options. This window can be suppressed to the command line with write_options.SuppressDialogBoxes = True.
Or you can check the 'Always use these settings. Do not show this dialogue again.' option and it will not interrupt export in the future.
Your example suggests you may be working in a headless environment so I'm not sure how these dialogues would be handled in that scenario.
Related
Good afternoon,
I am trying to use the filter "Plot Over Line" of Paraview in a Python script. Basically, I want to:
Open the file ".vtu";
Use the filter PlotOverLine for the velocity;
Save the data in a ".csv" file.
On internet, I found a possible way to do this, but it gives error if ran with pvpython (even if using the word "simple" before the commands):
from paraview import simple
import csv
flow = GetActiveSource()
plotOverLine1 = PlotOverLine(Input=flow, Source='High Resolution Line Source')
passArrays1 = PassArrays(Input=plotOverLine1)
passArrays1.PointDataArrays = ['U']
plotOverLine1.Source.Point1 = [0, 0, 0]
plotOverLine1.Source.Point2 = [0, 0.4, 0]
writer = CreateWriter('data.csv')
writer.UpdatePipeline()
First, you may report here you errors.
As you suggest, your script cannot work as is, you should change the import to from paraview.simple import *.
Also, your writer does not have explicit input. I recommend to use CreateWriter(filename='path', input=myInput), or, for a one shot write, SaveData(filename='path', input=myInput).
Finally, one way to produce such scripts is to use Tools / Start Trace menu option (with the default config). Then perform actions in the interface. Finally Tools / Stop Trace give you the python script corresponding to your actions.
In photoshop and using python, I cannot save the active document as PSB (Large Document Format) file
With win32com.client, I can save active documents as .psd files like this:
from win32com.client import Dispatch
psApp = Dispatch("Photoshop.Application")
activeDocument = psApp.Application.ActiveDocument
activeDocument.SaveAs("E:\\PSDCopy", PhotoshopSaveOptions, False)
Though I cannot force it to save as psb no matter what I tried.
I also could not find any clue in the VBScript documentation, now even a word about psb files.
Any help would be deeply appreciated.
Adobe created a terrible API for interfacing with Photoshop. Worse than that, the documentation is deprecated and doesn't include updates like PSB files, EXR files etc.
A good way to find out how to write code for that is to use the Photoshop ActionListener and hack your way around (doesn't always work but it gives you some good leads). You can read more about it here.
This should do what you are looking:
import comtypes.client as ct
app = ct.CreateObject('Photoshop.Application')
def save_as_psb(path):
""" Save the current Document as PSB with maximised compatibility
turned ON.
Args:
path (str): This is the filename of the output PSB
"""
desc19 = ct.CreateObject("Photoshop.ActionDescriptor")
desc20 = ct.CreateObject("Photoshop.ActionDescriptor")
desc20.putBoolean(app.StringIDToTypeID('maximizeCompatibility'), True)
desc19.putObject(
app.CharIDToTypeID('As '), app.CharIDToTypeID('Pht8'), desc20)
desc19.putPath(app.CharIDToTypeID('In '), path)
logging.debug(path)
desc19.putBoolean(app.CharIDToTypeID('LwCs'), True)
app.executeAction(app.CharIDToTypeID('save'), desc19, 3)
From stackoverflow I learned how to set image properties in LibreOffice Writer with pyhton macros, via com.sun.star.text.WrapTextMode. Now I use that to set the text wrap to THOUGHT. Now I would like to set the image to background, like a watermark.
In LibreOffice Writer interactively I select an image, right-click on it and the context menu contains the "Wrap" commands, one is "Wrap Through" and the other one is "In Background".
In the python macro I have the following code (from Insert several images at once using macro scripting in LibreOffice and from the often quoted Andrew Pitonyak):
from com.sun.star.text.WrapTextMode import THROUGHT
and then to insert the image:
img = doc.createInstance('com.sun.star.text.TextGraphicObject')
element_url = 'file://' + file_name
img.GraphicURL = element_url
img.Surround = THROUGHT
text.insertTextContent(cursor, img, False)
So what is the code to put it "In Background"?
MRI shows that setting "In Background" causes the Opaque attribute to be false. So add this to the code:
img.Opaque = False
By the way, Surround is deprecated. Try setting TextWrap to THROUGHT instead.
I want to programmatically (using Python) split a multi-page tiff into single pages using Adobe Acrobat's exposed COM Objects.
I am writing this in order to answer my own question in order to put a viable answer out there, as I did not find anyone doing this on SO or any other forum.
Please, let me know what you think about my solution and feel free to leave your way of doing this.
Here is one way:
from win32com.client import Dispatch
def acrobat_split(f_path,f_name,f_ext):
# Connect to Adobe Acrobat.
avDoc = Dispatch("AcroExch.AVDoc")
# Open the input file (as a pdf).
src = f_path+'\\'+f_name+f_ext
avDoc.Open(src,src)
pdDoc = avDoc.GetPDDoc()
page_ct = pdDoc.GetNumPages()
# Set dst.
dst = f_path+'\\'+f_name+PAGE_DIV+".tif"
jsObject = pdDoc.getJSObject()
#Here you can save as many other types by using, for instance: "com.adobe.acrobat.xml"
jsObject.saveAs(dst,"com.adobe.acrobat.tiff")
pdDoc.Close()
del pdDoc
I would like to open a PDF in Photoshop using Python. I know how to open photoshop (.psd) files using python, but I am wondering if there is a way to specify the program used to open a file.
So far, all I do to open a photoshop document is:
psd = "path\to\photoshop\document"
os.startfile(psd)
but when I use os.startfile on a PDF it opens with Adobe Acrobat. I'd like to open the PDF in photoshop instead. Any ideas?
from comtypes.client import GetActiveObject
# Start up Photoshop application
# app = Dispatch('Photoshop.Application')
# Or get Reference to already running Photoshop application instance
app = GetActiveObject("Photoshop.Application")
fileName = "C:\Git\PS_Samples_Files\MyPDFFile.pdf"
docRef = app.Open(fileName)
More examples at https://github.com/lohriialo/photoshop-scripting-python
os.startfile just starts the specified file with its default application. Changing the default application for PDFs to photoshop would get the result you want, but at the cost of making opening PDFs in other circumstances really annoying.
To do this properly you'd need to script it using photshop's COM interface. I haven't tried that but this tutorial looks like if might fit your needs.
Photoshop has options in opening a PDF document, called Photoshop.PDFOpenOptions
You need win32com to dispatch the photoshop application. See sample code below
import win32com.client
import os, glob
folderin = r'D:\in'
if (__name__ == '__main__'):
psApp = win32com.client.Dispatch('Photoshop.Application')
for infile in glob.glob(os.path.join(folderin, '*.pdf')):
options = win32com.client.Dispatch('Photoshop.PDFOpenOptions')
options.CropPage = 0 # BoundingBox
options.Resolution = 300 # Pixels
options.Mode = 1 # Grayscale
options.BitsPerChannel = 8 # 8 bits per channel
options.AntiAlias = True
options.ConstrainProportions = True #Deprecated for Adobe Photoshop CS3
doc = psApp.Open(infile, options)
doc.flatten
doc.Trim(1)
doc.Close(2)
psApp.Quit()