bermu
October 26, 2018, 9:41am
#1
Hi all,
Could someone tell me how to make this python code shorter and more professionnal?
Thank you in advance…
import rhinoscriptsyntax as rs
import ghpythonlib.components as ghcomp
if brep == None and weightSetted == 0 :
metalWeight = 0
if weightSetted > 0 :
metalWeight = weightSetted
if brep != None:
metalWeight = ghcomp.Volume(brep)
volume = ghcomp.ListItem (metalWeight,[0],True)
metalWeight = (volume * density)/1000
metalWeight = weightSetted + metalWeight
print metalWeight
Perhaps:
if brep and weightSetted and density:
volume = brep.GetVolume()
if volume:
metalWeight = (volume * density)/1000
metalWeight+=weightSetted
print metalWeight
You don’t need to import the GH component “Volume”, RhinoCommon has Brep.GetVolume() which I assume does the same thing.
bermu
October 26, 2018, 12:32pm
#3
Hi Helvetosaur,
Thank’s a lot for your reply, i need some more like what if:
sometime no brep from rhino is set in brep and the component do not accept null
sometime the weight is set to 0
I code this, Is there a more elegant way?
if brep == None and weightSetted == 0 :
metalWeight = 0
if weightSetted > 0 :
metalWeight = weightSetted
if brep != None:
volume = brep.GetVolume()
metalWeight = (volume * density)/1000
metalWeight = weightSetted + metalWeight
print metalWeight