PDA

View Full Version : Random Particle/ Line Thickness Python script



Diepgroen
03-05-2017, 06:47 PM
In the 'Lightwavers are Everywhere' facebook group Mark Warner asked for a specific way to randomly change the Particle/ Line Thickness value for all selected objects. Values should have a specific range defined by the user.

136207 136204

It is specific to his motion needs but you never know who else could find this useful later on.

136205

Mark Warner says:1x 2point poly chain with a bit of displacement. Radial Arrayed in Layout. Select all chains and run Ludwig's script. Texture using the Instance Info/Fixed Random into a gradient (fortunately they don't have to be instances!). Took about 5 mins where it would otherwise have taken much MUCH longer.

Python File to use, edit or destroy. UI thanx to the cheat sheet @ justaddmilk.org and BAM it works.
136206




#! /usr/bin/env python
# -*- Mode: Python -*-
# -*- coding: ascii -*-

"""
Random value Particle/Line Thickness
- Select one or more objects
- Click the 'RndThick' Button
- change min and max values to your liking
- to cancel out press the 'Escape' button
- to process script, press continue
"""

__author__ = "LAG"
__date__ = "5 march 2017"
__copyright__ = ""
__version__ = ""
__maintainer__ = ""
__email__ = ""
__status__ = ""
__lwver__ = "2015.3"

# ------------------------------------------------------------------------------
# Import Modules
# ------------------------------------------------------------------------------
import lwsdk
import random

class random_thickness(lwsdk.IGeneric):
# Default min and max values to populate initial inputs when ui opens
thicknessMin = 1
thicknessMax = 1
#initialize plugin
def __init__(self, context):
super(random_thickness, self).__init__()
# Start process ----------------------------------
def process(self, ca):
ui = lwsdk.LWPanels()
panel = ui.create("Random Thickness")
#min value
self.float_min_edit = panel.float_ctl("Min Float")
self.float_min_edit.set_float(self.thicknessMin)
self.float_min_edit.set_event(self.float_min_callb ack)
#max value
self.float_max_edit = panel.float_ctl("Max Float")
self.float_max_edit.set_float(self.thicknessMax)
self.float_max_edit.set_event(self.float_max_callb ack)
# what to do when user cancels/ close ui panel
if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_RESIZE) == 0:
ui.destroy(panel)
return lwsdk.AFUNC_OK
# apply randomization to all selected objects
self.user_selection()
# close ui panel
ui.destroy(panel)
return lwsdk.AFUNC_OK
# when user inputs value store this value in thickenssMin
def float_min_callback(self, id, data):
self.thicknessMin = self.float_min_edit.get_float()
# when user inputs value store this value in thickenssMin
def float_max_callback(self, id, data):
self.thicknessMax = self.float_max_edit.get_float()
# function to store current user selection and object ID
def user_selection(self):
UserSelection = lwsdk.LWInterfaceInfo().selected_items()
UserSelectionID = []
for item in UserSelection:
UserSelectionID.append(lwsdk.itemid_to_str(item))
# loop through each selected object and apply the random thickness code
for i in range(0, len(UserSelection)):
lwsdk.command("SelectItem " + str(UserSelectionID[i]))
self.changeThickness()
# reselect all objects
for i in range(0, len(UserSelection)):
if i == 0:
lwsdk.command("SelectItem " + UserSelectionID[i])
else:
lwsdk.command("AddToSelection " + UserSelectionID[i])
# randomize between user input values min and max
def changeThickness(self):
newValue = random.uniform(self.thicknessMin, self.thicknessMax)
lwsdk.command("LineThickness " + str(newValue))
# I have no idea what this is for
# LWInstanceFuncs -------------------------------------
def inst_descln(self):
"You should never see this! If you do, run!"


ServerTagInfo = [
( "RndmThick", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "RndmThick", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = {lwsdk.GenericFactory("Random_Thicnkess", random_thickness): ServerTagInfo}

erikals
03-06-2017, 03:22 AM
Nnnnnice!   :king:

Diepgroen
03-06-2017, 11:51 AM
Thanks Erikals, nice feeling when I run an experimental code and it actually runs. :)