Hi! I’m very new to python and running into a probably simple error if anyone could help. I’m trying to make a script to duplicate a hatch and assign layers and a new hatch pattern for the duplicate. I’ve got the below worked out and it works great for a single hatch (ie I only hatch one square) For some reason if I hatch multiple objects the last command rs.hatchpattern does not work, everything else functions but the hatchpattern does not seem to run on multiple inputs.
Thanks! I’m probably missing something simple…
import rhinoscriptsyntax as rs
rs.Command("_hatch")
hatches = rs.LastCreatedObjects()
if hatches:
rs.ObjectLayer(hatches, “NC_DRAFTING::HATCH::Hatch”)
rs.CopyObject(hatches)
poche = rs.LastCreatedObjects()
rs.ObjectLayer(hatches, “NC_DRAFTING::HATCH::Poche-Light”)
rs.HatchPattern(poche, “Solid”)
this is the error I get:
Message: Parameter must be a Guid or string representing a Guid
Traceback:
line 890, in coerceguid, “C:\Users\Nick_Work\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\utility.py”
line 1062, in coercerhinoobject, “C:\Users\Nick_Work\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\utility.py”
line 237, in HatchPattern, “C:\Users\Nick_Work\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\hatch.py”
line 12, in , “C:\Users\Nick_Work\AppData\Local\Temp\TempScript.py”
First, when posting code, let Discourse apply proper code formatting by doing the following:
```python
<paste code here>
```
i.e. enclose the pasted code in 3 “backticks” above and below, you can add the python label if it is python code. So your original code looks like this:
Second, it’s probably erroring out because CopyObject() only takes one object and you have a list of objects so you would need CopyObjects()
Third, you can maybe look into using some rhinoscriptsyntax native methods:
import rhinoscriptsyntax as rs
crvs=rs.GetObjects("Select curves to hatch",rs.filter.curve,preselect=True)
if crvs:
hatches=rs.AddHatches(crvs)
if hatches:
rs.ObjectLayer(hatches, "NC_DRAFTING::HATCH::Hatch")
poche=rs.CopyObjects(hatches)
rs.ObjectLayer(poche, "NC_DRAFTING::HATCH::Poche-Light")
for hatch in poche:
rs.HatchPattern(hatch,"Solid")
(I didn’t do any error checking to see if the layers actually exist…)
I didn’t check your previous code very carefully, you also need to add a loop for rs.HatchPattern() as it only takes one hatch at a time (as I did in my last example).
import rhinoscriptsyntax as rs
rs.Command("_Hatch")
hatches = rs.LastCreatedObjects()
if hatches:
rs.ObjectLayer(hatches, "NC_DRAFTING::HATCH::Hatch")
poche=rs.CopyObjects(hatches)
rs.ObjectLayer(poche, "NC_DRAFTING::HATCH::Poche-Light")
for hatch in poche: rs.HatchPattern(hatch, "Solid")
Ah thank you! That did it, still new to this. This is what the full deal ended up being, seems to be working now with a bit of adjustment to the names.
Thanks for the help!
#double hatch - poche and hatch
import rhinoscriptsyntax as rs
if not rs.IsLayer("NC_DRAFTING::HATCH::Hatch"): rs.AddLayer("NC_DRAFTING::HATCH::Hatch")
if not rs.IsLayer("NC_DRAFTING::HATCH::Poche-Light"): rs.AddLayer("NC_DRAFTING::HATCH::Poche-Light")
rs.LayerColor("NC_DRAFTING::HATCH::Poche-Light", (190,190,190))
rs.Command("_hatch")
rs.EnableRedraw(False)
hatches = rs.LastCreatedObjects()
if hatches:
rs.ObjectLayer(hatches, "NC_DRAFTING::HATCH::Hatch")
poche = rs.CopyObjects(hatches)
rs.ObjectLayer(poche, "NC_DRAFTING::HATCH::Poche-Light")
for hatches in poche: rs.HatchPattern(hatches, "Solid")
rs.EnableRedraw(True)