ZBrushCentral

Delete subtool if possible, if Not continue?

Hello I’m trying to script a macro to delete a “Zsphere” (or any other subtool name I insert here) at the start of my script, if there is no Zsphere I want the script to continue its commands, ATM The macro works but it there no zsphere it breaks so that what im trying to fix please help =(

You can check a subtool’s name and then delete it if there’s a match. This code snippet shows the basic idea:

[VarSet,nameStr,“ZSphere”]//this is the name you’re looking for

[VarSet,subTname,[IGetTitle,Tool:ItemInfo]]//get the subtool name
//remove end period:
[VarSet,subTname,[StrExtract,subTname,0,[StrLength,subTname]-2]]
//check if the same length and same characters:
[If,([StrLength,nameStr] == [StrLength,subTname])&&([StrFind,nameStr,subTname]>-1),//if they’re the same
//OK,we have a match so delete (or whatever)
[IPress,Tool:SubTool:Delete]
]//end if
//other code continues here

This code only checks that the name is exactly the same, so you’d need to change it a bit if you wanted to find names with “ZSphere” in them, such as “ZSphere#1”, “ZSphere#2” etc. or if you wanted the case ignored so that “zsphere” and “ZSPHERE” were found.

Also, if you wanted to check if a subtool was of a particular type, e.g. if it was a zsphere irrespective of its name, then more code would be necessary.

HTH,

Thank you for the quick reply ill will go test this out! :smiley:

What do I need edit to make it work, The script wont ask to delete the subtool but does continue to complete the rest of the task…im sure its a small tweek somewere but cant seem to figure it out

Post the script and I’ll take a look.

//ZBRUSH MACRO - Recorded in ZBrush version 4.7
[VarSet,nameStr,“ZSphere”]//this is the name you’re looking for
[VarSet,subTname,[IGetTitle,Tool:ItemInfo]]//get the subtool name
//remove end period:
[VarSet,subTname,[StrExtract,subTname,0,[StrLength,subTname]-2]]
//check if the same length and same characters:
[If,([StrLength,nameStr] == [StrLength,subTname])&&([StrFind,nameStr,subTname]>-1),//if they’re the same
//OK,we have a match so delete (or whatever)
[IPress,Tool:SubTool:Delete]
]//end if
[IButton,???,“Reset Tool palette to remove all but the selected 3D tool”,
[IShowActions,0]
[IConfig,4.5]
//check if there’s a 3D tool in Edit mode
[If,[IsEnabled,Transform:Edit],
[If,[IGet,Transform:Edit],
[Exit]
]
,
[Exit]
]
//ask to save the tool
[VarSet,toolName,[GetActiveToolPath]]
[VarSet,toolName,[StrMerge,toolName,".ztl"]]
[VarSet,toolName,[FileNameAsk,“ZTool(.ztl)|.ztl||”,#toolname ,“Please Save File…”]]
//if there’s a valid file name
[If,[StrLength,toolName],
[VarSet,toolName,[StrMerge,[FileNameExtract,toolName,3],".ztl"]]
//get the transform values
[MemDelete,MC_ToolReset]
[MVarDef,MC_ToolReset,9,0]
[MTransformGet,MC_ToolReset,0]
//save the tool
[FileNameSetNext,toolName]
[IPress,Tool:Save As]
//save the material
[VarSet,tmpMat,[IGet,Material:Item Info]]
[IKeyPress,13,[IReset,3]]//reset tools
//clear the canvas and reload and redraw the tool
[IPress,Layer:Clear]
[FileNameSetNext,toolName]
[IPress,Tool:Load Tool]
[ISet,Material:Item Info,#tmpMat]
[IClick,1004,10,10,10,20]
//reset transform values
[MTransformSet,MC_ToolReset,0]
[IPress,Transform:Edit]
[IPress,Tool:SubTool:Append]
[IPress,PopUp:ZSphere]
[IPress,Tool:SubTool:ZSphere]
[IPress,Tool:Topology:Edit Topology]
]
]//end of macro

I’m trying to combine existing scripts with my own bits of code
I just want it to delete any Zshperes in Tool before saving then appending a fresh zsphere for edit Topo

OK, I’ve revised your code. This should work OK now. It will delete any zspheres in the subtool list, though the first time it will throw up the warning message which you need to click so it doesn’t reappear. There’s no way in zscript to stop that.

I’ve left in the code that resets all the tools. Did you intend that? What it does is delete all the custom tools in the Tool palette before loading back in the one you are working on. There’s no warning, so you should remember that’s what’s happening or you may suddenly find you’ve deleted something you wanted to keep. It’s easy enough to take out that bit if all you want is to save the ZTL you’re working on.

Thank you so much Marcus This works flawlessly!!! The reset was very intend, I notice that append zspheres would cause a 1 at the end of the name and break the macro, reseting it fix that, but having a tool name zsphere would then cause edit topo to not work so deleting old zshperes was a must before press the macro. But now thanks to you the macro is prefect thanks again<3