Start a new topic
Answered

Python Scripting for faster Workflow

Hi,


I started writing python script to speed up my workflow. 

I am missing the feature to isolate a selection or work through the parts and tweak the tesselation settings each.

i.e. I wrote couple of functions to mark objects as "done" via custom attribute and the show/hide/isolate them.


Two questions:


  1. How can I assign a shortcut or create an icon/menu entry for a custom script/function?
  2. How can one quickly hide or show ALL parts at the same time based on a custom attribute without looping through all elements? (its kinda slow with over 5000 parts to iterate through)
    What I currently do is:



  1. # returns True or False if a part has the custom attribute Done set to True or False
    def is_done(part):
    	props = core.listProperties(part)
    	if ("Done" in props):
    		return ((core.getProperty(part,"Done")).lower() == "true" )
    	else:
    		return False
    # show all part occurrences which have custom attribute "Done" set to "True"
    def show_all_done():
    	for o in scene.getPartOccurrences():
    		if is_done(o):
    			scene.show(o)
    		else:
    			scene.hide(o)
    	scene.clearSelection()
    	scene.select(get_done())
    
    show_all_done()

     



Best Answer

Starting 2019.1, a new function lets you evaluate expression on list of occurrences. This is extremely efficient:


def show_all_done():
  done = scene.findByProperty('Done', 'true')
  scene.evaluateExpressionOnOccurrences(done, 'SetProperty("Visible", "Inherited")')
  undone = list(set(scene.getPartOccurrences()) - set(done))
  scene.evaluateExpressionOnOccurrences(undone, 'SetProperty("Visible", "False")')
  scene.clearSelection()
  scene.select(done)



Hello Philipp,


Thanks for posting here! How do you like our API so far? We've been working a lot on simplifying it lately.


  1. In PiXYZ environment, any addition on the GUI has to be made threw the creation of a custom plugin. You can check this section of the documentation for more information on how to create one. You'll see that the XML creation part is not really user friendly so don't hesitate to keep posting on these forums if you encounter any issue.

  2. Unfortunately, there is no way for now to show/hide several occurrences at the same time. I'll check with the dev team if we can add the functionality in the next versions. Anyway, your code can still be optimized (I managed to drop the process time from 8 to 4 seconds on a 2000 parts scene).


def show_all_done():
  done = scene.findByProperty('Done', 'true')
  for o in scene.getPartOccurrences(): 
    if o in done: core.setProperty(o, 'Visible', 'Inherited')
    else: core.setProperty(o, 'Visible', 'False')
  scene.clearSelection()
  scene.select(done)



Note that the core.setProperty function is more low level (so more efficient) than the scene.hide/scene.show ones.


thanks for the quick response. I managed to create my own plugin and added my functions as buttons. very cool! 

if you're talking to the dev team, please also suggest a "quick isolate" toggle button/function, where you can quickly take selected objects and temporarily hide all others objects and once done return to previous state and restore the visibility like it was before entering isolation mode. this would help examin part by part without the need to slice, explode or cumbersome hide/unhide/show hidden actions.


also the "show Hidden" button from the UI should be accessible through the API.

so far I am happy with the changes in the API. it seems to get easier. but I just started to dig into it and I have no comparism how it was before. It just seems there are some overload functions missing to either work with one entity or a list of entities, which would make things easier.


a more complete documentation on the plugin section would be nice.

what are the parameters of a function?

 <function name="show_to_do" state="stable" scriptable="true" guiable="true" description="SHOW toDo" logName="" scriptFile="mld_utils.py"/>

what does the state, scriptable, guiable, logName parameters exactly mean?


thx

I'll transfer all your requests to the team!


About the function parameters, they are not up to date anymore and should be removed from the documentation, thanks for pointing that up ;)

Answer

Starting 2019.1, a new function lets you evaluate expression on list of occurrences. This is extremely efficient:


def show_all_done():
  done = scene.findByProperty('Done', 'true')
  scene.evaluateExpressionOnOccurrences(done, 'SetProperty("Visible", "Inherited")')
  undone = list(set(scene.getPartOccurrences()) - set(done))
  scene.evaluateExpressionOnOccurrences(undone, 'SetProperty("Visible", "False")')
  scene.clearSelection()
  scene.select(done)



1 person likes this

Thanks, this is amazing. Now it takes 0.009s instead of 3s!! I am very happy with your quick and nice support! 

Yes it is! This is a completely new scripting feature. We'll push it in the documentation as soon as possible. 

Enjoy!

Login or Signup to post a comment