Skip to content

Commit

Permalink
[python] add meshScale parameter to mesh related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
YoheiKakiuchi authored and IRSL at Toyohashi Tech committed Dec 18, 2023
1 parent f5998d4 commit 2a78ad0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
10 changes: 5 additions & 5 deletions python/irsl_choreonoid/cnoid_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ def exportURDF(fname, body, **kwargs):
exec('ubw.set{}(v)'.format(k))
return ubw.writeBody(body, fname)

def exportBodyAsMesh(fname, input_body, scale=None, offset=None, useCollision=False, **kwargs):
def exportBodyAsMesh(fname, input_body, meshScale=None, offset=None, useCollision=False, **kwargs):
"""
Exporting a mesh file converted from Body
Exporting a mesh file converted from an instance of cnoid.Body.Body
Args:
fname (str) : Name of output file
input_body ( cnoid.Body.Body ) : Robot model to be exported
scale (float, optional) :
meshScale (float, optional) :
offset( cnoid.IRSLCoords.coordinates, optional) :
useCollision ( boolean, defalt=False) :
kwargs (dict) : Passing to irsl_choreonoid.makeshapes.exportMesh
Expand All @@ -202,9 +202,9 @@ def exportBodyAsMesh(fname, input_body, scale=None, offset=None, useCollision=Fa
base.addChild(trs)
if offset is not None:
base.setPosition(offset.cnoidPosition)
if scale is not None:
if meshScale is not None:
scl=cnoid.Util.SgScaleTransform()
scl.setScale(scale)
scl.setScale(meshScale)
scl.addChild(base)
base = scl
exportMesh(fname, base, **kwargs)
Expand Down
39 changes: 34 additions & 5 deletions python/irsl_choreonoid/make_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def extractDrawables(sg_node, currentCoords=None):
"""
return extractNode(sg_node, currentCoords=currentCoords, nodeTypes=[cutil.SgShape, cutil.SgPlot, cutil.SgText])

def loadScene(fname, fileUri=None, wrapped=True, rawShape=False, coords=None, **kwargs):
def loadScene(fname, meshScale=None, fileUri=None, wrapped=True, rawShape=False, coords=None, **kwargs):
"""Loading scene(wrl, scene, ...) file using cnoid.Util.SceneLoader
Args:
Expand Down Expand Up @@ -196,6 +196,11 @@ def loadScene(fname, fileUri=None, wrapped=True, rawShape=False, coords=None, **
if fileUri is not None:
sg.setUri(fname, fileUri)

if meshScale is not None:
scl_ = cutil.SgScaleTransform(meshScale)
scl_.addChild(sg)
sg = scl_

if rawShape:
return sg

Expand All @@ -214,7 +219,7 @@ def loadScene(fname, fileUri=None, wrapped=True, rawShape=False, coords=None, **
ret.setPosition(coords.cnoidPosition)
return ret

def loadMesh(fname, fileUri=None, wrapped=True, rawShape=False, coords=None, **kwargs):
def loadMesh(fname, meshScale=None, fileUri=None, wrapped=True, rawShape=False, coords=None, **kwargs):
"""Loading mesh file using cnoid.AssimpPlugin module
Args:
Expand Down Expand Up @@ -246,6 +251,11 @@ def loadMesh(fname, fileUri=None, wrapped=True, rawShape=False, coords=None, **k
for shape in shapes:
shape.setMaterial(mat)

if meshScale is not None:
scl_ = cutil.SgScaleTransform(meshScale)
scl_.addChild(sg)
sg = scl_

if rawShape:
return sg

Expand All @@ -260,7 +270,7 @@ def loadMesh(fname, fileUri=None, wrapped=True, rawShape=False, coords=None, **k
ret.setPosition(coords.cnoidPosition)
return ret

def __genShape(mesh, wrapped=True, rawShape=False, coords=None, texture=None, **kwargs):
def __genShape(mesh, meshScale=None, wrapped=True, rawShape=False, coords=None, texture=None, **kwargs):
sg = cutil.SgShape()
sg.setMesh(mesh)
if texture is not None:
Expand All @@ -270,6 +280,11 @@ def __genShape(mesh, wrapped=True, rawShape=False, coords=None, texture=None, **
if mat is not None:
sg.setMaterial(mat)

if meshScale is not None:
scl_ = cutil.SgScaleTransform(meshScale)
scl_.addChild(sg)
sg = scl_

if rawShape:
return sg

Expand Down Expand Up @@ -986,12 +1001,13 @@ def makeBoxFromBoundingBox(bbox, wrapped=True, rawShape=False, **kwargs):
##
## Function for exporting
##
def exportMesh(fname, sg_node, verbose=False, generatePrimitiveMesh=True, ignoreURDFPrimitive=False, outputType=None):
def exportMesh(fname, sg_node, meshScale=None, verbose=False, generatePrimitiveMesh=True, ignoreURDFPrimitive=False, outputType=None):
"""Exporting SgNode as a mesh file (using Assimp)
Args:
fname (str) : File name to be saved
sg_node ( cnoid.Util.SgNode ) : Root node of scene to be saved
meshScale (float, optional) :
verbose ( boolean, default=False ) :
generatePrimitiveMesh ( boolean, default=True ) :
ignoreURDFPrimitive (boolean, default=False ) :
Expand All @@ -1008,14 +1024,21 @@ def exportMesh(fname, sg_node, verbose=False, generatePrimitiveMesh=True, ignore
if outputType is not None:
wt.outputType = outputType

if meshScale is not None:
scl_=cnoid.Util.SgScaleTransform()
scl_.setScale(meshScale)
scl_.addChild(sg_node)
sg_node = scl_

return wt.writeScene(fname, sg_node)

def exportScene(fname, sg_node, exportMesh=False, **kwargs):
def exportScene(fname, sg_node, meshScale=None, exportMesh=False, **kwargs):
"""Exporting SgNode as .scen file
Args:
fname (str) : File name to be saved
sg_node ( cnoid.Util.SgNode ) : Root node of scene to be saved
meshScale (float, optional) :
exportMesh (boolean, default=False) : Exporting mesh instead of primitive type
kwargs ( dict[str, param] ) : Extra keyword arguments for using to execute ''StdSceneWriter.<keyword> = <value>''
Expand All @@ -1034,6 +1057,12 @@ def exportScene(fname, sg_node, exportMesh=False, **kwargs):
#sp.shape.mesh.translate(v)
sp.mesh.setMeshType()

if meshScale is not None:
scl_=cnoid.Util.SgScaleTransform()
scl_.setScale(meshScale)
scl_.addChild(sg_node)
sg_node = scl_

return wt.writeScene(fname, sg_node)

### util
Expand Down

0 comments on commit 2a78ad0

Please sign in to comment.