PDA

View Full Version : Python: Polygon tool and Attribut menager?


Shake0
11-26-2010, 02:46 PM
Hi All!
I'm really new to coding, .. like I'm use to rely to Xpresso even for a simple if statement...
However, it's a week I'm getting along with Python and it looks surprisingly easy to learn.
I write a script that randomly select poly/point on the model:

(this may not look like an object-oriented program )

import c4d
from c4d import gui
import random

# IDs
Gp_ID1,Gp_ID2=1000,1010
Button1=1001
Button2=1002
OFFSET=1003
STEP=1004
SEED=1015
PCENT=1016
DES1,DES2,DES3,DES4=1200,1201,1202,1203
ABS=1005

class Dialog(gui.GeDialog):

def CreateLayout(self):
self.SetTitle("Random Selection")
self.GroupBegin(Gp_ID1, c4d.BFH_SCALEFIT,5)
self.AddButton(Button1, c4d.BFH_SCALE,100,name="Sequence")
self.AddStaticText(DES1,c4d.BFH_RIGHT,50,name="Offset")
self.AddEditNumberArrows(OFFSET,c4d.BFH_SCALEFIT)
self.AddStaticText(DES2,c4d.BFH_RIGHT,50,name="Step")
self.AddEditNumberArrows(STEP,c4d.BFH_SCALEFIT)
self.GroupEnd()

self.AddSeparatorH(200)

self.GroupBegin(Gp_ID2, c4d.BFH_SCALEFIT,5)
self.AddButton(Button2, c4d.BFH_SCALE,100,name="Random")
self.AddStaticText(DES3,c4d.BFH_RIGHT,50,name="Seed")
self.AddEditNumberArrows(SEED,c4d.BFH_SCALEFIT)
self.AddStaticText(DES4,c4d.BFH_RIGHT,50,name="Selection%")
self.AddEditSlider(PCENT,c4d.BFH_SCALEFIT,0,0)
self.AddCheckbox(2054,c4d.BFH_CENTER,0,0,name="Polygon")
self.GroupEnd()

return True

def InitValues(self):
self.SetReal(STEP,1)
self.SetReal(SEED,2783)
return True

def Command(self,id,msg):
global ch
if id == Button1:
ch=1,int(self.GetReal(OFFSET)),int(self.GetReal(STEP)),self.GetBool(2054)
self.Close()
elif id == Button2:
ch=2,int(self.GetReal(SEED)),int(self.GetReal(PCENT)),self.GetBool(2054)
self.Close()

return True

def process(count):
if ch[0]==1: # SEQUENCE
pointOS=[]
for i in range(count):
if select.IsSelected(i) == True:
pointOS.append(i)
lengthOS = len(pointOS)
for i in range (ch[1],lengthOS,ch[2]):
select.Select(i)
elif ch[0]==2: # RANDOM
pointOS=[]
for i in range(count):
if select.IsSelected(i) == True:
pointOS.append(i)

pointNS=[]
lengthOS = len(pointOS)-1
lengthOS2 = (ch[2]*len(pointOS))/100
random.seed(ch[1])
while len(pointNS)<lengthOS2:
rdn_p = random.randint(0,(len(pointOS)-1))
pointNS.append(pointOS.pop(rdn_p))

select.DeselectAll()
for i in pointNS:
select.Select(i)

return select

def main():
global obj, select
obj = c4d.documents.GetActiveDocument().GetActiveObject()
if obj == None:
pass
else:
Dlg = Dialog()
Dlg.Open(c4d.DLG_TYPE_MODAL,defaultw=400)

if ch[3]==False:
select = obj.GetPointS()
count = len(obj.GetAllPoints())
process(count)
elif ch[3]==True:
select = obj.GetPolygonS()
count = len(obj.GetAllPolygons())
process(count)

main()

The user has to specify if the selection is on polygon or not.
Is there a way to detect if the document is on Polygon Tool mode or Point Tool mode?

Then, I would like to display the user area in the Attribute menage, instead of a pop-up dialog. But there is no class to access attributes manager :curious:

Scott Ayers
11-26-2010, 10:28 PM
Here is a method to get what mode you're in: import c4d
from c4d import gui

mode = 1; #toggle between 0&1 to switch modes

def main():
if mode is 0 and not c4d.CallCommand(12139): #If point mode is not active
c4d.CallCommand(12139) #Activate point mode

if mode is 1 and not c4d.CallCommand(12187): #If polygon mode is not active
c4d.CallCommand(12187) #Activate polygon mode

if __name__=='__main__':
main()

I don't know about the attribute question.
It sounds like you might want to make a tag plugin rather than a dialog based plugin.

-ScottA

donelgreeko
11-27-2010, 01:20 PM
Hi,

you can also return the mode by calling:


mode = doc.GetMode()
if mode==c4d.Medges: #in edge mode
pass
elif mode==c4d.Mpolygon: #in polygon mode
pass
elif mode==c4d.Mpoint: #in point mode
pass
elif mode==c4d.Maxis: #in axis mode
...

Shake0
11-27-2010, 07:58 PM
I totally forgot the functions in the main module :S..
Thanks to both!
I looked up your function and I found exactly what I needed (c4d.IsCommandChecked / or GetMode as donelgreeko suggested).

I don't think is a tag plugin.. I would prefer that it displays all the parameters in the attribute window. This is common with most of C4D commands.

http://img202.imageshack.us/img202/280/11337204.jpg (http://img202.imageshack.us/i/11337204.jpg/)
Like the knife tool... Is it possible to achieve the same without the pop-up window?

CGTalk Moderation
11-27-2010, 07:58 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.