任务
从Rhino导出作为obj文件的层。
The issue
在使用Python脚本时,我运行一个导出命令。而不是导出模型,而是在接口中显示一个对话框。如果我单击导出接口,它可以正常工作。但是,它会为每个层打开对话框。我有很多层,我想自动化整个导出过程。
最终目标是通过webGL通过three.js显示obj文件。
我是Python和Rhino的新手,但是我了解JavaScript和PHP,所以我非常了解这些概念,并且看了一些关于Python的教程,这样我就可以开始编写这个脚本了。
我已经尝试过的
我在用Rhino,Atom/Python。
import scriptcontext
import rhinoscriptsyntax as rs
from Rhino.Geometry import Point3d
def layerNames(sort=False):
rc = []
for layer in scriptcontext.doc.Layers:
if not layer.IsDeleted: rc.append(layer.FullPath)
if sort: rc.sort()
return rc
rs.EnableRedraw(False)
strPath = rs.DocumentPath()
strName = rs.DocumentName()
arrLayers = layerNames(False)
for layerName in arrLayers:
objs = scriptcontext.doc.Objects.FindByLayer(layerName)
rs.Command("_-Export "+layerName+".obj", False) Notes
我正在考虑使用python本机文件保存(打开(“layername.json”,"a")。其思想是从每个层中的对象中获取网格,将其转换为three.js json,然后使用它。但我不知道如何从一层中得到一个网格。我已经导入了Rhino.Geometry,看看它是否有用。我不知道如何找到要转换的网格,或者是否可以以自动化的方式使用本机导出命令,只需使用obj文件。
发布于 2015-04-07 15:41:06
这是最后一个输出dae、obj和stl的脚本。在适当的设置是相当积极的多边形缩减。调整角度和密度来改变这一点。
我发现,在将其设置为另一个值之前,需要将密度设置为零,这解决了网格转换中的多边形计数问题。
import os
import scriptcontext
import rhinoscriptsyntax as rs
print "//export run started/////////////"
# this function via mcneel/rhinoscriptsyntax
#https://github.com/mcneel/rhinoscriptsyntax/blob/master/Scripts/rhinoscript/layer.py
def layerNames(sort=False):
rc = []
for layer in scriptcontext.doc.Layers:
if not layer.IsDeleted: rc.append(layer.FullPath)
if sort: rc.sort()
return rc
def GetDAESettings():
e_str = ""
return e_str
def GetOBJSettings():
e_str = "_Geometry=_Mesh "
e_str+= "_EndOfLine=CRLF "
e_str+= "_ExportRhinoObjectNames=_ExportObjectsAsOBJGroups "
e_str+= "_ExportMeshTextureCoordinates=_Yes "
e_str+= "_ExportMeshVertexNormals=_No "
e_str+= "_CreateNGons=_No "
e_str+= "_ExportMaterialDefinitions=_No "
e_str+= "_YUp=_No "
e_str+= "_WrapLongLines=Yes "
e_str+= "_VertexWelding=_Welded "
e_str+= "_WritePrecision=4 "
e_str+= "_Enter "
e_str+= "_DetailedOptions "
e_str+= "_JaggedSeams=_No "
e_str+= "_PackTextures=_No "
e_str+= "_Refine=_Yes "
e_str+= "_SimplePlane=_No "
e_str+= "_AdvancedOptions "
e_str+= "_Angle=50 "
e_str+= "_AspectRatio=0 "
e_str+= "_Distance=0.0"
e_str+= "_Density=0 "
e_str+= "_Density=0.45 "
e_str+= "_Grid=0 "
e_str+= "_MaxEdgeLength=0 "
e_str+= "_MinEdgeLength=0.0001 "
e_str+= "_Enter _Enter"
return e_str
def GetSTLSettings():
eStr = "_ExportFileAs=_Binary "
eStr+= "_ExportUnfinishedObjects=_Yes "
eStr+= "_UseSimpleDialog=_No "
eStr+= "_UseSimpleParameters=_No "
eStr+= "_Enter _DetailedOptions "
eStr+= "_JaggedSeams=_No "
eStr+= "_PackTextures=_No "
eStr+= "_Refine=_Yes "
eStr+= "_SimplePlane=_No "
eStr+= "_AdvancedOptions "
eStr+= "_Angle=15 "
eStr+= "_AspectRatio=0 "
eStr+= "_Distance=0.01 "
eStr+= "_Grid=16 "
eStr+= "_MaxEdgeLength=0 "
eStr+= "_MinEdgeLength=0.0001 "
eStr+= "_Enter _Enter"
return eStr
settingsList = {
'GetDAESettings': GetDAESettings,
'GetOBJSettings': GetOBJSettings,
'GetSTLSettings': GetSTLSettings
}
fileName = rs.DocumentName()
filePath = rs.DocumentPath().rstrip(fileName)
arrLayers = layerNames(False)
def initExportByLayer(fileType="obj", visibleonly=False, byObject=False):
for layerName in arrLayers:
layer = scriptcontext.doc.Layers.FindByFullPath(layerName, True)
if layer >= 0:
layer = scriptcontext.doc.Layers[layer]
save = True;
if visibleonly:
if not layer.IsVisible:
save = False
if rs.IsLayerEmpty(layerName):
save = False
if save:
cutName = layerName.split("::")
cutName = cutName[len(cutName)-1]
objs = scriptcontext.doc.Objects.FindByLayer(cutName)
if len(objs) > 0:
if byObject:
i=0
for obj in objs:
i= i+1
saveObjectsToFile(cutName+"_"+str(i), [obj], fileType)
else:
saveObjectsToFile(cutName, objs, fileType)
def saveObjectsToFile(name, objs, fileType):
rs.EnableRedraw(False)
if len(objs) > 0:
settings = settingsList["Get"+fileType.upper()+"Settings"]()
rs.UnselectAllObjects()
for obj in objs:
obj.Select(True)
name = "".join(name.split(" "))
command = '-_Export "{}{}{}" {}'.format(filePath, name, "."+fileType.lower(), settings)
rs.Command(command, True)
rs.EnableRedraw(True)
initExportByLayer("obj",True, False)
initExportByLayer("dae",True, False)
initExportByLayer("stl",True, False)
print "//export run ended/////////////"发布于 2015-04-02 17:24:23
事实证明,这很简单。您需要提供完整的文件路径、名称和扩展名。这些对话框可以通过多个_Enter命令实现自动化。在此脚本中使用默认obj选项。但是,在调用各自的_Enter命令之前,可以将选项附加到_-导出命令。
import scriptcontext
import rhinoscriptsyntax as rs
def layerNames(sort=False):
rc = []
for layer in scriptcontext.doc.Layers:
if not layer.IsDeleted: rc.append(layer.FullPath)
if sort: rc.sort()
return rc
fileName = rs.DocumentName()
filePath = rs.DocumentPath().rstrip(fileName)
extension = ".obj"
arrLayers = layerNames(False)
for layerName in arrLayers:
objs = scriptcontext.doc.Objects.FindByLayer(layerName)
if len(objs) > 0:
rs.UnselectAllObjects()
for obj in objs:
obj.Select(True)
scriptcontext.doc.Views.Redraw()
rs.Command("_-Export "+filePath+layerName+extension+" _Enter _Enter")在我的说明中,我提到了使用mesh对象并通过Python编写自己的文件。这仍然是一个可行的选择,但该方法尚未在Mac上为Rhino实现。现在我将使用一个递归函数来查找嵌套层。完成后,我会把解决方案发到Gist上。
https://stackoverflow.com/questions/29401951
复制相似问题