Jump to content

Execute script without selecting object


Slumb

Recommended Posts

Hey,

Quick question how do I run a script on a object without having to select the object? Already tried FindObject("objectname"); but I dont think I used it right. Another question.
After the script selects the object I want it to 'Restore Selection' through a 'Edge Selection Tag'. Hope someone can help me.

Timo

 

Link to post

Why not make a tag into this object

#op = current so in a tag context op = the tag
op.GetObject() #return the object

 

What you really want to do because it's seem obscure. Basicly you want to have  a script (who run only when you press a button?) who act on the selected object and do => Restore Selection into edge selection tag?

Link to post
6 hours ago, graphos said:

Why not make a tag into this object


#op = current so in a tag context op = the tag
op.GetObject() #return the object

 

What you really want to do because it's seem obscure. Basicly you want to have  a script (who run only when you press a button?) who act on the selected object and do => Restore Selection into edge selection tag?

Yes, basically. After it has selected the object and restored the selection I want it to execute Subidivision Surface Weight thing that sets it automatically. But I already figured that out with the help of someone else.

 

Code:

CallCommand(16351); // Edges
CallCommand(12112); // Select All
CallCommand(1007573); // Weight Subdivision Surface
tool()#MDATA_SDSWEIGHT_MODE=0;
tool()#MDATA_SDSWEIGHT_STRENGTH=0.85;
CallButton(GetActiveDocument()->GetActiveTool(),MDATA_SDSWEIGTH_SETSTRENGTH);
CallCommand(13324); // Deselect All
CallCommand(200000083); // Live Selection

But the '//Select All' has to be replaced with the Edge Selection Tag. 

Link to post

Here a working example.

import c4d

def main():
    obj = doc.SearchObject("Plan")
    if not obj:
        return
    
    selection_name = "MySelection"
    selection_tag = None
    
    #Get the first segment selectionTag named by MySelection
    for tag in obj.GetTags():
        if tag.CheckType(5701) and tag.GetName() == selection_name:
            selection_tag = tag
            break
        
    if not selection_tag:
        return
        
    save_mode = doc.GetMode()
    save_tool = doc.GetAction()
    
    #Set To edge mdoe
    doc.SetMode(c4d.Medges)
    
    #Get selected edge
    bs = obj.GetEdgeS()
    bs_copy = bs.GetClone() #for retrieve it later
    
    bs.DeselectAll() #deselect all currently selected edge

    #Select all edge from tag
    c4d.CallButton(selection_tag, c4d.EDGESELECTIONTAG_COMMAND3)
    
    
    #Set the weight
    c4d.CallCommand(1007573)
    tool = c4d.plugins.FindPlugin(doc.GetAction(), c4d.PLUGINTYPE_TOOL)
    tool[c4d.MDATA_SDSWEIGHT_MODE] = 0
    tool[c4d.MDATA_SDSWEIGHT_STRENGTH] = 1
    c4d.CallButton(tool ,c4d.MDATA_SDSWEIGTH_SETSTRENGTH)
    
    #reset edge selection
    bs_copy.CopyTo(obj.GetEdgeS())
    
    #reset doc
    doc.SetAction(save_tool)
    doc.SetMode(save_mode)
    
    #trigger c4d update
    c4d.EventAdd()
    

if __name__=='__main__':
    main()

 

Link to post
1 hour ago, graphos said:

Here a working example.


import c4d

