Author Topic: Would This Be Possible  (Read 2663 times)

0 Members and 1 Guest are viewing this topic.

Offline nicktrick92

  • Newbie
  • *
  • Posts: 7
Would This Be Possible
« on: June 07, 2016, 07:36:10 am »
READ AFTER STRIKETHROUGH

Hey there,

I am a software developer for my company and my job is to write some scripts for KeyShot that we will use over the course of the next year or so. I'm fluent in Python, but that's not my problem. I am unsure of the capabilities of KeyShot scripting. So could somebody let me know if this would be possible in KeyShot scripting?

1. Open a .prt file (I know this is possible)
2. Open and read a text document or get text from the name of the file (Possible in Python)
3. Show a dialog box that lets user choose main materials for each layer
4. Using the text received from each of the model files, designate a material for each piece of the model (Unsure if you can texture/add material to models with scripting)
5. Repeat for the next model

So basically, a company might have 5000 of the same looking models, but maybe some have a chrome plate as opposed to the generic aluminum plate. I would like a script that can take the material it needs (If the text file says "Plate: Chrome", then it will prepare a chrome material) and then add that material to the correct layer (The plate layer, which will be chosen from the list of layers in a dialog box at the start of the program). It would do this for all the parts of the model as well, but if one can be done, they all can. Now I could probably make this script, I just want to make sure it's possible to add materials to certain parts of the model based on layer.

Also, for the dialog box of KeyShot (The one that you can create in scripting), is there a combo box option to show all materials available and let the user select one of them?

Thanks,

- Nick


EDIT: I have found through reading the documentation more thoroughly that I am able to get a dialog box and able to have the user enter a material type. I can then have it put that material type on an object in the assembly (Ex. Bearing). Now my problem is that I would like to also have an option for which object to apply the material to (Ex. A text box called layer that you type the layer in to. Like "yha_head"). I have created the text box in the dialog, but when I run the following code, it doesn't seem to find the layer name even though I type it in exactly how it shows it in the KeyShot pane (I know what I'm doing works for static entering because if I replace the opts["layer"] with "yha_head" it puts the material on the right layer, but when I type yha_head in the box and run it, it can't find it). Am I missing something really simple? Code:

Code: [Select]
# AUTHOR -------------
# VERSION 0.0.1
# Write description of the script here, and put your code after these lines.
import os

values = [("material", lux.DIALOG_TEXT, "Material:", " "),
("layer", lux.DIALOG_TEXT, "Layer:", " ")]

opts = lux.getInputDialog(title = "Set Material",
                              desc = "Set material of the object!",
                              values = values,
                              id = "setMaterial.py.luxion")

if len(opts["material"]) == 0:
        raise Exception("You must put a valid material")

if len(opts["layer"]) == 0:
        raise Exception("You must put a valid layer")

root = lux.getSceneTree()

for node in root.find(name = opts["layer"]):
try:
node.setMaterial(opts["material"])
except:
node.setMaterial("Chrome Black")

That should get the material and find the layer, then put it on the layer, but the substitution of a variable string in place of just typing the string in doesn't seem to work... For example, this would work even though I entered "yha_head" for layer on the last one too:

Code: [Select]
# AUTHOR -------------
# VERSION 0.0.1
# Write description of the script here, and put your code after these lines.
import os

values = [("material", lux.DIALOG_TEXT, "Material:", " "),
("layer", lux.DIALOG_TEXT, "Layer:", " ")]

opts = lux.getInputDialog(title = "Set Material",
                              desc = "Set material of the object!",
                              values = values,
                              id = "setMaterial.py.luxion")

if len(opts["material"]) == 0:
        raise Exception("You must put a valid material")

if len(opts["layer"]) == 0:
        raise Exception("You must put a valid layer")

root = lux.getSceneTree()

for node in root.find(name = "yha_head"):
try:
node.setMaterial(opts["material"])
except:
node.setMaterial("Chrome Black")

Could someone let me know! Thanks!
« Last Edit: June 07, 2016, 11:10:19 am by nicktrick92 »

Offline TpwUK

  • KeyShot Beta
  • Hero Member
  • *****
  • Posts: 2910
Re: Would This Be Possible
« Reply #1 on: June 07, 2016, 11:25:44 am »
the root.find("name", types, material) as far as i know does not cover layers, but i may be wrong as not setting _Types searches all occurrences of "name"

Martin

Offline nicktrick92

  • Newbie
  • *
  • Posts: 7
Re: Would This Be Possible
« Reply #2 on: June 07, 2016, 01:10:51 pm »
Okay, so I got it working... I literally just retyped the whole thing and now it works the way I want it to (Not sure what the problem was).

In response to TpwUK, I don't know if layers was the term I was going for. I meant to say the different parts of the whole assembly. You can use root.find(name = "PART_NAME") or root.find(name = partName) apparently and they will both get the part given that name. In my case. "yha_head" was the top mounting bracket of an air cylinder, so when I passed "Brass Polished" and "yha_head" in the dialog box, it put the polished brass material on the bracket.

Thanks for the help,

-Nick

Offline TpwUK

  • KeyShot Beta
  • Hero Member
  • *****
  • Posts: 2910
Re: Would This Be Possible
« Reply #3 on: June 07, 2016, 01:25:31 pm »
Okay, so I got it working... I literally just retyped the whole thing and now it works the way I want it to (Not sure what the problem was).

In response to TpwUK, I don't know if layers was the term I was going for. I meant to say the different parts of the whole assembly. You can use root.find(name = "PART_NAME") or root.find(name = partName) apparently and they will both get the part given that name. In my case. "yha_head" was the top mounting bracket of an air cylinder, so when I passed "Brass Polished" and "yha_head" in the dialog box, it put the polished brass material on the bracket.

Thanks for the help,

-Nick

Excellent - glad it's working for you Nick. :)

Martin