def main():
    obj = doc.SearchObject("Plan")
    if not obj:
        return
    
    selection_name = "MySelection"
    selection_tag = None
    
    #Get the first segment selectionTag named by MySelection
    for tag in obj.GetTags():
        if tag.CheckType(5701) and tag.GetName() == selection_name:
            selection_tag = tag
            break
        
    if not selection_tag:
        return
        
    save_mode = doc.GetMode()
    save_tool = doc.GetAction()
    
    #Set To edge mdoe
    doc.SetMode(c4d.Medges)
    
    #Get selected edge
    bs = obj.GetEdgeS()
    bs_copy = bs.GetClone() #for retrieve it later
    
    bs.DeselectAll() #deselect all currently selected edge

    #Select all edge from tag
    c4d.CallButton(selection_tag, c4d.EDGESELECTIONTAG_COMMAND3)
    
    
    #Set the weight
    c4d.CallCommand(1007573)
    tool = c4d.plugins.FindPlugin(doc.GetAction(), c4d.PLUGINTYPE_TOOL)
    tool[c4d.MDATA_SDSWEIGHT_MODE] = 0
    tool[c4d.MDATA_SDSWEIGHT_STRENGTH] = 1
    c4d.CallButton(tool ,c4d.MDATA_SDSWEIGTH_SETSTRENGTH)
    
    #reset edge selection
    bs_copy.CopyTo(obj.GetEdgeS())
    
    #reset doc
    doc.SetAction(save_tool)
    doc.SetMode(save_mode)
    
    #trigger c4d update
    c4d.EventAdd()
    

if __name__=='__main__':
    main()

 

wow thank you for the code!

Link to post
On 2017-5-10 at 5:40 PM, graphos said:

Here a working example.


import c4d

def main():
    obj = doc.SearchObject("Plan")
    if not obj:
        return
    
    selection_name = "MySelection"
    selection_tag = None
    
    #Get the first segment selectionTag named by MySelection
    for tag in obj.GetTags():
        if tag.CheckType(5701) and tag.GetName() == selection_name:
            selection_tag = tag
            break
        
    if not selection_tag:
        return
        
    save_mode = doc.GetMode()
    save_tool = doc.GetAction()
    
    #Set To edge mdoe
    doc.SetMode(c4d.Medges)
    
    #Get selected edge
    bs = obj.GetEdgeS()
    bs_copy = bs.GetClone() #for retrieve it later
    
    bs.DeselectAll() #deselect all currently selected edge

    #Select all edge from tag
    c4d.CallButton(selection_tag, c4d.EDGESELECTIONTAG_COMMAND3)
    
    
    #Set the weight
    c4d.CallCommand(1007573)
    tool = c4d.plugins.FindPlugin(doc.GetAction(), c4d.PLUGINTYPE_TOOL)
    tool[c4d.MDATA_SDSWEIGHT_MODE] = 0
    tool[c4d.MDATA_SDSWEIGHT_STRENGTH] = 1
    c4d.CallButton(tool ,c4d.MDATA_SDSWEIGTH_SETSTRENGTH)
    
    #reset edge selection
    bs_copy.CopyTo(obj.GetEdgeS())
    
    #reset doc
    doc.SetAction(save_tool)
    doc.SetMode(save_mode)
    
    #trigger c4d update
    c4d.EventAdd()
    

if __name__=='__main__':
    main()

 

Hey, thank you a lot for this peace of code! But only a small problem the code can't find the "Plan" object. It does work when I select the object, but thats the thing what I dont want. Any chance you can take a look what I did wrong? I did not change the code. Download: https://mega.nz/#!2ZZiBDpR!0N5zudhXY1UPnR1kCf9ohimTq8LJOC_lX6KXV8G7Slw

Link to post

A better explenation what I want to happen. In the video you can see that when I press the button 'Set' I want the object to sharp at some edges (these are just some random edges) without having to select the object. And when I press 'Sub' I want the 'Subdivision Surface Weight Tag' to be deleted. Also someone who knows how I can do this by using Expresso?

What I have to do:

 


What I want to happen:

 

Link to post

It's way more complicated than he seem to be.

And I doubt you will do it but here it's how I would build it if I have to do it.

In python you have to split thing since you can't use CallButton in a Tag, So you have to execute your code in the main thread:

  • Register a tag who will store in his basecontainer the actual state.
  • Register a MessageData plugin. Who will check all the scene for this tag and then act on the scene.

In C++

  • Since you have directly acces to the weightData you can do it in your tagData. But since it's not thread safe it might be crash (I never really tested)
Link to post

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...